【问题标题】:OAuth authentication with Twitter's API using R使用 R 使用 Twitter 的 API 进行 OAuth 身份验证
【发布时间】:2014-04-14 14:39:57
【问题描述】:

我正在探索 Twitter API。

我在 R 中使用 ROAuthtwitteR 包。

我认为事情进展顺利:

To enable the connection, please direct your web browser to: 
https://api.twitter.com/oauth/authorize?oauth_token=XXXXX
When complete, record the PIN given to you and provide it here: XX

到目前为止一切顺利。现在,我准备好查看一些 Twitter 时间线了:

> my_tweets <- userTimeline('someTimeline')

不幸的是,我得到:

Error in InterfaceObj$doAPICall(cmd, params, method, ...) : 
  OAuth authentication is required with Twitter's API v1.1

我一直在研究这意味着什么。我觉得我的 OAuth 身份验证是合适的。为什么我会收到此错误?

我使用的 API 版本有问题吗?

【问题讨论】:

    标签: r twitter oauth roauth


    【解决方案1】:

    您可以按照以下步骤操作:

    reqURL <- "https://api.twitter.com/oauth/request_token"
    accessURL <- "https://api.twitter.com/oauth/access_token"
    authURL <- "https://api.twitter.com/oauth/authorize"
    consumerKey <- "Mjn6tdsadsadkasdklad2SV1l"
    consumerSecret <- "58Z7Eldsdfaslkf;asldsaoeorjkfksaVCQtvri"
    twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                                 consumerSecret=consumerSecret,
                                 requestURL=reqURL,
                                 accessURL=accessURL,
                                 authURL=authURL)
    twitCred$handshake()
    

    运行此代码后,您将在 R 控制台中看到如下消息:

    To enable the connection, please direct your web browser to: 
    https://api.twitter.com/oauth/authorize?oauth_token=scmVODruosvz6Tdsdadadasdsa
    When complete, record the PIN given to you and provide it here:
    

    只需将链接粘贴到您的浏览器然后授权应用程序,最后一个您将获得 PIN 码,只需将 PIN 码复制并粘贴到您的 R 控制台。

    registerTwitterOAuth(twitCred)
    

    如果成功,R 控制台将显示 TRUE。

    user <- getUser("xxx")
    userTimeline(user, n=20, maxID=NULL, sinceID=NULL, includeRts=FALSE)
    

    如果仍然有任何问题,请尝试显示您的软件包版本并更新到最新版本

    sessionInfo()
    update.packages("twitteR")
    

    twitteR 的最新版本是 1.1.7 => http://cran.r-project.org/web/packages/twitteR/index.html

    【讨论】:

      【解决方案2】:

      我看到您已成功访问(又名握手)。我假设您的握手代码将类似于以下行。

      Cred$handshake(cainfo = system.file("CurlSSL", "your_certificate.pem", package = "RCurl") )
      

      那么我假设你使用以下行注册了 Twitter OAuth,你说它是成功的。

      registerTwitterOAuth(Cred)
      

      那么您的 userTimeline 应该包含您之前创建的 pem 文件。

      my_tweets <- userTimeline('someTimeline', cainfo="your_certificate.pem", n=200)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-16
        • 2012-11-27
        • 1970-01-01
        • 2012-10-09
        • 2010-11-01
        • 1970-01-01
        • 2013-11-29
        • 2020-03-13
        相关资源
        最近更新 更多