Always get 401 error from reddit API using oauth_token2.0 from httr package

1.2k Views Asked by At

As the title says, i'm trying to connect to the reddit API, i've created an app (named comment extractor) on my profile, and copy paste the public and secret keys, and use http://localhost:1410/ as redirect URI and about URL. The app is a script, but i tried web app as well with the same result.

The code i'm using is just copy pasted from Hadleys httr demos, but i exchanged the keys for my own (everything done with the latest version of httr, 1.3.1).

library(httr)

# 1. Find OAuth settings for reddit:
#    https://github.com/reddit/reddit/wiki/OAuth2
reddit <- oauth_endpoint(
  authorize = "https://www.reddit.com/api/v1/authorize",
  access =    "https://www.reddit.com/api/v1/access_token"
)

# 2. Register an application at https://www.reddit.com/prefs/apps
app <- oauth_app("comment extractor", "rrG5wfgHkm5Kvw", "[secret key]")


# 3b. If get 429 too many requests, the default user_agent is overloaded.
# If you have an application on Reddit then you can pass that using:
token <- oauth2.0_token(
  reddit, app,
  scope = c("read", "modposts"),
  use_basic_auth = TRUE,
  config_init = user_agent("reddit_username")
)

The web browser opens, i'm asked to allow or deny the token, and everything seems fine, but it always fails with this message

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = 
user_params,  : 
  Unauthorized (HTTP 401). Failed to get an access token.

I was unsure of what to do with the user agent, i noticed the app asks for developer names so i tried both some random text and using my reddit username there, either way, i always get a 401 error, which seems to mean wrong keys, but they definately aren't.

Any help would be greatly appreciated, i'm somewhat at a loss when i get stopped at the most basic step and don't know what to do next.

2

There are 2 best solutions below

1
On

I'm having the exact same problem! I have not found a solution yet, but I think I have found where the problem may lie.

The error occurs in the oauth2.0_access_token function and not in the oauth2.0_token function. In the oauth2.0_token function we have set the "use_basic_auth" to TRUE. BUT in the oauth2.0_acces_token function the default for "use_basic_auth" is set to FALSE.

Since the token function seems to call the acces_token function, I have no idea how to change that default... but I hope my insight will bring us a step closer (If you have found the final solution, would you please share this with me?)

0
On

I have found a solution to our problem! Pull request #485 of the httr github page has solved our problem by making init.oauth2.0() pass use_basic_auth. So you can install his httr version by adding following lines:

#install.packages("devtools")  
library(devtools)  
devtools::install_github("r-lib/httr#485")
library(httr)  

Here is my full code that worked for me (don't foret to define key and secret):

#install.packages("httr")
#install.packages("devtools")
library(devtools)
devtools::install_github("r-lib/httr#485")
library(httr)

# 1. Find OAuth settings for reddit:
#    https://github.com/reddit/reddit/wiki/OAuth2
endpoint <- oauth_endpoint(
  authorize = "https://www.reddit.com/api/v1/authorize",
  access =    "https://www.reddit.com/api/v1/access_token"
)

# 2. Register an application at https://www.reddit.com/prefs/apps
app <- oauth_app("Test_App", key, secret)

# 3b. If get 429 too many requests, the default user_agent is overloaded.
# If you have an application on Reddit then you can pass that using:
token <- oauth2.0_token(
  endpoint = endpoint, 
  app=app,
  scope = c("read", "modposts"),
  use_basic_auth = TRUE,
#  cache = F,
  config_init = user_agent("Testing Oauth with httr")
)

#trying to make a call
#Important! Make sure to put oauth.reddit.com instad of reddit.com!
request_url <- "https://oauth.reddit.com/r/AskReddit/comments/7az0np/what_is_the_most_pointless_piece_of_information/.json"
response <- GET(request_url,
                user_agent("Testing Oauth with httr"),
                config(token = token)
                )

content(response, "text")