使用JavaScript SDK获取linkedin访问令牌

时间:2015-07-22 03:42:37

标签: javascript linkedin access-token

我正在处理该应用程序允许用户连接到linkedin(使用javascript)。我想存储我从IN.ENV.auth.oauth_token获得的访问令牌,因为我将使用它来发布到用户的时间线。

但是当我使用这个访问令牌发布到Linkedin时,我得到了#34;无效的访问令牌"错误。 我使用了正确的访问令牌吗?获取访问令牌的正确方法是什么?

这是我的代码:

$("#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

1 个答案:

答案 0 :(得分:1)

此事件IN.Event.on(IN, "auth", OnLinkedInAuth);应将一些数据传递给您的函数OnLikedInAuth,如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);
}

正如该示例(可在文档中找到)一样,getProfileData(类似于您的OnLinkedInAuth)会返回一个Promise,当它被解析后会给你一些你需要阅读的data。在该对象中,您将找到可以存储的令牌(LocalStorage)并使用