【问题标题】:Retrieving cached oauth token with packages httr, twitteR, and streamR使用包 httr、twitteR 和 streamR 检索缓存的 oauth 令牌
【发布时间】:2015-02-14 14:17:40
【问题描述】:

我发现让自己通过 twitter API 进行身份验证的唯一方法是:

library(twitteR)

setup_twitter_oauth(consumer_key = "a", 
                consumer_secret = "b", 
                access_token = "c", 
                access_secret = "d")

运行后,我可以正常使用 twitteR 中的所有功能。但是,我也想使用 streamR 包,它需要令牌作为 OAuth 对象:

filterStream("tweets.json", track = c("Obama", "Biden"), timeout = 20, oauth=my_oauth)

据我所知,上面的 setup_twitter_oauth 函数是一些 httr 函数的包装器,用于获取我的授权令牌。此令牌作为名为“.httr-oauth”的文件缓存在我的工作目录中。我的问题是:如何将此文件加载到 R 中,以便获得可以与 streamR 一起使用的 OAuth 对象?

【问题讨论】:

    标签: r twitter oauth httr


    【解决方案1】:

    使用readRDS()

    readRDS('.httr-oauth') $xxxx0x000xxxx00000x0xx0x000000xx 请求:https://api.twitter.com/oauth/request_token 授权:https://api.twitter.com/oauth/authenticate 访问:https://api.twitter.com/oauth/access_token 推特 键:xxxxxxxxx0xxxxxxxxxxxxxxx 秘密: oauth_token、oauth_token_secret、user_id、screen_name

    通过$long-alphanumeric-hash访问列表中的环境,并在该访问范围内访问$credentials和$oauth_token/$oauth_token_secret

    【讨论】:

    【解决方案2】:

    这是一个 hack,不会直接从 setup_twitter_oauth 检索 OAuth 对象,但它可以工作(改编自 http://www.datablog.sytpp.net/2014/04/scraping-twitter-with-r-a-how-to/)。

    在设置好 consumer_key 和 consumer_secret 后执行以下操作

        twitCred <- OAuthFactory$new(consumerKey=consumer_key,
        consumerSecret=consumer_secret,
        requestURL="https://api.twitter.com/oauth/request_token",
        accessURL="https://api.twitter.com/oauth/access_token",
        authURL="http://api.twitter.com/oauth/authorize")
        twitCred$handshake()
        save(twitCred, file="credentials.RData")
    

    混合 TwitteR 和 streamR 时,使用 twitCred 作为 streamR 调用的 OAuth

        twitCred<- NULL
        load("credentials.RData")
    

    用于检索与足球相关的推文的示例测试 streamR 调用

        foo<- filterStream(file.name="",track =c("Football","NFL"),oauth=twitCred,timeout=30)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 2013-01-30
      • 2015-05-28
      • 2014-02-26
      • 2011-09-05
      • 2013-08-11
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多