【发布时间】:2021-07-25 16:04:50
【问题描述】:
我正在尝试为我的网站制作一个不和谐的登录系统,它是用 express 制作的。我已经制作了一个函数来获取访问令牌,以便我可以在路由中使用该函数。
我正在尝试从以下位置获取访问令牌:https://discord.com/api/oauth2/token
这是我的代码:
async GetToken(code) {
let access_token;
const payload = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': redirect_uri,
'scope': scope,
};
const config = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
};
fetch(discord_token_url, {
method: 'post',
body: payload,
headers: config.headers,
}).then(response => response.json()).then(json => console.log(json)).catch(err => console.log(err));
return access_token;
},
这是我得到的错误:
{
error: 'unsupported_grant_type',
error_description: 'Grant type None is not supported'
}
如您所见,我提供了正确的授权类型,但出现此错误。
【问题讨论】:
-
嗨,尝试对有效负载进行字符串化,因此
body: JSON.stringify(payload)如文档 npmjs.com/package/node-fetch#post-with-json 中所示,或者在您的情况下,您可以尝试使用 npmjs.com/package/node-fetch#post-with-form-parameters -
我尝试将 JSON 字符串化,但也没有成功
-
您是否尝试过使用“URLSearchParams”? npmjs.com/package/node-fetch#post-with-form-parameters
-
这似乎行得通! tysm
标签: node.js express oauth-2.0 discord