如何使用RCurl使用reddit进行身份验证

时间:2014-10-05 21:48:45

标签: r curl oauth reddit rcurl

我一直在尝试使用来自R的Reddit根据来自Reddit's github的示例使用RCurl进行身份验证:

 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

我尝试将其转换为RCurl命令,如下所示:

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

但我收到错误:Error: Unauthorized

我不确定将curl命令转换为Rcurl时我到底在做什么。感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:3)

试试这个httr代码:

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")
)