How to authenticate with reddit using RCurl

I am trying to authenticate using Reddit from R using RCurl based on this example from Reddit github :

 curl -X POST -d 'grant_type=password&username=reddit_bot&password=snoo' --user   'p-jcoLKBynTLew:gko_LXELoV07ZBNUXrvWZfzE3aI' https://ssl.reddit.com/api/v1/access_token

I tried converting it to an RCurl command as follows:

postForm("https://ssl.reddit.com/api/v1/access_token?grant_type=password",
     username = "MyUserName",
     password = "MyPassword",
     .opts = list(userpwd = "MyClientid:MySecret")
     )

But I get an error message: Error: Unauthorized

I'm not sure what I'm really doing with the conversion of the curl command to Rcurl. Thanks for any help you could provide!

+4
source share
1 answer

Try this httr code:

library(httr)

POST("https://ssl.reddit.com/api/v1/access_token",
  body = list(
    grant_type = "password",
    username = "MyUserName",
    password = "MyPassword"
  ),
  encode = "form",
  authenticate("p-jcoLKBynTLew", "gko_LXELoV07ZBNUXrvWZfzE3aI")
)
+3
source

All Articles