0

this error is coming when i m clicking connect with linkedin this button is created in doGetAuthorizationcode method i can't understand why is client id parameter nt passing,here is the code public class LinkedlnoAuthController{

public Boolean iscode { get; set; } 
public String authCodeFromuRL { get; set; }    
public List<Linkedin_Information__c> linkedlnInfoList { get; set; }   
public String BasicInfo { get; set; }   
public String urlToSharedPost { get; set; }
public String sharedInformation { get; set; }
public Final String consumerkey = 'xyz';      **given real id and secret in org**
public Final String consumerSecret ='abc' ;
public Final String redirect_URI = 'https://rahultiwari-dev-ed--c.visualforce.com/apex/Linkedinoauth?core.apexpages.request.devconsole=1' ;
public String linkedin_Scope = 'r_fullprofile%2er_emailaddress%2er_network%2er_compliance' ;
public Final String linkedln_Authurl = 'https://www.linkedin.com/oauth/v2/authorization?';    
public Final String accessToken_Endpoint = 'https://www.linkedin.com/oauth/v2/accessToken' ;


public LinkedlnoAuthController(){
    
    authCodeFromURL = ApexPages.currentPage().getParameters().get('code');  
    if(authCodeFromuRL == null || authCodeFromuRL == '')
        isCode = true;
    else
        isCode = false;
    
    linkedlnInfoList = [Select Id, Name,Access_Token__c,Expires_In_Seconds__c From Linkedin_Information__c ];
}

public PageReference doGetAuthorizationcode(){    **this is the method for connecting to linkedin**

    string requestToAuthUrl= linkedln_Authurl + 'response_type=code &client_Id='+consumerKey +'&redirect_uri='+redirect_URI+
        '&state=12345567dggd&scope='+linkedin_Scope;
    system.debug('@@@@doGetAutharizationcode'+ requestToAuthUrl);
    PageReference pageref = new PageReference(requesttoAuthurl);
    return pageref;
}
public void doGetAccesstoken(){
    Http http = new Http();
    HttpRequest httpreq = new HttpRequest();
    HttpResponse httpRes = new HttpResponse();
    
    String requestokenbody = 'code='+authcodefromurl+ '&grant_type=authorization_code'+
        '&client_Id='+consumerkey+ '&client_secret='+consumersecret+
        '&redirect_Uri='+redirect_URI;
    system.debug('####doGetAccesstoken'+requestokenbody);
    string errormessage = '';
    system.debug('### authcodefromurl'+authcodefromurl);
    System.debug ( '&&& requestokenbody' +requestokenbody);
    httpReq.setMethod('POST');
    httpReq.setEndpoint(accesstoken_Endpoint);
    httpReq.setHeader('content-type','application/x-www-form-urlencoded');
    httpReq.setBody(requestokenbody);
    
    try{
        httpRes = http.send(httpReq);
        if(httpRes.getStatusCode()==200){
            Map<string,object> Tokeninfo = (Map<string,object>)Json.deserializeuntyped(httpRes.getBody());
            Linkedin_Information__c Linkedininfo = new Linkedin_Information__c();
            Linkedininfo.Access_Token__c = String.valueOf(Tokeninfo.get('access_token'));
            Linkedininfo.Expires_In_Seconds__c = Double.valueof(Tokeninfo.get('expires_in'));
            Linkedininfo.Name = 'Linkedininfo';
            if(linkedlnInfoList == null && linkedlnInfoList.size()>0){
                Linkedininfo.Id = linkedlnInfoList[0].Id;
            } ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,
                                                         'Congrattz2z2z..... You have done successfull Integration with Linkedin' ));
            upsert linkedInInfo;
            
        }else{
            errormessage = 'Unexpected Error while communicating with Linkedin API'
                +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
            
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
        }
    }catch(system.exception e){
        system.debug('#### Exception Excuted '+e.getStackTraceString()+' '+e.getMessage());
        if (String.valueof(e.getMessage()).startswith( 'Unauthorized endpoint')){
            errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->'
                + 'Remote site setting and add' +' '+ accessToken_Endpoint +' Endpoint';
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
        }else{
            
            errormessage = 'unexpected Error while communinicating with Linkedin API'+'status'+httpres.getStatus()+
                'and status code'+httpres.getStatusCode();
            Apexpages.addmessage(new apexpages.message(Apexpages.severity.Error,errormessage));
        }
    }
}

public void doFetchBasicInfo(){
    string errormessage = '';
    Http http = new Http();
    HttpRequest httpReq = new HttpRequest();
    HttpResponse httpRes = new HttpResponse();
    list<Linkedin_Information__c> linkedIninfoListNew = [Select id,name,Access_Token__c,Expires_In_Seconds__c from Linkedin_Information__c where name='LInkedininfo'];
    httpReq.SetMethod( 'GET' );
    httpReq.setendpoint( 'https://api.linkedin.com/v1/people/~format=json');
    httpReq.setHeader( 'Authorization', 'Bearer '+ linkedIninfoListNew[0].Access_Token__c);
    httpReq.setHeader( 'Content-Type', 'application/json');
    try{
        httpRes = http.send(httpReq);
        if(httpRes.getStatusCode() == 200){
            Map<string,object> Tokeninfo = (Map<string,object>)Json.deserializeuntyped(httpRes.getBody());
            string firstname = string.valueof(Tokeninfo.get('firstname'));
            string lastname = string.valueof(Tokeninfo.get('lastname'));
            string headline = string.valueof(Tokeninfo.get('headline'));
            
            Basicinfo = firstname +' ' + lastname+' '+headline;
        }else{
            errormessage = 'Unexpected Error while communicating with LinkedIn API.'
                +'status'+httpres.getstatus()+'and status code'+httpres.getStatusCode();
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
        }
    }catch(System.Exception e){
        system.debug('#### Exception Executed '+e.getStackTraceString()+' '+e.getMessage());
        if (string.valueof(e.getMessage()).startswith( 'Unauthorized endpoint')){
            errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Admin -> security control'
                + 'Remote Site setting and add' +' '+' https://api.linkedin.com/'+'Endpoint';
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,errormessage));
        }else{
            errorMessage = 'Unexpected Error while communicating with LinkedIn API'
                +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getstatuscode();
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errormessage));
        }
    }
}

public void dosharePostonLinkedin(){
    string errorMessage = '';
    Http http = new Http();
    HttpRequest httpReq = new HttpRequest();
    HttpResponse httpRes = new HttpResponse();
    List<Linkedin_Information__c> linkedlninFOlIStNeW = [select Id, Name,Access_Token__c,Expires_In_Seconds__c From Linkedin_Information__c];
    httpReq.SetMethod( 'PosT' );
    httpReq.setEndpoint( 'https://api.linkedin.com/v1/people/~/shares?format=json');
    httpReq.setHeader( 'Authorization', 'Bearer' +linkedlnInfoListNew[0].Access_Token__c);
    httpReq.setHeader( 'Content-Type', 'application/json');
    httpReq.setHeader('x-li-format' , 'json');
    
    string requestbody  =  '{'+
        '"comment":"checkout developer.linkedin.com!",'+
        '"content":{'+
        '"title":"Linkedin developer resources",'+
        '"description":"Leverage Linkedin APIs to maximum engagement",'+
        '"submitted_url":"https://developer.linkedin.com"'+
        '},'+
        '"visibility":{'+
        '"code":"anyone"'+           
        '}'+
        '}';
    httpreq.setBody(requestbody);
    try{
        httpres = http.send(httpreq);
        if(httpres.getStatusCode()==200 || httpres.getStatusCode()==201){
            sharedinformation = requestbody;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, 'Post has been shared successfully!!'));
            Map<String,object> sharedPostInformaionMap = (Map<String, object>)JSON.deserializeUntyped(httpRes.getBody());
            urlToSharedPost = 'https://'+(String)sharedPostInformaionMap.get('updateUrl');
        }else{
            errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
                +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
        }
    }catch(System.Exception e){
        System.debug('#### Exception Excuted '+e.getStackTraceString()+' '+e.getMessage());
        if (String.valueof(e.getMessage()).startsWith('Unauthorized endpoint')){
            errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->'
                +' Remote Site Setting and add '+' '+ 'https://api.linkedin.com/ '+' Endpoint';
            
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
        } else{ 
            errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
                +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
            
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
        }
    }
}

}

here is the vf page

<apex:page controller="LinkedlnoAuthController" >
    <br/>
    <apex:slds />
    <apex:form id="theForm">
        <apex:actionstatus id="statusLinkdln">
            <apex:facet name="start">
                <div class="waitingsearchDiv" id="el_loading" style="background-color: #fbfbfb; height:100%;opacity:0.65;width:100%;">
                    <div class="waitingHolder" style="top: 10@px; width: 91px;">
                        <img class="waitingImage" src="/img/loading.gif" title="Please wait.."/>
                        <span class="waitingDescription">Loading...</span>
                    </div>
                </div>
            </apex:facet>
        </apex:actionstatus>
        <apex:pagemessages >
        </apex:pagemessages>
        <apex:pageBlock >
            <apex:pageBlockButtons location="top" >
                <apex:commandButton value="Connect With Linkedln" action="{!doGetAuthorizationcode}" rendered="{!iscode}" /> 
                <apex:commandButton value="Complete Authorization" action="{!doGetAccessToken}" rendered="{!isCode}" rerender="theForm" status="statusLinkdln" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Get Basic Info" action="{!doFetchBasicInfo}" status="statusLinkdln" rerender="theForm, thePanel" />
                <apex:commandButton value="Share Post on LinkedIn" action="{!doSharePostOnLinkedIn}"
                                    status="statusLinkdln" rerender="theForm, thePanel, sharedPanel" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:outputPanel id="thePanel">         
                {!Basicinfo} <br/>
                {!Sharedinformation}
            </apex:outputPanel>
        </apex:pageBlock>       
    </apex:form>
</apex:page>

can anyone pls tell me d sol

1 Answer 1

1

simple but maybe powerful guess write the I of client_id in lowercase not uppercase.

Regards, Bernhard

2
  • now no error is coming but nothing is happening Aug 18, 2020 at 11:58
  • search through the logs, maybe you can find something Aug 19, 2020 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .