Oauth 2 returns 无效的客户端
Oauth 2 returns invalid client
我尝试使用 Discord Api 实现 Oauth 2.0。我知道有一些包可以为我实现这个,但我想自己做。我有以下代码(节点 JS)
axios
.post(
"https://discordapp.com/api/oauth2/token",
"client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=client_credentials&code=" + code + "&redirect_uri=http://localhost:5000"
这总是会导致错误“无效的客户端”。
但是使用邮递员
这有效!
所以我不知道,我的 js 做错了什么......
感谢您的帮助。
顺便说一下:我是 nodeJs 的新手。
这对我有用,请尝试使用适当的设置 header application/x-www-form-urlencoded
const axios = require('axios')
const qs = require('querystring')
const requestBody = {
client_id: 'clientId',
client_secret: 'CLIENT SEVRET',
grant_type: 'authorization_code',
code: 'code',
redirect_uri: 'http://locahost:5000'
}
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
axios.post("https://discordapp.com/api/oauth2/token", qs.stringify(requestBody), config)
.then((result) => {
// Do something
})
.catch((err) => {
// Do something
})
好的,我可以找到我的问题:
我将客户 ID 保存为一个 Int 值,它全是数字。一旦我将 '' 放在客户端 ID 周围,它就起作用了。不知道这是一回事。
我尝试使用 Discord Api 实现 Oauth 2.0。我知道有一些包可以为我实现这个,但我想自己做。我有以下代码(节点 JS)
axios
.post(
"https://discordapp.com/api/oauth2/token",
"client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=client_credentials&code=" + code + "&redirect_uri=http://localhost:5000"
这总是会导致错误“无效的客户端”。
但是使用邮递员
这有效! 所以我不知道,我的 js 做错了什么......
感谢您的帮助。
顺便说一下:我是 nodeJs 的新手。
这对我有用,请尝试使用适当的设置 header application/x-www-form-urlencoded
const axios = require('axios')
const qs = require('querystring')
const requestBody = {
client_id: 'clientId',
client_secret: 'CLIENT SEVRET',
grant_type: 'authorization_code',
code: 'code',
redirect_uri: 'http://locahost:5000'
}
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
axios.post("https://discordapp.com/api/oauth2/token", qs.stringify(requestBody), config)
.then((result) => {
// Do something
})
.catch((err) => {
// Do something
})
好的,我可以找到我的问题: 我将客户 ID 保存为一个 Int 值,它全是数字。一旦我将 '' 放在客户端 ID 周围,它就起作用了。不知道这是一回事。