Node-LinkedIn模块返回“未知身份验证方案”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node-LinkedIn模块返回“未知身份验证方案”相关的知识,希望对你有一定的参考价值。
我正在使用node-linkedin npm包来验证和读取来自其他用户的信息(名称,职位,公司名称,个人资料照片,共享连接)。我可以正确地接收和存储访问令牌(在我自己的LinkedIn配置文件的已批准应用和控制台中验证令牌),但我无法返回任何请求的信息。我的调用是从包文档复制和粘贴的,但它返回以下内容:
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-controller范围的服务器端全局变量。 (我知道这需要改变最终产品,这是一个学生项目。)
app.get('/companies', function (req, res) {
console.log(linkedin.connections.config.accessToken);
linkedin.companies_search.name('facebook', 1, function(err, company) {
console.log('Merpy merpy mc merpers'
,company);
// name = company.companies.values[0].name;
// desc = company.companies.values[0].description;
// industry = company.companies.values[0].industries.values[0].name;
// city = company.companies.values[0].locations.values[0].address.city;
// websiteUrl = company.companies.values[0].websiteUrl;
res.redirect("/");
});
});
app.get('/companies2', function (req, res) {
linkedin.companies.company('162479', function(err, company) {
console.log(company);
res.redirect("/");
});
});
app.get('/connections', function (req, res) {
linkedin.connections.retrieve(function(err, connections) {
console.log(connections);
res.redirect("/");
});
});
这是我的授权码,似乎有效:
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的作者问题部分没有帮助,其他搜索此包的错误也是如此。
我的部署:https://linkedin-api-test.herokuapp.com/
- (访问部署时,在根据路由手动更改uri之前,必须单击蓝色的“想连接到LinkedIn?”链接。结果也只会显示在Heroku日志中,这很可能对您无益它本来应该是一个简单的测试,所以我只是偷走了之前项目的前端。)
我的回购:https://github.com/SteveSonoa/LinkedIn-Test
node-linkedin文档:https://github.com/ArkeologeN/node-linkedin/blob/master/README.md
这是我第一个无法找到答案的问题;如果我在询问时遗漏了任何重要内容,我会道歉。预先感谢您的任何帮助!
解决方案是将以下令牌代码传递给linkedin变量,而不是简单地传递myToken:
linkedin = Linkedin.init(myToken.access_token || myToken.accessToken);
我不理解downvote,因为没有留下评论;如果我遗漏重要或一般预期的信息,我会道歉,因为这是我提出的第一个问题。我想确保为遇到同样问题的任何人发布解决方案。这个问题现在解决了。
以上是关于Node-LinkedIn模块返回“未知身份验证方案”的主要内容,如果未能解决你的问题,请参考以下文章