node.jsoauth-2.0redditnpm-requestreddit-access-token

Reddit gives 403 when upvoting via API


I've registered as the Web app as required by the Reddit API for the Oauth access with identity, edit, flair, history, modconfig, modflair, modlog, modposts, modwiki, mysubreddits, privatemessages, read, report, save, submit, subscribe, vote, wikiedit, wikiread scopes.

I'd authorized my app and have exchanged the generated code for the access_token with 3600 seconds validity.

'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);
});

But when I try to upvote a reddit submission using API, I get an error.

{"message": "Forbidden", "error": 403}

The link that I'm trying to upvote is Tim Cook warns of ‘data-industrial complex’ in call for comprehensive US privacy laws

I tried switching both bearer and Bearer as per the answer in Reddit API returns HTTP 403, and tried using different User-Agent as suggested in 403 error when trying to get data from Reddit API. Nothing seem to work.

What am I missing?


Solution

  • Solved. I need to use https://oauth.reddit.com instead of www.reddit.com.

    You may now make API requests to reddit's servers on behalf of that user, by including the following header in your HTTP requests:

    Authorization: bearer TOKEN
    

    API requests with a bearer token should be made to https://oauth.reddit.com, NOT www.reddit.com.