使用 JavaScript SDK 获取 Linkedin 访问令牌 [英] Get linkedin Access Token with JavaScript SDK

查看:32
本文介绍了使用 JavaScript SDK 获取 Linkedin 访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是,当我使用此访问令牌发布到 Linkedin 时,出现访问令牌无效"错误.我是否使用了正确的访问令牌?获取访问令牌的正确方法是什么?

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?

这是我的代码:

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

推荐答案

这个事件 IN.Event.on(IN, "auth", OnLinkedInAuth); 应该传递一些数据给你的函数 OnLikedInAuth 如 sdk 文档中所示.

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);
}

作为那个例子(在文档中可用),getProfileData(类似于你的 OnLinkedInAuth)返回一个 Promise,当它解决时会给你一些 data代码> 你需要阅读.在该对象中,您将找到可以存储 (LocalStorage) 和使用的令牌

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

这篇关于使用 JavaScript SDK 获取 Linkedin 访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆