简体   繁体   English

如何撤销 Discord OAuth2.0 中的令牌?

[英]How to revoke a token in Discord OAuth2.0?

In order to use Discord's API I need a token, and to get it I open a link such as https://discordapp.com/api/oauth2/authorize?client_id=<client_id>&redirect_uri=<redirect_url>&response_type=token&scope=identify为了使用 Discord 的 API,我需要一个令牌,并打开一个链接,例如https://discordapp.com/api/oauth2/authorize?client_id=<client_id>&redirect_uri=<redirect_url>&response_type=token&scope=identify

Then I set the token as authorization (in format Bearer <token> ) header of requests that are issued to the Discord's API.然后我将令牌设置为向 Discord 的 API 发出的请求的authorization (格式为Bearer <token> )header。

Let's say I want to "logout", so that a certain token can't be used anymore to do such requests.假设我想“注销”,这样就不能再使用某个令牌来执行此类请求。 In this case I have to revoke that token, right?在这种情况下,我必须撤销该令牌,对吗?

So after reading Discord's documentation and making some adjustments I decided that I have to make a POST request to a URL such as https://discordapp.com/api/oauth2/token/revoke , and content-type header of this request should be set to x-www-form-urlencoded . So after reading Discord's documentation and making some adjustments I decided that I have to make a POST request to a URL such as https://discordapp.com/api/oauth2/token/revoke , and content-type header of this request should be设置为x-www-form-urlencoded

When I do it I'm getting an error message from discord's server with message saying {error: "invalid_client"}当我这样做时,我从不和谐的服务器收到一条错误消息,其中显示{error: "invalid_client"}

What do I do wrong?我做错了什么?

So the problem was in actual format of the data I was sending.所以问题出在我发送的数据的实际格式上。 I was sending JSON data because I thought that setting specific headers would automatically turn the data into the right format, but it turns out I had to use FormData object to create the data in the right format, and after that I also removed the lines where I'm setting the header explicitly, after these steps everything worked fine.我正在发送 JSON 数据,因为我认为设置特定的标头会自动将数据转换为正确的格式,但事实证明我必须使用FormData object 以正确的格式创建数据,之后我还删除了我正在明确设置 header,在这些步骤之后一切正常。

If you come by this question and are wondering what is the full API call to revoke the token, here it is:如果您遇到这个问题并且想知道撤销令牌的完整 API 调用是什么,这里是:

POST https://discord.com/api/oauth2/token/revoke
Content-Type: application/x-www-form-urlencoded
data:
  client_id: <client_id>
  client_secret: <client_secret>
  token: <access_token>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM