使用JavaScript SDK获取linkedin访问令牌

sak*_*ura 16 javascript linkedin access-token

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

但是,当我使用此访问令牌发布到Linkedin时,我收到"无效访问令牌"错误.我使用了正确的访问令牌吗?如何获得Access令牌的正确方法?

这是我的代码:

$("#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);
}
Run Code Online (Sandbox Code Playgroud)

JSFiddle示例

mat*_*sfh 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);
}
Run Code Online (Sandbox Code Playgroud)

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