Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get linkedin Access Token with JavaScript SDK

I am working on that application allow user to connect to linkedin (using javascript). I want to store access token that I got from IN.ENV.auth.oauth_token because I will use it to post to user's timeline.

But when I use this access token to post to Linkedin, I got "Invalid access token" error. Did I use correct access token? How's the correct way to get Access token?

Here's my code:

$("#linkedin-connect").on('click',function(e){  
    e.preventDefault();
    IN.UI.Authorize().place();
    IN.Event.on(IN, "auth", OnLinkedInAuth);  
    return false;
});

function OnLinkedInAuth() {
    console.debug("oauth token:" + IN.ENV.auth.oauth_token);
}

JSFiddle Example

like image 298
sakura Avatar asked Jul 22 '15 03:07

sakura


People also ask

How do I get a LinkedIn API?

Login to the LinkedIn Developer portal. Click My Apps from the top of the page and select Create App. Complete the app details and add your company page. For Self-Serve, complete all the steps and then click the Create App button at the bottom of the page.


1 Answers

this event IN.Event.on(IN, "auth", OnLinkedInAuth); should pass some data to your function OnLikedInAuth as in shown in the documentation of the sdk.

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key: YOUR_API_KEY_HERE
    authorize: true
    onLoad: onLinkedInLoad
</script>

<script type="text/javascript">

// Setup an event listener to make an API call once auth is complete
function onLinkedInLoad() {
    IN.Event.on(IN, "auth", getProfileData);
}

// Handle the successful return from the API call
function onSuccess(data) {
    console.log(data);
}

// Handle an error response from the API call
function onError(error) {
    console.log(error);
}

// Use the API call wrapper to request the member's basic profile data
function getProfileData() {
    IN.API.Raw("/people/~").result(onSuccess).error(onError);
}

As that example (available in docs) the getProfileData (similar to your OnLinkedInAuth) returns a Promise and when it's resolved will give you some data that you need to read. In that object you will find the token that you can store (LocalStorage) and use

like image 155
matiasfha Avatar answered Sep 28 '22 02:09

matiasfha