1

Kindly suggest me where I'm I going wrong, It works perfectly fine in cURL but when i try doing in in salesforce I'm facing all these Issues

This is My cURL

curl -H "Authorization:Bearer 7512d7f8-2706-4dcd-a288-af482fb728ca" -H "Content-Type: application/json" -d '{"filter" :{}}' https://sandbox-api.flipkart.net/sellers/orders/search

Equivalent Http method

My Access Token : 7512d7f8-2706-4dcd-a288-af482fb728ca

public static void new_version(){

        string tok = 'https://sandbox-api.flipkart.net/sellers/orders/search';
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setTimeout(60*1000);        
        req.setEndpoint(tok);

        Blob headerValue = Blob.valueOf('7512d7f8-2706-4dcd-a288-af482fb728ca');
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization' + 'Bearer', authorizationHeader);
        req.setHeader('Content-Type', 'application/json');
        system.debug('@@@@@@@'+req);

        Http h = new Http();
        String resp;
        HttpResponse res = h.send(req);
        resp = res.getBody();        
        system.debug('@@@@@@@@@@'+res);

    }

Error Which I'm facing

System.HttpResponse[Status=Unauthorized, StatusCode=401]
2
  • Hi Sharat. In case your problem is not solved yet, I doubt you are doing a 'GET' request in CURL and doing a 'POST' request in code. So I think changin "req.setMethod('POST');" to "GET" should solve you problem. Commented Sep 2, 2016 at 8:52
  • ^Above changes along with the authorization changes suggested below. Commented Sep 2, 2016 at 8:58

3 Answers 3

1

Your Apex is combining Basic Auth and OAuth, you are confusing yourself... that approach won't work. The OAuth dance is NOT the same thing as Basic Authentication.

This is "basic-auth-ish" and won't work with OAuth bearer token. You don't need either of these lines:

Blob headerValue = Blob.valueOf('7512d7f8-2706-4dcd-a288-af482fb728ca');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

... and since your curl script is clearly OAUth, the equivalent Apex header is:

req.setHeader('Authorization', 'Bearer 7512d7f8-2706-4dcd-a288-af482fb728ca');

Make sure to include the space after the word Bearer.

To recap: remove the 2 lines and make sure your 'Authorization' header is as shown above.

0

Change this line

req.setHeader('Authorization' + 'Bearer', authorizationHeader);

into

req.setHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN');

this will solve your problem. you don't need to convert it again into encoding.

0
0

mine solve with following : it might help you.

req.setHeader('Authorization', 'OAuth 7512d7f8-2706-4dcd-a288-af482fb728ca');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .