Search code examples
arraysoauth-2.0linkedin-api

Want to fetch data of user using id in linkedin using rest api


I am using Linkedin Oauth 2.0 . Here i have fetch the token in the code below next step is that i want to fetch info of a user whose email id i'll be passing. But its giving error unable to load resource error:500.

Below is the code:

 chrome.identity.launchWebAuthFlow({
    "url": " https://www.linkedin.com/uas/oauth2/authorization?&response_type=code&client_id=" + clientid + 
                "&redirect_uri=" + encodeURIComponent(redirectUri) +
                "&state=121212121"

                  ,'interactive': true,  
                },
                function(redirect_url) { 
                    var pairs = redirect_url.split('/');
                    var values = {};
                    var code1 = redirect_url.split('?');
                    var code2 = code1[1].split('&');
                    var code3 = code2[0].split('=');
                    var actualCode = code3[1];// actual code obtained in request
                    var deferred = $q.defer();
                    var req = {
                        method: 'POST',
                        url: 'https://www.linkedin.com/uas/oauth2/accessToken?&grant_type=authorization_code&code=' + actualCode + 
                        '&redirect_uri='+ redirectUri + '&client_id=' + clientid + '&client_secret=' + clientSecretKey,
                        headers:{
                            'Access-Control-Expose-Headers': 'X-My-Custom-Header, X-Another-Custom-Header'
                        } 
                    }
                    $http(req).then(function(data){
                        deferred.resolve(data);
                        console.log("deferred.resolve(data)"+ data.data.access_token ); // gives the token generated

                        var req2 = {
                            method: 'GET',
                            url: 'https://api.linkedin.com/v1/people/[email protected]:(first-name,last-name)?format=json',
                            headers :{
                                'oauth_token': data.data.access_token,
                                'x-li-format': 'json'
                            }
                        }
                        $http(req2).then(function(data1){
                            deferred.resolve(data1);
                            console.log("sndsnd");
                            console.log("deferred.resolve(data)"+ data1.data );
                        });
                    });
                }
            );

Solution

  • You dont have to specify email and fields for the basic profile data. Use

    url : https://api.linkedin.com/v1/people/~?format=json

    and the correct accesstoken with "r_basicprofile" permission to get the basic details like firstName, lastName, id, headline and siteStandardProfileRequest.

    For retreiving additional profile fields, use

    url : https://api.linkedin.com/v1/people/~:(id,num-connections,picture-url)?format=json
    

    For available profile fields using "r_basicprofile" permission, check https://developer.linkedin.com/docs/fields/basic-profile

    For full profile details of the user, your app needs to get access from Linkedin to use "Apply with Linkedin"

    https://developer.linkedin.com/docs/fields/full-profile

    https://developer.linkedin.com/docs/apply-with-linkedin

    Apply with Linkedin application Form :

    https://help.linkedin.com/app/ask/path/api-dvr