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

How to revoke a token in Discord OAuth2.0?

为了使用 Discord 的 API 我需要一个令牌,为了得到它我打开了一个 link 例如 https://discordapp.com/api/oauth2/authorize?client_id=<client_id>&redirect_uri=<redirect_url>&response_type=token&scope=identify

然后我将令牌设置为 authorization(格式 Bearer <token>)header 发出到 Discord 的 API.

的请求

假设我想 "logout",这样就不能再使用某个令牌来执行此类请求。在这种情况下,我必须撤销该令牌,对吗?

所以在阅读了 Discord 的文档并做了一些调整后,我决定我必须向 URL 发出 POST 请求,例如 https://discordapp.com/api/oauth2/token/revoke, content-type header 应该设置为 x-www-form-urlencoded.

当我这样做时,我从 discord 的服务器收到一条错误消息,消息内容为 {error: "invalid_client"}

我做错了什么?

所以问题出在我发送的数据的实际格式上。我正在发送 JSON 数据,因为我认为设置特定的 headers 会自动将数据转换为正确的格式,但事实证明我不得不使用 FormData object 来以正确的格式创建数据,然后我还删除了我明确设置 header 的行,在这些步骤之后一切正常。

如果您遇到这个问题并且想知道撤销令牌的完整 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>