API calls #LinkedIn for r_basicprofile

戏子无情 提交于 2021-02-11 13:37:01

问题


I have read carefully the LinkedIn developer documents regarding API calls, following the documents, I was able to request the access code, exchange it with server and manage to get access token.

After that I managed to get some of the member's profile, using the access token in json format with the below code.

$api_url = 'https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address)?format=json';
$context = stream_context_create(array(
    'http' => array(
        'header' => "Authorization: Bearer " . $accessToken
    ),
));

$result = file_get_contents($api_url, false, $context);
echo $result;

however the issue is: trying to conduct any different calls will fail, although they are as per the documents of LinkedIn and they are allowed for the r_basicprofile, but yet the error:

Warning: file_get_contents(https://api.linkedin.com/v2/people/~:(industryId~)?format=json): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

I tried some of the following but nothing goes right

$api_url = 'https://api.linkedin.com/v2/me';
$context = stream_context_create(array(
    'http' => array(
        'header' => "Authorization: Bearer " . $accessToken
    ),
));

$result = file_get_contents($api_url, false, $context);
echo $result;

I even tried to get memeber's profile with the below GET requests, but same 403 returned.

https://api.linkedin.com/v2/people/(id:{person ID})

https://api.linkedin.com/v2/people/(id:{profileID})?projection=(id,firstName,lastName,industryId~)

Would appreciate to understand how to make those calls, and what is wrong.


回答1:


the problem is that the V2 api requires you to get a partnership with linkedin. this means you get the 403 forbidden error.

Partnering with LinkedIn provides you with additional API functionality & data access, increased call limits & dedicated support. Read more about our various Partner Programs and use cases and see if one is a match for your app. Applications are only accepted when we feel that they're providing value to members, developers and LinkedIn.

source: https://developer.linkedin.com/partner-programs

fortunatly for you there is a V1 version of the api you require.

https://api.linkedin.com/v1/people/{ID}?format=json



回答2:


Add your LinkedIN JS SDK to your site, should look something like this:

<script type="text/javascript" src="//platform.linkedin.com/in.js">
  api_key:                 <!-- YOUR CLIENT ID (API KEY) HERE -->
  onLoad:                  LinkedINJSAPI.onLoad
  authorize:               true
  credentials_cookie:      true
  credentials_cookie_crc:  true
</script>

After which in your JavaScript add in the following:

 var LinkedINJSAPI = {
   onLoad: function(){
     IN.User.authorize(LinkedINJSAPI.request, this );
   },
   request: function(){
    var url = 'people/~:(firstName,lastName,emailAddress,positions,location)?format=json';
    IN.API.Raw(url).
      method('GET').
      result(function(result){
        console.log( JSON.stringify( result ) );
      }).
      error(function(error){
        console.error( error.message );
      });
   }
 }

You can see a working demo here...

https://codepen.io/craigiswayne/pen/KGbqRq



来源:https://stackoverflow.com/questions/52936376/api-calls-linkedin-for-r-basicprofile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!