Accessing LinkedIn Profile API me endpoint

One of  the requirements I came across for the Sitecore MVP site is pick up profile pic from social media. So, I picked LinkedIn as the first candidate to do some basic steps. This also is a way to train myself for composable DXP  API-first world. So, here are the basic steps involved to access the LI Profile API from Postman and then get the profile pic url. I picked the profile API since its the simplest and doesn't need any filling of marketing forms and justification etc. for accessing the endpoints!

Nevertheless, here is the step-by-step to setup/access the LinkedIn Profile API:

1. Generate token



2. Create a new LinkedIn app:



3. Since a personal LI profile url will not work and you need a company url, create a new company here:


4. The company created above should be added to the LinkedIn app:


5. Once the app is created, you can access the auth tab to check the credentials and these credentials are our stepping stone for accessing the Profile API:




But, before that, we need the oAuth 2.0 token created through oAuth 2.0 tools accessible through the link highlighted in the right side  in the above screen shot.

6. Click Create Access token in the tools page:


Note that the client id is passed as a param for the above page

7.a Before you can generate the oAuth 2.0 token, you must have products selected in your app. This tab actually specifies what APIs the oAuth 2.0 can access. The products are selected here:



7.b Since I aim to access the me endpoints, I just added the above product. In case if  you have to access the people endpoints wherein you can pass the vanityname of a user and get their details, you need to select the following product:

8. Associated API endpoints for ones in 7.a:


Associated endpoint for 7.b:


Note that the marketing endpoints need a form to be filled providing justification for accessing the API!

9. So, the next step is, providing access for the app to a profile so that a oAuth 2.0 token is created and with this token, API calls can be made:


10. Since, the product we chose encapsulates the profile API's me endpoints, I was asked to login to LI and my personal profile is picked automatically as follows:


11. oAuth 2.0 token is created:


12. Fire-up Postman and pass the access token from the Authorization tab as follows:


13. Response from the me endpoint:


14. Now, use the following endpoint to pick the profile pic  url:

https://api.linkedin.com/v2/me?projection=(id,profilePicture(displayImage~digitalmediaAsset:playableStreams))



15. Here is my profile pic from the above url:


On the other hand, if you don't have access to the people endpoints, 

Another approach is to pass a url with the following params to  the LI  user (whose profile must be fetched) in the following format:

https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={your_ln_app_client_id}&redirect_uri=https://www.linkedin.com/developers/tools/oauth/redirect&state=dummyval&scope=r_liteprofile%20r_emailaddress%20w_member_social


Note that the redirect uri must be registered in the LI App as pointed above and ideally it will be a page that does processing of the code generated (covered down the line).

Moving forward, when the user clicks the above url, they will see the LI login page and they can login with their credentials:


Once they login and provide consent to fetch their personal information, a code will be generated in the redirected page url:


So, you have a query string param in the following format:

code=AQRl10lxjqUSwl8KREG0cjJD2JHUk9VreW7rlQGKt0R0HEprhXiVwIHfPIxarqgVk7oEy37sKV69kIK5sobR3hLfQyiK92X4CYrjC1QRjBRk8PQUvnJSQ4LXulnA7eETbfNztVCrhY1fYqC1sdfsfdsdfsfsdfsfsfsfsfsfsfsdfsfsfsfsfsdfssfdf

Now, in the redirected  page, fetch the value in the code param and get the access token through this GET endpoint:

https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&client_id=dfdffffdffdfddff&client_secret=fgfgfgfgfgfgfgfggf&code=AQRl10lxjqUSwl8KREG0cjJD2JHUk9VreW7rlQGKt0R0HEprhXiVwIHfPIxarqgVk7oEy37sKV69kIK5sobR3hLfQyiK92X4CYrjC1QRjBRk8PQUvnJSQ4LXulnA7eETbfNztVCrhY1fYqC1sdfsfdsdfsfsdfsfsfsfsfsfsfsdfsfsfsfsfsdfssfdf&redirect_uri=https://www.linkedin.com/developers/tools/oauth/redirect

Note that the above code expires as soon as you use it!


On the other hand, the response from above end point, is an access token that can be passed to the me endpoint to fetch the profile pic or other personal info of the consented user (already highlighted in steps 13 and 14).




Comments

Popular Posts