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]
Best Answer
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:
... and since your curl script is clearly OAUth, the equivalent Apex header is:
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.