Javascript 节点LinkedIn模块返回;未知身份验证方案“;
我使用node linkedin npm包来验证和读取其他用户的信息(姓名、职务、公司名称、个人资料图片、共享连接)。我可以正确地接收和存储访问令牌(在我自己的LinkedIn个人资料的apps&console中验证并记录令牌),但我无法返回任何请求的信息。我的呼叫是从包文档中复制和粘贴的,但它返回以下内容:Javascript 节点LinkedIn模块返回;未知身份验证方案“;,javascript,node.js,authentication,Javascript,Node.js,Authentication,我使用node linkedin npm包来验证和读取其他用户的信息(姓名、职务、公司名称、个人资料图片、共享连接)。我可以正确地接收和存储访问令牌(在我自己的LinkedIn个人资料的apps&console中验证并记录令牌),但我无法返回任何请求的信息。我的呼叫是从包文档中复制和粘贴的,但它返回以下内容: 2018-02-28T03:46:53.459839+00:00 app[web.1]: { errorCode: 0, 2018-02-28T03:46:53.459843+00:00
2018-02-28T03:46:53.459839+00:00 app[web.1]: { errorCode: 0,
2018-02-28T03:46:53.459843+00:00 app[web.1]: message: 'Unknown authentication scheme',
2018-02-28T03:46:53.459845+00:00 app[web.1]: requestId: '3B55EVY7XQ',
2018-02-28T03:46:53.459847+00:00 app[web.1]: status: 401,
2018-02-28T03:46:53.459848+00:00 app[web.1]: timestamp: 1519789613443 }
我在下面列出了我的路线。仅出于测试目的,myToken和linkedin是linkedin控制器作用域的服务器端全局变量。(我知道最终产品需要改变这一点,这是一个学生项目。)
这是我的授权码,似乎有效:
app.get('/auth', function (req, res) {
// This is the redirect URI which linkedin will call to and provide state and code to verify
/**
*
* Attached to the redirect_uri will be two important URL arguments that you need to read from the request:
code — The OAuth 2.0 authorization code.
state — A value used to test for possible CSRF attacks.
*/
//TODO: validate state here to secure against CSRF
var error = req.query.error;
var error_description = req.query.error_description;
var state = req.query.state;
var code = req.query.code;
if (error) {
next(new Error(error));
}
/**
*
* The code is a value that you will exchange with LinkedIn for an actual OAuth 2.0 access
* token in the next step of the authentcation process. For security reasons, the authorization code
* has a very short lifespan and must be used within moments of receiving it - before it expires and
* you need to repeat all of the previous steps to request another.
*/
//once the code is received handshake back with linkedin to send over the secret key
handshake(req.query.code, res);
});
function handshake(code, ores) {
//set all required post parameters
var data = querystring.stringify({
grant_type: "authorization_code",
code: code,
redirect_uri: OauthParams.redirect_uri,//should match as in Linkedin application setup
client_id: OauthParams.client_id,
client_secret: OauthParams.client_secret// the secret
});
var options = {
host: 'www.linkedin.com',
path: '/oauth/v2/accessToken',
protocol: 'https:',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(options, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
//once the access token is received store it
myToken = JSON.parse(data);
linkedin = Linkedin.init(myToken);
ores.redirect("/");
});
req.on('error', function (e) {
console.log("problem with request: " + e.message);
});
});
req.write(data);
req.end();
}
在我的故障排除研究中,似乎我需要将令牌传递到请求中;然而,我在软件包中找不到任何地方或任何方法来这样做。每天下载的软件包都有这么多,我不可能是唯一一个遇到这个错误的人。作者在GitHub的“问题”部分没有任何帮助,对该包错误的其他搜索也是如此
我的部署:
- (当访问部署时,必须单击蓝色的“想要” 手动更改uri之前连接到LinkedIn?“链接 根据路线。结果也将仅显示在 Heroku日志,这很可能对你毫无帮助 应该是一个简单的测试,所以我只是从我的 先前的项目。)
这是我第一个没有找到答案的问题;如果我在询问时遗漏了任何重要内容,我向您道歉。提前感谢您的帮助 解决方案是将以下令牌代码传递到linkedin变量中,而不是简单地传递myToken:
linkedin = Linkedin.init(myToken.access_token || myToken.accessToken);
我不理解否决票,因为没有留下任何评论;如果我遗漏了重要或一般预期的信息,我道歉,因为这是我问的第一个问题。我想确保解决方案发布给任何跟我有相同问题的人。这个问题现在已经解决了
linkedin = Linkedin.init(myToken.access_token || myToken.accessToken);