Javascript &引用;无效的“授权”;尝试使用Discord时出错';非统组织2

Javascript &引用;无效的“授权”;尝试使用Discord时出错';非统组织2,javascript,oauth-2.0,discord,Javascript,Oauth 2.0,Discord,当我运行上面的代码时,它返回{error:'invalid_grant'},我不知道为什么。这有什么解决办法吗?谢谢。您是否也需要根据文档发送范围参数: const response = await fetch(`https://discordapp.com/api/oauth2/token`, { method: "POST", headers: { "Content-Type": "application/x-www-form

当我运行上面的代码时,它返回
{error:'invalid_grant'}
,我不知道为什么。这有什么解决办法吗?谢谢。

您是否也需要根据文档发送范围参数:

const response = await fetch(`https://discordapp.com/api/oauth2/token`, {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: JSON.stringify({
    client_id: process.env.CLIENT_ID,
    client_secret: process.env.CLIENT_SECRET,
    redirect_uri: redirectURL,
    grant_type: 'authorization_code',
    code: code,
  }),
});
您需要将您的身体作为URLSearchParams(数据)发送

您还需要在代码顶部添加以下内容:

const data = {

        client_id: 'your client id',
        client_secret: 'your client secret',
        grant_type: 'authorization_code',
        redirect_uri: 'your redirect uri',
        code: accessCode,
        scope: 'the scopes',
    };
    
    fetch('https://discord.com/api/oauth2/token', {
        method: 'POST',
        body: new URLSearchParams(data),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
    })
        .then(res => res.json())
        .then(console.log);
检查此新链接:


您是否也需要根据文档发送范围参数:

const response = await fetch(`https://discordapp.com/api/oauth2/token`, {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: JSON.stringify({
    client_id: process.env.CLIENT_ID,
    client_secret: process.env.CLIENT_SECRET,
    redirect_uri: redirectURL,
    grant_type: 'authorization_code',
    code: code,
  }),
});
您需要将您的身体作为URLSearchParams(数据)发送

您还需要在代码顶部添加以下内容:

const data = {

        client_id: 'your client id',
        client_secret: 'your client secret',
        grant_type: 'authorization_code',
        redirect_uri: 'your redirect uri',
        code: accessCode,
        scope: 'the scopes',
    };
    
    fetch('https://discord.com/api/oauth2/token', {
        method: 'POST',
        body: new URLSearchParams(data),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
    })
        .then(res => res.json())
        .then(console.log);
检查此新链接:


我修复了它,我所要做的就是使用
querystring
而不是JSON。

我修复了它,我所要做的就是使用
querystring
而不是JSON。

我通过了范围,但仍然没有用。您现在遇到了什么错误?与以前相同的错误,
{error:'invalid_grant'}
我将发布另一个代码供您尝试。当然,谢谢!我通过了检查范围,但仍然无济于事。您现在遇到了什么错误?与以前相同的错误,
{error:'invalid_grant'}
我将发布另一个代码供您尝试。当然,谢谢!