您好,所以我设置了一个不一致的应用程序并进行了OAUTH登录。我需要获取用户ID,但是无论尝试如何,都存在问题。我读过this,但对我来说没有任何意义。如何知道它是什么用户。 它显示“ 401:未经授权”,我应该怎么做才能授权。 我也有访问令牌;刷新令牌 谢谢
答案 0 :(得分:2)
I had the same problem and just figured out the solution!
Once you successfully receive the access token, you will need to perform a git request to the following endpoint:
https://discordapp.com/api/users/@me
with the headers set to:
Authorization: Bearer ${ACCESS_TOKEN}
You should then receive the following response:
data: {
avatar: ....,
discrimnator: ....,
email: ....,
flags: ....,
id: ....
}
You'll also get some other data points. But those are the big ones. Note: You will only receive the email field if you included that in scope when you received your access token. You can use the id received from this call to hit the users/:userId endpoint referenced in Discord's documentation.
Here is my actual code in case you're interested:
fetchUser: (data) =>
axios
.get(
`https://discordapp.com/api/users/@me`,
{headers: {Authorization: `Bearer ${data.access_token}`}}
)
Hope that helps!