16

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

Óscar Andreu
  • 1,630
  • 13
  • 32
sakura
  • 294
  • 2
  • 11
  • Did you get that token from an [OAuth authentication](https://developer.linkedin.com/docs/oauth2) using the [LinkedIn API](https://developer.linkedin.com/docs)? – Sumner Evans Jul 22 '15 at 03:47
  • 2
    I followed the instructions here https://developer.linkedin.com/docs/js-sdk. On the callback function I captured IN.ENV.auth.oauth_token; If its not the correct acces token, how's the correct way to get it? – sakura Jul 22 '15 at 07:40
  • Please show us the code you used to get the IN.ENV.auth.oauth_token. – Sumner Evans Jul 22 '15 at 13:57
  • I put the code here http://jsfiddle.net/rsakura/osar4po0/. Thanks, really appreciate your help. – sakura Jul 23 '15 at 02:38
  • 5
    So it looks like there is no answers ? – viq Aug 30 '16 at 12:34
  • Where do yo do the POST request? It may be that you are using an OAuth 1 token and it needs an OAuth 2 token. If you do it from the library itself, it may be that your token has expired and you need to call refresh method to refresh the token. – jesusbotella Oct 04 '16 at 14:32
  • Its a guess - I think when using javascript SDK it gives an oauth token with short expiry time and its not permanent. If you are using a backend code to get the oauth token it might be a permanent token. – Sooraj Jan 13 '17 at 20:53
  • 3
    The oauth tokens you get via JS api cannot be used server-side. I have debugged how JS uses them and they are passed via "oauth_token: ..." HTTP header instead of the "Authorization: Bearer ...". However when you try it that way from your server-side you will get the error "[unauthorized]. IP Address Mismatch" back from LinkedIn. Apparently they link the client IP address with the oauth token. Honestly I think the JS API is completely useless like this because if you can't verify/use the token server-side you basically cannot trust any of the information. – Arthur Jan 05 '18 at 00:46
  • @Arthur have you find any solution to this as I am also struggling with same issue – vinit payal Apr 07 '18 at 19:52
  • 1
    @vinitpayal I ended up using their REST API instead – Arthur Apr 11 '18 at 20:54

1 Answers1

1

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

matiasfha
  • 1,270
  • 4
  • 23
  • 42