【发布时间】:2019-03-30 00:05:35
【问题描述】:
我已按照Reddit API 的要求注册为Web app,以使用identity, edit, flair, history, modconfig, modflair, modlog, modposts, modwiki, mysubreddits, privatemessages, read, report, save, submit, subscribe, vote, wikiedit, wikiread 范围进行Oauth 访问。
我已授权我的应用程序并已将生成的代码交换为 access_token 和 3600 秒有效期。
'use strict';
let request = require('request');
const USER_AGENT = 'web:com.example.server:v0.0.1 (by /u/sridharrajs)';
const token = '<my access_token within 3600 seconds validity>';
request({
method: 'POST',
url: 'https://www.reddit.com/api/vote',
headers: {
'User-Agent': USER_AGENT,
'Authorization': `bearer ${token}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
id: "t1_9qy47p",
dir: "1"
},
json: false
}, function (error, response, body) {
if (error) {
console.log('error', error);
} else if (body.error) {
console.log('body.error', body);
}
return console.log(body);
});
但是当我尝试upvote a reddit submission using API 时,我得到一个错误。
{“消息”:“禁止”,“错误”:403}
我要投票的链接是Tim Cook warns of ‘data-industrial complex’ in call for comprehensive US privacy laws
我尝试根据Reddit API returns HTTP 403 中的答案同时切换bearer 和Bearer,并尝试按照403 error when trying to get data from Reddit API 中的建议使用不同的User-Agent。似乎没有任何效果。
我错过了什么?
【问题讨论】:
-
当您错误地访问端点或者您没有完成请求所需的所有请求数据时,通常会收到 403 禁止返回。如果这是一个身份验证问题,您很可能会收到未经授权的响应。你在使用 request NPM 包吗?您确定您正在正确构建请求吗,几乎好像您缺少请求的正文。
-
@EMcG 是的。我正在使用
requestnpm。另外,由于'Content-Type': 'application/x-www-form-urlencoded'标头,我正在使用表单。更新了问题以反映这一点
标签: node.js oauth-2.0 reddit npm-request reddit-access-token