【问题标题】:Oauth 2 returns invalid clientOauth 2 返回无效客户端
【发布时间】:2021-02-18 22:06:55
【问题描述】:

我尝试使用 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很陌生。

【问题讨论】:

  • 您是否通过邮递员和代码来处理您的请求?将两者都粘贴在这里
  • 您是否尝试从其他服务访问服务?

标签: node.js oauth-2.0 discord token


【解决方案1】:

好的,我可以找到我的问题: 我将客户端 ID 保存为一个 Int 值,它全是数字。一旦我在客户端 ID 周围加上 '',它就起作用了。不知道这是一回事。

【讨论】:

    【解决方案2】:

    这对我有用,尝试使用正确的标题设置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
      })
    

    【讨论】:

    • 如我之前的评论中所述,能否显示通过邮递员和代码发送的请求?
    • 我做到了,你可以在我的 Post 中看到我的 Postman 设置的图像和我对 JS 的请求。还是您在搜索其他内容?
    • 我的意思是实际的网络调用快照或捕获。您上面显示的是请求代码。当该请求由 axios 发送时,它看起来像什么。在邮递员中,右手边有 code 按钮。复制粘贴
    • 我还用正确的grant_type 更新了代码。请检查并替换为您自己的值
    • 邮递员生成的代码:var request = require("request"); var options = { method: 'POST', url: 'discord.com/api/oauth2/token', headers: { 'content-type': 'application/x-www-form-urlencoded' }, form: { client_id: 'CLIENT ID', client_secret:'CLIENT SECRET',代码:'CODE',grant_type:'authorization_code',redirect_uri:'localhost:5000'}}; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); });
    猜你喜欢
    • 2023-03-25
    • 2020-12-04
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多