不支持不一致的访问令牌;授予类型为无; [英] Discord Ouath2 Access Token 'Grant type None is not supported'

查看:7
本文介绍了不支持不一致的访问令牌;授予类型为无;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的网站做一个不和谐的登录系统,这是用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()
});

这篇关于不支持不一致的访问令牌;授予类型为无;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆