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
RahulSFDCTSHRahulSFDCTSH 

Salesforce To Salesforce Integration of 2 different Org using Web Server OAuth Flow

Hi Everyone,

I am trying to test out web server OAuth flow between 2 different salesforce org but i am unable to get authorization code. I already have connected app set up done in my Target org and i am using Callback URL as https://[MY Domain Name].salesforce.com/services/oauth2/token and i am making a post request first to Authorization end point but when i try to check the response body in debug i get unsupported_Respose_type error. Below is code for gettting Auth code

public class getCaseFromTarget {

   String clientid = My Client Id;
   String csecret = My Client Secret;
   String auth_url = 'https://[MY Domain Name]..salesforce.com/services/oauth2/authorize';
   String response_type = 'code';
   String callback ='https://[MY Domain  Name].salesforce.com/services/oauth2/token';
   String callback_URL = EncodingUtil.urlEncode(callback, 'UTF-8');

   public class deserializeResponse {
        public String access_token;
   }
   public String ReturnAccessToken() {
       String reqbody = '?response_type=code&client_id='+cid+'&redirect_uri='+callback_URL; 
       Http h = new Http();
       HttpRequest req1 = new HttpRequest();
       req1.setBody(reqbody);
       req1.setMethod('POST');    
       req1.setEndpoint(auth_url);
       req1.setHeader('Content-Type', 'application/x-www-form-urlencoded');
       HttpResponse res = h.send(req1);
       System.debug( '**********'+res.getBody() );
       return null;/// please ignore this part. it's just temporary
   }
}

When i form the URL Manually and paste it in browser i can see the Authorization screen but getting invalid grant type error after that. I am not sure what i am doing wrong here.

I want to know if it possible to get authorization and token in same apex call implicitly. Also it would be helpful if someone can provode some sample code for this as most of the blogs or post are talking about Username-Password Flow which i don't want to use for this purpose. Thanks in Advance
sreejasreeja
hi rahul , hope this link will help out 

https://blog.lkatney.com/2018/03/07/oauth-series-web-server-oauth-flow-salesforce/


 thanks 

sreeja