【问题标题】:How to fix unsupported_grant_type error in nodejs request to Reddit API如何修复对 Reddit API 的 nodejs 请求中的 unsupported_grant_type 错误
【发布时间】:2019-08-17 09:28:08
【问题描述】:

我在向 Reddit 发出请求以获取访问令牌时收到 unsupported_grant_type 错误。我正在尝试制作一个在没有用户上下文的情况下发出 API 请求的应用程序。

我试过移动参数和标题,但无济于事。

这是我目前使用的代码(请注意,我的用户名 = '我的客户 ID' 和密码 = '我的密钥'

var request = require("request");

var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');

var options = { method: 'POST',
  url: 'https://www.reddit.com/api/v1/access_token',
  headers:
   { 'Content-Type': 'application/x-www-form-urlencoded',
   'Authorization': auth,
   },
  body: 
   JSON.stringify({grant_type: 'client_credentials',
     user: username,
     password: password})
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(response,body);
});

【问题讨论】:

    标签: javascript node.js request reddit


    【解决方案1】:

    我确实以这种方式解决了这个问题。

    我将标题 'Content-Type': 'application/x-www-form-urlencoded' 更改为 Accept: 'application/json'。 正文不会使其成为 JSON 对象,我将其作为字符串发送。 options.body = 'grant_type=client_credentials'

        const options = {};
            options.method = 'POST';
            options.headers = new Headers({
                'Accept-Language': 'en_US',
                Accept: 'application/json',
                Authorization: auth,
            });
        options.body = 'grant_type=client_credentials';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多