Magento – Consumer is not authorized to access %resources REST API

apimagento2oauthrest

I use MAGENTO 2 REST API in MobileFIrst project (Worcklight)
in my adapter :

function _retrieve(token) { 
    if (!token)
        return { 'isSuccessful': false, 'errorMsg' : 'params is ' + params};
    var authtok = "Bearer "+ token;
    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'rest/default/V1/categories/41/products',
        parameters: { },
        headers: {"Accept":"application\/json", "Authorization":authtok} 
    };
    return WL.Server.invokeHttp(input);
}

and get this result :

{"message":"Consumer is not authorized to access %resources","statusCode":401,"isSuccessful":true,"statusReason":"Unauthorized","parameters":{"resources":"self"},"responseHeaders":{"Content-Length":"95","Expires":"Thu, 19 Nov 1981 08:52:00 GMT","X-XSS-Protection":"1; mode=block","Connection":"Keep-Alive","Server":"Apache\/2.4.17 (Win32) OpenSSL\/1.0.2d PHP\/5.6.15","X-Powered-By":"PHP\/5.6.15","Cache-Control":"no-store, no-cache, must-revalidate, post-check=0, pre-check=0","Pragma":"no-cache","X-Content-Type-Options":"nosniff","Date":"Mon, 21 Mar 2016 13:04:35 GMT","Access-Control-Allow-Origin":"*","Keep-Alive":"timeout=5, max=100","Content-Type":"application\/json; charset=utf-8"},"responseTime":1899,"totalTime":1899}*/

whene i use the RestClient it works !

Status:200: OK

Best Answer

Authorization token should be passed in the following format:

Authorization: Bearer TOKEN_VALUE

In your example Bearer is missing