gpt4 book ai didi

node.js - 我需要帮助尝试通过 Node 服务器的身份验证调用第 3 方 api

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:40 25 4
gpt4 key购买 nike

我正在尝试调用 2 个第 3 方 api。第一个登录并接收不记名 token ,第二个发布消息。第一个工作正常,我得到了 token 。但是当我尝试调用第二个 api 来发布消息时,它失败了,可能是因为我不知道如何在第二个 api 中设置接收到的 token

这是我的代码

var myJSONObject = {
"email": auth[0],
"password": auth[1]
};
req.post({
url: "{{server_url}}/auth/login",
method: "POST",
json: true,
body: myJSONObject
}, function (error, res, body){
if(error){
console.log(error.message);
} else {
var myJSONObject1 = {
"category":"SYSTEM",
"type": "ALERT",
"keywords":"FUNCTION|createSomethingLvl1",
"status":"UNREAD",
"from": "tenantadmin@tenantadmin.com",
"to": "someemail@gmail.com",
"subject": "Some nice subject",
"body": "Some detailed body that contains information that informs the person"
};

req.post({
url: "{{server_url}}/api/message",
method: "POST",
headers: {
"Authorization": res.body.access_token,
"Content-Type": "application/json"
},
json: true,
body: myJSONObject1
}, function (err, res1, body){
if(error){
console.log(err.message);
} else {
console.log(res1.body);
}
});
}
});

最佳答案

如果这是一个“典型”http 承载 token ,那么您需要在 token 前面添加“Bearer”一词,如下所示:

"Authorization": "Bearer " + res.body.access_token,

您可以在 OAuth RFC 6750 中查看承载 token 的示例,其中显示的语法如下:

b64token    = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
"Bearer" 1*SP b64token

而且,这是来自 Google API doc 的示例:

Authorization: Bearer AbCdEf123456

还有来自 OAuth doc 的另一个示例:

Authorization: Bearer vF9dft4qmT

关于node.js - 我需要帮助尝试通过 Node 服务器的身份验证调用第 3 方 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481349/

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