簡體   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?

我無法通過不和諧 oauth2 獲取客戶身份。 首先我們這樣做:

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

獲取他們的代碼。 這似乎工作正常。

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);
    });

這里發生的是我收到錯誤 400 而不是訪問令牌。 最初,“grant_type”被設置為 client_credientals,但我意識到這只會獲取應用程序所有者本身的身份,而不是其他人。 然而,這奏效了。 但是,將其更改為 authorization_code 不會。

有什么建議么?

令牌交換示例相比,您在請求中傳遞了scope ——這不應該存在。 范圍僅在初始授權 URL 中傳遞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM