gpt4 book ai didi

node.js - 如何修复 Node.js 中 Oauth 2.0 的 'invalid_client' 错误

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

我正在尝试使用 Oauth 2.0 访问 Bexio API。我正在尝试在 Node.js 服务器上实现此功能。第一步(获取访问代码)正在运行,并且我的回调方法已执行。然而,获取访问 token 总是失败。当我执行发布请求来获取访问 token 时,我总是收到“invalid_client”错误,其描述为“在 header 或正文中找不到客户端凭据”。

我有一个客户端 ID 和密码,并且我设法使用 postman 获取了访问 token ,所以显然错误出在我的代码中。有人建议我需要对请求正文进行编码(客户端 key 中可能有“=”之类的内容),但我找不到太多相关信息。

这是我的代码:

let redirect_uri =
process.env.REDIRECT_URI ||
'http://localhost:8888/callback'

app.get('/login', function(req, res) {
res.redirect('https://office.bexio.com/oauth/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: CLIENT_ID,
scope: 'contact_edit monitoring_show',
state: 'abc',
redirect_uri
}))
})

app.get('/callback', async (req, res) => {
const code = req.query.code
axios.post('https://office.bexio.com/oauth/access_token', {

grant_type: 'authorization_code',
code: code,
redirect_uri: redirect_uri,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET
})
.then(response => {
console.log(response)
})
.catch(error =>{
console.log(error)
})
});

这是http请求:

 { url: 'https://office.bexio.com/oauth/access_token',
method: 'post',
data:
'{"grant_type":"authorization_code",
"code":CODE,
"redirect_uri":"http://localhost:8888/callback",
"client_id":CLIENT_ID,
"client_secret":CLIENT_SECRET}',
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
'User-Agent': 'axios/0.19.0',
'Content-Length': 265 }

我需要一个访问 token ,但我总是收到“invalid_client”错误,其描述为“在 header 或正文中找不到客户端凭据”

最佳答案

您必须以 application/x-www-form-urlencoded 格式将数据发送到 OAuth2 token 端点。 Axios 默认为 application/json。您可以在此处查看使用该格式的文档:https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format 。在你的情况下,它会像这样完成:

const params = new URLSearchParams();
params.append('grant_type', 'authorization_code');
params.append('code', code);
params.append('redirect_uri', redirect_uri);
params.append('client_id', CLIENT_ID);
params.append('client_secret', CLIENT_SECRET);

axios.post('https://office.bexio.com/oauth/access_token', params)

关于node.js - 如何修复 Node.js 中 Oauth 2.0 的 'invalid_client' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877287/

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