4

I have below cURL:

curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "<Client-Id>:<Secret>" \
-d "grant_type=client_credentials".

I want it to be in Http request model as below :

 req.setEndpoint('https://api.sandbox.paypal.com/v1/oauth2/token');
 req.setMethod('POST');
 req.setHeader('Content-Type','application/x-www-form-urlencoded');
 req.setBody('grant_type=client_credentials&<Client-Id>:<Secret>');   //replaced clientid and secret with the values from sandbox.

But i am getting below code as response : HttpResponse[Status=Not Acceptable, StatusCode=406]

Can any one point me what was the issue.

3

As far as I can tell a 406 response most likely indicates you are missing a supported Accept header.

There are some parts missing from what I can see of your Apex code.

In cURL, the -H option sets a header. You will need to use the corresponding setHeader(key, value) method in Apex to set the Accept and Accept-Language headers.

The -u header sets the user name and password for server authentication. The -d header sets the post request body.

These two have been merged in your example. I'm not sure if you can just append the authentication details onto the request body like that. I suspect you will need to use a named credential when setting the endpoint.

The older question Basic Http Authentication has an example of how to manually set the header to correspond to the -u option:

Blob headerValue = Blob.valueOf(clientId +':' +password);
String authorizationHeader = 'Basic ' + 'EncodingUtil.base64Encode(headerValue);
system.debug('Encoder Value' + EncodingUtil.base64Encode(headerValue));
req.setHeader('Authorization', authorizationHeader);
|improve this answer|||||
  • So what should we use for -u Header for Http Request. Like -H can be taken as req.setHeader(key,value); – sunny Jul 7 '15 at 9:33
  • @sunny It looks like you can manually base64 encode the authentication details and use them to set the Authorization header. I slightly modified the example from the linked question to suit your needs. – Daniel Ballinger Jul 7 '15 at 9:41
1

You need to set basic authorization header like below, also i have added missing headers.

 req.setEndpoint('https://api.sandbox.paypal.com/v1/oauth2/token');
 req.setMethod('POST');
 req.setHeader('Content-Type','application/x-www-form-urlencoded');
 req.setHeader('Accept','application/json');
 req.setHeader('Accept-Language','en_US');
 Blob headerValue = Blob.valueOf(<Client-Id>+':'+<Secret>);
 String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
 req.setHeader('Authorization', authorizationHeader);
 req.setBody('grant_type=client_credentials');
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.