node.js - Discord Ouath2 访问令牌“不支持授予类型无”
问题描述
我试图为我的网站制作一个不和谐的登录系统,它是用 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'
}
正如你所看到的,我给出了正确的授权类型,但我得到了这个错误。
解决方案
忘记更新以添加解决方案,看到很多人在看这个,所以这里是解决方案(感谢@Kira):你必须使用URLSearchParams
// Modules
const fetch = require('node-fetch');
const { url } = require('inspector');
const { URLSearchParams } = require('url');
// Add the parameters
const params = new URLSearchParams();
params.append('client_id', client_id);
params.append('client_secret', client_secret);
params.append('grant_type', 'authorization_code');
params.append('code', code);
params.append('redirect_uri', redirect_uri);
params.append('scope', scope);
// Send the request
fetch('https://discord.com/api/oauth2/token', {
method: 'post',
body: params,
headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' },
}).then(r => r.json()).then(Response => {
// Handle it...
handle()
});
推荐阅读
- ios - 选择单元格时如何将collectionviewcell自动滚动到最近的单元格(上一个或下一个)
- python-3.x - 带有烧瓶和 UWSGI 的芹菜
- c# - InputSelect - 验证未按预期工作
- nativescript - 如何在 nativescript-pdf-view 中为每个页面留出边距
- oracle - pl/sql 中的循环条件返回值
- c# - .NET Core MVC 视图模型中的数据持久性
- php - 试图在同一索引页面上获取 AJAX POST JSON
- android - Android 启动画面拉伸
- python - 在 Python 中比较两个列表并创建一个组合的新列表
- usb - 当端点和 PMA 地址都更改时,CubeMX 生成的 USB HID 设备发送错误数据