1

I 'm using successfully oAuth v2 with the linked in api.

linkedin success authv2

it gives me an access token like that :

access_token: "AQUqvUuTXuQ2_Tr9NutHYlQQEDhiTtkHb5Jn5ozgvDfhXzu4l-TP9pE07VqYiH4QTjijzwWFa28tdmjflrtNG3ycyxNAdVx0WsWl9LcfZMSyYqSfBQjtaWtkF4WzoDoxomS7J0b0m-1avrWbsvU3ZpPc388ACtN3d2wCKgK6CXFXoHNMiz4tnHnKhtjU-yzvnFJDRTSHKKBu4lt1zZZ0hr33w-SsXqh4hAZNHxUu6s2itR85RV1cT17_EfHt2UiqeS7wB7_udxxIEYxSOk5GvdXRT5txDveKLjMs1rMhxHf72JcAoGAoxDSQXK2ek2KcFgYOcJ6Zg5L5pDImfjls4mSGWFnU-w"
​
del: function mkHttp()
​
expires_in: 5183999
​
get: function mkHttp()​
me: function mkHttpMe()​
patch: function mkHttp()​
post: function mkHttp()
​
provider: "linkedin2"
​
put: function mkHttp()​
toJson: function toJson()​
<prototype>: Object { … }

But, unfortunatly, i can't succeed to do a simple get query, after i get authenticated .

This is my first jquery try :

$(function(){
     $('#linkedin-button').on('click', function() {

        // Initialize with your OAuth.io app public key
        OAuth.initialize('MYKEYISOK');

        // Use popup for oauth
        OAuth.popup('linkedin2').done(function(result) {

            $.get( "https://api.linkedin.com/v2/me", function( data ){
              console.log(data),
            });
        })

      })

});

This is the $.get firefox console error :

{"serviceErrorCode":65604,"message":"Empty oauth2 access token","status":401}

This is my second try without jquery :

result.get( "http://api.linkedin.com/v1/companies/1441/updates", function( data ) {
console.log(data);
});

it doesnt work either .

This is my 3rd try out :

$.ajax({
    url: 'https://api.linkedin.com/v2/me',
    headers: {
         'access_token':result.access_token

    },
    method: 'GET',
    dataType: 'json',

    success: function(data){
      console.log('succes: '+data);
    }
  });

This is my 4th try out , it seems to work now :

  $.ajax({
    url: 'https://api.linkedin.com/v2/me?oauth2_access_token='+result.access_token,

    method: 'GET',
    dataType: 'json',

    success: function(data){
      console.log('succes: '+data);
    }
  });

but ive got a new error :

{"serviceErrorCode":100,"message":"Not enough permissions to access: GET /me","status":403} 

This is the explanation : Any queries to the api.linkedin.com/v2/ return "Not enough permissions to access ..."

Use r_liteProfile instead of r_basicprofile during the first step of Authorization. Use this accessToken solved all of my issues, superb !

do i send well the OAuthv2 settings along with the GET queries, to make it work ?

there is a few information there : https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-4-make-authenticated-requests

harmonius cool
  • 337
  • 1
  • 2
  • 23

1 Answers1

0

Make sure that you have used this following scopes.

r_liteprofile,r_emailaddress,w_member_social

Below code will send you the authorization flow

$params = array('response_type' => 'code',
            'client_id' => $this->api_key,
            'scope' => $this->scope,//r_liteprofile,r_emailaddress,w_member_social
            'state' => uniqid('', true), // unique long string
            'redirect_uri' => 'redirect url',
        );
        // Authentication request
        $url = 'https://www.linkedin.com/oauth/v2/authorization?' . http_build_query($params);

To receive access token use below code

$params = array('grant_type' => 'authorization_code',
        'client_id' => $this->api_key,
        'client_secret' => $this->api_secret,
        'code' => $_GET['code'],
        'redirect_uri' => base_url().$this->redirect,
    );
    // Access Token request
    $url = 'https://www.linkedin.com/oauth/v2/accessToken?' . http_build_query($params);
    $data_len = strlen(http_build_query($params));
    // Tell streams to make a POST request
    $context = stream_context_create(
            array('http' =>
                array('method' => 'POST','header'=> 'Content-Length: 0'
                )
            )
    );

    // Retrieve access token information
    $response = file_get_contents($url, false, $context);
    $token = json_decode($response);
    return $token->access_token;
augustine jenin
  • 424
  • 4
  • 10
  • Thank you a lot, but i'm using jquery front end, is there any code for this ? Finnally, i cant make it work and have the cross origin error. DO i have to use a back end PHP to be able to use the linkedin api ? – harmonius cool May 17 '19 at 13:41
  • DO i have to pass my access_token to the back end ? I'm already authenticated with the oauth2 popup, then i need to do GET and PUT queries. – harmonius cool May 17 '19 at 13:49