【问题标题】:Always get 401 error from reddit API using oauth_token2.0 from httr package使用 httr 包中的 oauth_token2.0 总是从 reddit API 得到 401 错误
【发布时间】:2018-02-20 05:20:11
【问题描述】:

正如标题所说,我正在尝试连接到 reddit API,我在我的个人资料上创建了一个应用程序(名为评论提取器),并复制粘贴公钥和密钥,并使用 http://localhost:1410/ 作为重定向URI 和关于 URL。该应用程序是一个脚本,但我也尝试了 Web 应用程序,结果相同。

我使用的代码只是从 Hadleys httr 演示中复制粘贴,但我用自己的密钥交换了密钥(一切都使用最新版本的 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")
)

网络浏览器打开,我被要求允许或拒绝令牌,一切似乎都很好,但总是失败并显示此消息

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.

我不确定如何处理用户代理,我注意到该应用程序要求提供开发人员姓名,所以我尝试了一些随机文本并在那里使用我的 reddit 用户名,无论哪种方式,我总是收到 401 错误,这似乎意味着错误的键,但它们肯定不是。

任何帮助将不胜感激,当我在最基本的步骤上停下来不知道下一步该怎么做时,我有点不知所措。

【问题讨论】:

    标签: r oauth-2.0 reddit httr


    【解决方案1】:

    我也遇到了同样的问题! 我还没有找到解决方案,但我想我已经找到了问题所在。

    错误发生在 oauth2.0_access_token 函数中,而不是 oauth2.0_token 函数中。 在 oauth2.0_token 函数中,我们将“use_basic_auth”设置为 TRUE。但是在 oauth2.0_acces_token 函数中,“use_basic_auth”的默认设置为 FALSE。

    由于令牌函数似乎调用了acces_token函数,我不知道如何更改该默认值......但我希望我的见解能让我们更进一步(如果你找到了最终的解决方案,请分享这个和我一起?)

    【讨论】:

    • 如果我弄明白了会在这里更新,我已经用尽了我能想到的东西,我已经在 httr 包 github 上打开了一个问题,希望 Hadley 或其他人有想法需要做什么(或我做错了什么)。
    【解决方案2】:

    我找到了解决我们问题的方法! httr github 页面的拉取请求#485 通过使 init.oauth2.0() 通过 use_basic_auth 解决了我们的问题。 因此,您可以通过添加以下行来安装他的 httr 版本:

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

    这是对我有用的完整代码(不要忘记定义密钥和秘密):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多