我正在创建一个网站,用户可以使用他们的Discord帐户登录。
我正在做一个API调用,看起来像这样:
https://discordapp.com/api/oauth2/token?grant_type=authorization_code&client_id=791748434310201344&client_secret=superSecretID&redirect_uri=mywebsite%2Fprotocols%2Fadd_discord.php&code=KI5LYgKj7QuO3oBkddXQW6SnJbg17K&scope=identify
我期待着这样的回报:
{
"access_token": "6qrZcUqja7812RVdnEKjpzOL4CvHBFG",
"token_type": "Bearer",
"expires_in": 604800,
"scope": "identify"
}
但相反,我得到了:
{"message": "405: Method Not Allowed", "code": 0}
我看Discord oauth2文档已经有一段时间了,但我搞不清楚。
请确保您正在发出POST
请求,不允许使用其他方法(如GET
(。
如果你运行下面的代码片段,你可以看到使用GET
,你会收到一个";方法不允许";错误,而对于POST
,它只是抱怨无效的client_id
:
fetch('https://discordapp.com/api/oauth2/token?grant_type=authorization_code&client_id=CLIENTID&client_secret=superSecretID&redirect_uri=http%3A%2F%2Flocalhost%2Fadd_discord.php&code=xxx&scope=identify', {
method: 'GET'
})
.then(res => res.json())
.then(res => console.log({
method: 'GET',
res
}))
fetch('https://discordapp.com/api/oauth2/token?grant_type=authorization_code&client_id=CLIENTID&client_secret=superSecretID&redirect_uri=http%3A%2F%2Flocalhost%2Fadd_discord.php&code=xxx&scope=identify', {
method: 'POST'
})
.then(res => res.json())
.then(res => console.log({
method: 'POST',
res
}))