function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
varunkaushish03@gmail.comvarunkaushish03@gmail.com 

REST API Call to Salesforce Org using Visualforce page

I'm trying to use REST APEX API that I created in a dev.Org(A). And I want to use this APEX REST Service in an another Dev Org(B) using REST call to "A" from "B". I cretaed a simple VF page with controller class and some logic inside it to call this serivce.

 

Everything works well till the point i get the "code" value after successful authorization of from "A". But when I try to create a request to get the token/refesh_token part i get a strange error:-  error=unsupported_grant_type&error_description=grant%20type%20not%20supported.

 

following is the piece of code 

 

Http h = new Http();         

HttpRequest req = new HttpRequest();   

req.setMethod('POST');   

req.setHeader('grant_type','authorization_code');       

req.setHeader('code',code);

req.setHeader('client_id',clientId );   

req.setHeader('client_secret',client_secret); 

req.setHeader('redirect_uri',callbackUrl ); 

req.setHeader('Content-Type','application/x-www-form-urlencoded');         

req.setEndpoint('https://login.salesforce.com/services/oauth2/token');            

   

HttpResponse res = null;

res = h.send(req);     

system.debug('###'+res.getBody()+'#####');

Best Answer chosen by Admin (Salesforce Developers) 
vikashvikash

This is what exaclty I did and am successfull...........

 

 It's pretty tricky, You have to get a token for a user in the target org(dev.org A in this case). You can't do this using login.salesforce.com, since you'll likely just get a token for your source org, which won't work.

 

I have posted exaclty similar query and Pat helped me alot for this to happen successfully. This post gives you whole Idea and solves your issue.

All Answers

vikashvikash

This is what exaclty I did and am successfull...........

 

 It's pretty tricky, You have to get a token for a user in the target org(dev.org A in this case). You can't do this using login.salesforce.com, since you'll likely just get a token for your source org, which won't work.

 

I have posted exaclty similar query and Pat helped me alot for this to happen successfully. This post gives you whole Idea and solves your issue.

This was selected as the best answer
rvgrvg

Hi Vikash,

 

I tried the exact same code which you posted but I get an error message saying not able to get to the service. It does the authentication and the access_token is also sent back. But when I click on the submit button, its not able to find the service.

 

Even in the same target org, if I try to execute the getnamemethod to get the results,it says unauthorized endpoint.

 

can you help me on that?

 

Thanks,