【问题标题】:Reddit gives 403 when upvoting via APIReddit 通过 API 投票时给出 403
【发布时间】: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_token3600 秒有效期。

'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 中的答案同时切换bearerBearer,并尝试按照403 error when trying to get data from Reddit API 中的建议使用不同的User-Agent。似乎没有任何效果。

我错过了什么?

【问题讨论】:

  • 当您错误地访问端点或者您没有完成请求所需的所有请求数据时,通常会收到 403 禁止返回。如果这是一个身份验证问题,您很可能会收到未经授权的响应。你在使用 request NPM 包吗?您确定您正在正确构建请求吗,几乎好像您缺少请求的正文。
  • @EMcG 是的。我正在使用request npm。另外,由于'Content-Type': 'application/x-www-form-urlencoded' 标头,我正在使用表单。更新了问题以反映这一点

标签: node.js oauth-2.0 reddit npm-request reddit-access-token


【解决方案1】:

解决了。我需要使用https://oauth.reddit.com 而不是www.reddit.com.

您现在可以代表该用户向 reddit 的服务器发出 API 请求,方法是在您的 HTTP 请求中包含以下标头:

Authorization: bearer TOKEN

带有不记名令牌的 API 请求应发送至 https://oauth.reddit.com,而不是 www.reddit.com。

【讨论】:

  • T_T 我想我不应该浏览文档。
猜你喜欢
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2014-12-02
  • 2011-02-26
  • 2010-10-04
  • 2017-06-19
相关资源
最近更新 更多