gpt4 book ai didi

node.js - Nodejs twitter api 不返回预期的 token

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:45 25 4
gpt4 key购买 nike

我正在使用nodejs来获取不记名 token ,我的代码如下

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 的预期返回是我的不记名 token ,它在 postman 应用程序中这样做,但在这里我收到错误 {"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_​​missing_parameter"}]}

有谁知道为什么 api 服务器无法读取授权类型

最佳答案

您的问题只是一个拼写错误。在这一行:

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

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

当我使用此curl命令发送无效的内容类型时,我看到了与您相同的错误:

$ 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"}]}

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

$ 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>"}

关于node.js - Nodejs twitter api 不返回预期的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35381232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com