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
LBSLBS 

Integrating with SFDC Apex+oAuth services

Hi There,

With referred to following blog I'm trying to integrate 2 salesforce Orgs. It follows the token requests with the token url. I have created connected Apps on the target env and remote access settings on the source org as suggested.
https://developer.salesforce.com/forums?id=906F00000009ARWIA2

I have created a VF page with a button and in the controller itself i have defined usernames, passwords, clientId/secrets to start the process. I.m getting no errors but it doesn't return the Access URL. Alaways have Null in the debug log. What could possibly go wrong? Something related to connected App settings?

Thanks,
Mudi
Best Answer chosen by LBS
LBSLBS
Found out the issue guys! its with duplicate variable of accessToken. It simply wasted 3 hours of mine

All Answers

LBKLBK
Can you post your code, so that we can take a look?
Anilkumar KotaAnilkumar Kota
Hi LBK,

Find the below blog , it clearly explained salesforce to salesforce integartion  .

http://amulhai.blogspot.in/2015/11/salesforce-to-salesforce-integration.html
LBSLBS
Hi There, 

Here is my code,

VF Page

<apex:page controller="HandleOAuthController"> 
<apex:form >
<apex:pageBlock title="Retrive Access Token And Process">
   <apex:pageBlockSection columns="1">
     <apex:commandLink action="{!doAuthorization}" value="Authorize" id="lnCommandLink"/>     
  </apex:pageBlockSection>
 </apex:pageBlock>
</apex:form>
</apex:page>

Controller
public with sharing class HandleOAuthController {
    
    private String username = '';
    private String password = '';
    private String clientId = '';
    private String clientSecret = '';
    
    public class deserializeResponse{
        public String id;
        public String accessToken;
    }
    
    public HandleOAuthController(){
        
        this.username = 'xxx@yahoo.com';
        this.password = 'xxxuGwbdHBhYY5EzaCex7K4pzf5';
        this.clientId = 'xxVG9YDQS5WtC11qKJsfXwErtfFqFioTmBglcoXTG21dL8TNWoEcgo6ZLGM_jFFib7caOwHoC4ME9kwRYQuCm';
        this.clientSecret = 'xx04062233353435285';  
    }
    
    
    public String returnAccessToken(HandleOAuthController obj){
    
        String reqBody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
        
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setBody(reqBody);
        req.SetMethod('POST');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        
        HttpResponse res = h.send(req);
        deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
        
        //HERE THE ACESS TOKEN COMES AS NULL
        system.debug('::accessToken::'+resp1.accessToken);
        system.debug('::resp1::'+resp1);
        
        return resp1.accessToken;   
                
    }
    
    
    public static void doAuthorization(){
        HandleOAuthController ctrl = new HandleOAuthController();
        String accessToken;
        
        accessToken = ctrl.returnAccessToken(ctrl);       
        
        
    }    
}

 
LBSLBS
Found out the issue guys! its with duplicate variable of accessToken. It simply wasted 3 hours of mine
This was selected as the best answer