正如 jar 头上所说的那样。
无论如何,这段代码应该是有效的。它完全遵循 https://dev.twitter.com/oauth/application-only 的内容页说。然而,它不断返回 {{"errors":[{"code":99,"message":"Unable to verify your credentials","label":"authenticity_token_error"}]}
[在 0.551 秒内完成]
,我不知道为什么。
var request = require('request');
var options = {
url: 'https://api.twitter.com/oauth2/token',
method: 'POST',
headers: {
"Authorization": "Basic " + new Buffer("[key]:[secretkey]").toString('base64'),
"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
},
body: "grant_type=client_credentials"
};
request.post(options, function(error, response, body){
console.log(body);
});
快速说明一下,由于存在固有的敏感性,此处的代码中省略了 key 和 secret key 。你可以把你自己的key/secretkey放在那里试试看,但我不愿意提供我自己的。
谢谢!
编辑:
最初的问题是 contentType 应该是 Content-Type,这在技术上解决了它,但实际上它只是改变了我得到的错误。
更新:
将缓冲区 key 转码为 base64 并返回表明授权 key 的完整性在整个编码过程中保持不变,并且根据 Twitter 提供的文档,Basic [base64keyhash] 是有效的。
最佳答案
由于您已经在选项中指定了方法:“POST”,因此请尝试使用 request() 而不是 request.post():
var request = require('request');
var options = {
url: 'https://api.twitter.com/oauth2/token',
method: 'POST',
headers: {
"Authorization": "Basic " + new Buffer("[key]:[secretkey]").toString('base64'),
"contentType":"application/x-www-form-urlencoded;charset=UTF-8"
},
body: "grant_type=client_credentials"
};
request(options, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
关于node.js - 推特 API, "Unable to verify your credentials.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37816329/