+ Start a Discussion
nitin sharmanitin sharma 

Just started,Confused about rest Api's,need some help

Hi ,


I am trying to connect salesforce to salesforc connection.Please help me.


Before I start doing anything I want to have some clarity on thw whole procedure
Following are the steps which I have in my mind.

1)I will start from trigger and in the trigger I am going to call the rest Api which i will develop in

another salesforce org and pass some paremeters to it.


I have some confusion about the redirect_uri paraemeter.I am not sure about it

What's gonna be the URI redirect_uri in my case.I have gone through the docs but still not sure.

Will redirect uri be the URL of source org from which I am calling the destination salesforce org Api's

Can somebody please explain?.I need to start soon as it is critical .Please help
Abhinav GuptaAbhinav Gupta
Can you explain with more details what are you trying to build ?
nitin sharmanitin sharma
Just an example.

For example, we will be posting some account info from salesfore to another salesforce org.I know there are other ways but we need to do it through user Oauth authetication in which we need to get the code from the other org and then we will be using that code for authorization purposes and then sedn it with the authorization url.

Firstly,
How do I get that code in Apex .
2)In the body of the below given code ,I have specified rant type,I do not have any code value to assign to the response_type becasue I believe it has to come 
from the other org and I do not know how to get it from the other code. I have client secret and cliet id  and I have specified the Redirect_URI as the Endpoint URL.


Can u please guide how to start to get code .
I am assuming I will use salesforce  endpoint URL and 'https://login.salesforce.com/services/oauth2/token' and then some how get code and then 

I will use salesforce authorization URL in which I will have to pass code value and other information .


Can u please help me with the intial step?


     
     String ClientId='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
     String ClientSecret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
     HttpRequest req = new HttpRequest();
     req.setHeader('Content-Type','application/x-www-form-urlencoded');
     req.setMethod('POST');
     req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        
      req.setBody('grant_type=authorization_code'+'response_type=code'+'&client_id='+clientId +'&client_secret=' + clientSecret+'&redirect_uri='+redirectUri);
     
     
     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug('BODY: '+res.getBody());
     System.debug('STATUS:'+res.getStatus());
     System.debug('STATUS_CODE:'+res.getStatusCode());


Abhinav GuptaAbhinav Gupta
This post is showing the integration with Google : http://cloudyworlds.blogspot.in/2013/10/oauth-google-with-salesforcecom-using.html

But OAuth is mostly same, its mostly changing end points and little bit configuration.

OAuth could be done in many ways (http://help.salesforce.com/HTViewHelpDoc?id=remoteaccess_authenticate.htm&language=en_US" target="_blank). I mostly see Web Server flow in use, for which you need to setup Visualforce Pages to initiate the process with Target Org. I would suggest reading this official doc first (http://help.salesforce.com/HTViewHelpDoc?id=remoteaccess_oauth_web_server_flow.htm&language=en_US) , and this guide (https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com) as well . Then using above Google OAuth Example you can take similar steps to redirect user to OAuth to other org. 

In end of Successfull OAuth you will get access_token, which is like salesforce session id and you can use for accessing user data. 


nitin sharmanitin sharma
Yes,

You are correct.It is web server in flow.You have mentioned that we need to have visual force page to accomplish this.We cannot do it without creating visual force.Is that Correct?.I have been trying to do it without creating visual force page.

Are CallBackURL and Redirect URI same or they have to be different ?.

Thanks a lot for your help I will go thorugh these docs