0

I am trying to get the access token by calling PayPal API,I Am getting Client credentials are missing response.

 public static HttpResponse  GetAccessToken() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.sandbox.paypal.com/v1/oauth2/token');
        request.setMethod('POST');
        request.setHeader('content-type', 'application/x-www-form-urlencoded');
        //request.setHeader('grant_type', 'client_credentials');
        request.setBody('grant_type=client_credentials'); 
        String username = 'username';
        String password = 'password';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic' +
            EncodingUtil.base64Encode(headerValue);
        request.setHeader('Authorization', authorizationHeader);
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            /*List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for (Object animal: animals) {
System.debug(animal);
}*/
        }
        System.debug(response.getBody());
        return response;   
    }

Response:

{"error":"invalid_client","error_description":"Client credentials are missing"}

Can anyone please help me What is missing here. It's working in Postman client.

  • Instead of setting grant type in header, set it in the body: setBody('grant_type=client_credentials' ); – Shailesh Patil Jul 20 at 10:32
  • request.setBody('grant_type=client_credentials'); Tried, still same response. Updated my code – SFDC_Beginner Jul 20 at 10:45
  • try with grant type present at body and header both the places – User6670 Jul 20 at 11:55
0

Below code working fine, Not sure what's different between the above and below code.

 String username = 'username';
        String password = 'password';
        string endpoint = 'https://api.sandbox.paypal.com/v1/oauth2/token';
        string body = 'grant_type=client_credentials';
        
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic ' +  EncodingUtil.base64Encode(headerValue);
        system.debug('Encoder Value' + EncodingUtil.base64Encode(headerValue));
        http http = new http();
        httpRequest request = new httpRequest();
        request.setMethod('POST');
        request.setEndpoint(endpoint);
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        request.setHeader('Authorization', authorizationHeader);
        request.setBody(body);
        httpResponse Res = http.send(request);
| 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.