使用httr、twitteR和streamR包检索缓存的OAuth令牌

5
我发现与Twitter API进行身份验证的唯一方法如下所示:
library(twitteR)

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

运行后,我可以正常使用twitteR中的所有功能。但是,我还想使用streamR包,该包需要token作为OAuth对象:
filterStream("tweets.json", track = c("Obama", "Biden"), timeout = 20, oauth=my_oauth)

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

2个回答

5

使用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 Twitter 密钥:xxxxxxxxxx0xxxxxxxxxxxxxx 密码: oauth_token,oauth_token_secret,user_id,screen_name

通过$long-alphanumeric-hash访问列表中的环境,在其中访问$credentials$oauth_token/$oauth_token_secret


此外,这里有一个关于 httr 如何缓存令牌 的很棒的解释。 - akhmed

0

这是一个技巧,不能直接从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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接