简体   繁体   English

当我使用 Discord OAuth2 时,我收到错误 400。我怎样才能让它正确返回 access_token?

[英]When I use Discord OAuth2 I am getting error 400. How can I make this return the access_token correctly?

I am unable to get the clients identiy through discords oauth2.我无法通过不和谐 oauth2 获取客户身份。 First we do this:首先我们这样做:

https://discord.com/api/oauth2/authorize?client_id=9999999999999&redirect_uri=http%3A%2F%2Fxxxx.xyz%2F&response_type=code&scope=identify https://discord.com/api/oauth2/authorize?client_id=9999999999999&redirect_uri=http%3A%2F%2Fxxxx.xyz%2F&response_type=code&scope=identify

to get their code.获取他们的代码。 Which seems to work fine.这似乎工作正常。

let options = {
  url: 'https://discord.com/api/oauth2/token',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: new URLSearchParams({
    'client_id': '9999999999999',
    'client_secret': 'MYSECRETHERE',
    'grant_type': 'authorization_code',
    'code': code,
    'redirect_uri': 'https://xxxx.xyz/callback',
    'scope': 'identify'
  }).toString()
};


   await fetch("https://discord.com/api/oauth2/token", options)
    .then(handleErrors)
    .then(response => response.json())
    .then(response => {
        access_token = response.access_token;
    }).catch(function(error) {
        console.log(error);
    });

What happens here is I get a error 400 instead of the access token.这里发生的是我收到错误 400 而不是访问令牌。 Originally the 'grant_type' was set as client_credientals but I realized that this only grabs the identity of the application owner itself, not others.最初,“grant_type”被设置为 client_credientals,但我意识到这只会获取应用程序所有者本身的身份,而不是其他人。 This worked however.然而,这奏效了。 Changing it to authorization_code however does not.但是,将其更改为 authorization_code 不会。

Any suggestions?有什么建议么?

Compared to the token exchange example , you are passing scope in the request – that shouldn't be there.令牌交换示例相比,您在请求中传递了scope ——这不应该存在。 Scope is passed only in the initial authorization URL.范围仅在初始授权 URL 中传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到此 Facebook Passport oauth2 响应错误? - Why am I getting this Facebook Passport oauth2 response error? 如何从 JSON 数据中获取 access_token - How can I get access_token from JSON data 如何在资源服务器中验证有关oauth2的access_token - how to verify access_token in resource server about oauth2 使用oauth2 access_token做什么 - What to do with oauth2 access_token 如何访问谷歌 Oauth2 护照中的个人资料信息? - How can I access profile information in google Oauth2 passport? 当我尝试更改我的 Discord 头像时,为什么会收到“400:错误请求”? - Why am I getting a '400: Bad Request' when i try to change my Discord avatar? 为什么我的 OAuth2 身份验证在 heroku 上出现 redirect_uri_mismatch 错误? - Why am I getting redirect_uri_mismatch error on heroku with my OAuth2 authentication? 如何安全地存储 Discord(OAuth2) 用户的访问令牌? - How to securely store the Access-Token of a Discord(OAuth2) User? 如何将Google OAuth2 ID令牌与node.js / passport.js结合使用,以验证用户是否有效并经过身份验证? - How can I use the Google OAuth2 ID token with node.js / passport.js to verify if the user is valid and authenticated? 我可以使用jwcrypto验证Google生成的OAuth2 id_token吗? - Can I use jwcrypto to validate a Google generated OAuth2 id_token?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM