2

I have created Connected App & Auth Provider.
This is the Code i am currently running. Getting error. What I am doing wrong?
Can anyone please help me with this?

1st Org - (Class which will get 10 Account details)

@RestResource(urlMapping='/v1/getAccounts/*')
global with sharing class FetchAccount {
@HttpGet
    global static list<account> fetchAccount(){
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        List<account> listAccount =[Select Id , Name from Account LIMIT 10 ];
        return listAccount ;
    }
}

2nd Org - (REST API which is calling method from 1st Org)

public class AccountRestExampleConsumeFromOtherSFOrg{

public String AccountRestExampleConsumeFromOtherSFOrg(){

String endpoint='https://login.salesforce.com/services/oauth2/token';

String username = '*******'; 
String password = '********';
String ClientId= '********************';
String ClientSecret = '***************'; 

Httprequest req = new HttpRequest();    
req.setMethod('POST');    
//req.setHeader('Content-Type','application/json');

req.setBody('grant_type=password + &client_id=' + ClientId + '&client_secret=' + ClientSecret + '&username=' + username +'&password=' + password); 

req.setEndpoint(endpoint);
//req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
Http http = new Http();
HttpResponse res;       
String Access_Token;
try {
res = http.send(req);                
system.debug('body:'+res.getBody());  
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')) {
            parser.nextToken();
            Access_Token = parser.getText();    
            } 
    }            
}catch(system.CalloutException e){            
system.debug('error'+e);
}
system.debug('access token'+Access_Token);
Httprequest req1 = new HttpRequest();  
req1.setEndpoint('https://einsteinpersi-dev- 
ed.my.salesforce.com/services/apexrest/v1/getAccounts');  
req1.setMethod('GET');    
req1.setHeader('Content-Type','application/x-www-form-urlencoded');
req1.setHeader('Authorization','Bearer '+Access_Token);            
Http http1 = new Http();
HttpResponse res1 = http1.send(req1);                 
system.debug('body11:'+res1.getBody()); 
return res1.getBody();
}
}

Error I am getting - enter image description here

  • What error are you receiving, verbatim, and on which line? Please add this to your question via an edit. – David Reed Dec 27 '18 at 12:29
  • 1
    Can you try by removing + after password in this line req.setBody('grant_type=password – Reshma Dec 27 '18 at 12:42
  • @Reshma Removed +, but still same error. One is grant_type problem and also access token is null – Mohit Kulkarni Dec 27 '18 at 14:08
  • @MohitKulkarni This is a community site; the best way for you to ensure you get help is to write a detailed, high-quality question. It's not really productive to try to tag specific people in with comments. – David Reed Dec 27 '18 at 14:45
1

There are 2 things wrong in your callout in 2nd org.

  1. Set body is not proper,

    req.setBody('grant_type=password&client_id=' + ClientId + '&client_secret=' + ClientSecret + '&username=' + username +'&password=' + password); 
    
  2. You don't have to set Authorization header as Session ID from Source org to get access token. Remove that.

Src: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm

1

Finally understood what was the issue. Solved the problem.
Although, the error it was showing was - "Invalid grant_type", actually I was not able to call API in another Org because 'Access Token' wasn't getting generated.

Answer - I had to append my Org's "Security Token" with my Org password....!!!!!

Till now I was only passing Org password in req.setBody().
Rest of the code absolutely correct & no changes required.

0

Update your setBody method by

req.setBody('grant_type=password&client_id=' + ClientId + '&client_secret=' + ClientSecret + '&username=' + username +'&password=' + password); 

Refer the format here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm

  • I found the answer...Have posted it – Mohit Kulkarni Jan 8 at 17:32

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.