• Divya Ghodasara
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

Hi everyone,

Right now, I have connected my salesforce user to LinkedIn using OAuth. But now I want to get the basic information from LinkedIn user using Apex.
Linked In has provided 2 pages for this but to be honest, I'm clueless about OAuth or Rest API.

I have try to authenticate to OAuth using the code below but it always said "No_Oauth_State: State was not valid linkedin". Can anyone help me to fix this bug and get the profile data using REST API after that:

public class LinkedInAuth {
    
    
    public string theCode {get;set;}
    public string theTokenResponse {get;set;}
    
    /*******************************************
    *
    *   Get Authorization Code
    *
    *******************************************/
    
    public pagereference auth_Step_1(){
        
        String auth_url = 'https://www.linkedin.com/oauth/v2/authorization';
        String params =  
            '?response_type=code' +
            '&client_id=*************' +
            '&redirect_uri=https://login.salesforce.com/services/authcallback/00D7F000000opivUAA/DemoSync' + 
            //                            '&prompt=consent' + 
            '&scope=' + encodingUtil.URLEncode('r_basicprofile w_share','UTF-8') +
            '&state=DCEeFWf45A53sdfKef424';
        pageReference pr = New PageReference(auth_url + params);            
        return pr;
    }
    
    /*******************************************
    *
    *   Get Access Token
    *
    *******************************************/
    
    
    public pagereference auth_Step_2(){
        
        if(apexPages.currentPage().getParameters().get('state') != 'DCEeFWf45A53sdfKef424')
            return null;
        
        theCode = apexPages.currentPage().getParameters().get('code');
        
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        
        String auth_url = 'https://www.linkedin.com/oauth/v2/accessToken';
        String params =  
            '?code=' + apexPages.currentPage().getParameters().get('code') +
            '&grant_type=authorization_code' + 
            '&client_id=************' +
            '&client_secret=***************' + 
            '&redirect_uri=https://login.salesforce.com/services/authcallback/00D7F000000opivUAA/DemoSync';
        
        req.setMethod('POST');
        req.setEndpoint(auth_url + params);
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setHeader('Content-Length',string.valueOf(req.getBody().length()));
        
        HTTPResponse resp = http.send(req);
        
        
        theTokenResponse = resp.getBody();
        
        return null;
        
    }
}
Hi,
I have created a Component page on that page, I have create a next button, now I want when I press next button it will we call another component page .

Thanks, 
Sumit