【问题标题】:Nodejs twitter api does not return expected tokenNodejs twitter api没有返回预期的令牌
【发布时间】:2016-05-24 16:51:31
【问题描述】:

我正在使用 nodejs 来获取不记名令牌,我的代码看起来像

var fs = require("fs");
var https = require("https");
var querystring = require("querystring");
var bearer = "cunsomer_key:cunsomer_secret"
var base64ed = new Buffer(bearer).toString("base64");

var options = {
    port: 443,
    hostname: "api.twitter.com",
    path: "/oauth2/token",
    method: "post",
    headers: {
        Authorization: "Basic " + base64ed,
        "Content-Type": "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
        "User-Agent": "socialginie"
    },
    key: fs.readFileSync("./testssl.key"),
    cert: fs.readFileSync("./testcert.cert"),
}

var req = https.request(options, res => {
    res.on("data", d => {
        console.log(d.toString());
    })
})
req.on("error", e => {
    console.log(e);
});
req.write(querystring.stringify({
    "grant_type": 'client_credentials'
}))
req.end();

api 的预期返回是我的不记名令牌,它在邮递员应用程序中这样做,但在这里我收到错误 {"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}

有谁知道为什么 api server 不能读取授权类型

【问题讨论】:

    标签: node.js api httprequest twitter-oauth node-request


    【解决方案1】:

    您的问题只是一个错字。在这一行:

        "Content-Type": "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
    

    您将“Content-Type”指定为标题的一部分。

    当我使用此 curl 命令发送无效的 Content-Type 时,我看到了与您相同的错误:

    $ curl --data "grant_type=client_credentials" -H "Authorization: Basic <credentials-omitted>" -H "Content-Type:" -H "Content-Type: Content-Type: application/x-www-form-urlencoded;charset=UTF-8" https://api.twitter.com/oauth2/token
    {"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}
    

    如果我更正标题,我会得到一个令牌:

    $ curl --data "grant_type=client_credentials" -H "Authorization: Basic <credentials-omitted>" -H "Content-Type:" -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" https://api.twitter.com/oauth2/token
    {"token_type":"bearer","access_token":"<token-omitted>"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      相关资源
      最近更新 更多