javascript - 如何修复对 Reddit API 的 nodejs 请求中的 unsupported_grant_type 错误
问题描述
我在向 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);
});
解决方案
我确实以这种方式解决了这个问题。
我将标题更改 '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';
推荐阅读
- mysql-workbench - MYSQL Workbench 主键未更新
- php - PHP - 通过 OVPN 连接时无法使套接字正常工作
- excel - 从用户窗体数据填充单元格。根据 `sub`,目标单元格会发生变化
- javascript - 如何在 vuejs 和 laravel 中隐藏按钮
- python - Sys.path.insert 插入模块的路径,但导入不起作用
- html - 如何更改动画以使箭头向左?
- python - 模拟python类属性的指针行为
- java - 隐式扩展对象
- java - 如何在 Mac 上使用 Jenkins 运行 selenium 测试用例?
- java - 混合字符串中的字符(更改二进制结果的顺序)