0

I'm new to integration. I am going to generate an access token for the third-party application.

I tried to build following javascript request in sfdc.

But the response body is blank. I need to retrieve access token & other things to call specific endpoint in another class.Please let me know where i did wrong

I am searlize json string also while sending the body

They gave same code request sample in java format like below.

var result = {};
var application = "";
var vendor = "";
var businessunit = "";
var user = "";
var pass = "";
var authCode = window.btoa(application + "@" + vendor + ":" + businessunit);
$.ajax({
    "url": 'https://',
    "type": 'post',
    "contentType": 'application/json',
    "dataType": 'json',
    "headers": {
        'Authorization': 'basic ' + authCode
    },
    "data": JSON.stringify({
    "grant_type": 'password',
    "username": user,
    "password" : pass,
    "scope": 'AdminApi AgentApi AuthenticationApi PatronApi RealTimeApi'
    }),
    "success": function (resp) {
        result.access_token = resp.access_token;
        result.token_type = resp.token_type;
        result.resource_server_base_uri = resp.resource_server_base_uri;
        result.expires_in = resp.expires_in;
        result.refresh_token = resp.refresh_token;
        result.scope = resp.scope;
        result.refresh_token_server_uri = resp.refresh_token_server_uri;
    },
    "error": function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Failed to retrieve token.\n" + XMLHttpRequest.status + ' ' 
            + XMLHttpRequest.statusText);
    }
});

My Apex class

    public class AuthCallout {
    public void basicAccessTokenGeneration() {
     HttpRequest req = new HttpRequest();
     req.setEndpoint('https://');
     req.setMethod('POST');

     // Specify the required username and password to access the endpoint
     // As well as the header and header information

     String username = '';
     String password = '';
     String vendor   = '';
     String businessunit ='';
     String application ='';

     Blob headerValue = Blob.valueOf(application + '@' + vendor + ':' + businessunit);
     String authorizationHeader = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue);
     req.setHeader('Authorization', authorizationHeader);


    JSONGenerator gen = JSON.createGenerator(true);    

    gen.writeStartObject();      
    gen.writeStringField('grant_type ', 'password');
    gen.writeStringField('username', username);
    gen.writeStringField('password',password);
    gen.writeStringField('scope','');
    gen.writeEndObject();
    String jsonS = gen.getAsString();
    system.debug('DAta in '+str);
    system.debug('Data in gen'+jsonS);
    req.setBody(jsonS);
     // Create a new http object to send the request object
     // A response object is generated as a result of the request  

     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug(res.getBody());
        System.debug(res.getStatusCode());
        System.debug(res.getBodyDocument());
   }
}
11
  • Do you know what is the response body which is receiving from third party? As per your comment it's successfully sent your request to third party, Can you check with admin of third party? Commented Aug 7, 2018 at 15:45
  • As per there documentation, they said you will get access token and other stuff like this result.access_token = resp.access_token; result.token_type = resp.token_type; result.resource_server_base_uri = resp.resource_server_base_uri; result.expires_in = resp.expires_in; result.refresh_token = resp.refresh_token; result.scope = resp.scope; result.refresh_token_server_uri = resp.refresh_token_server_uri
    – Pavan tej
    Commented Aug 7, 2018 at 15:54
  • But i got response body is blank
    – Pavan tej
    Commented Aug 7, 2018 at 15:54
  • Can you paste your response here? Commented Aug 7, 2018 at 15:58
  • Debug logs are blank Only it is displaying status code 200 and status OK
    – Pavan tej
    Commented Aug 7, 2018 at 23:15

1 Answer 1

0

I'm able to get the response body with an access token. The error is the password of the client I'm using in above API call.

When I check from postman I got the status code as

409 Login failed. You must change your password.

Once I reset the password. Everything works smoothly

You must log in to answer this question.

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