R 中带有 Twitter Streaming API 的 Oauth(使用 RCurl) [英] Oauth with Twitter Streaming API in R (using RCurl)

查看:20
本文介绍了R 中带有 Twitter Streaming API 的 Oauth(使用 RCurl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中使用 RCurl 连接到 Twitter 的 Streaming API,并且还能够过滤关键字.但是,Twitter API v1.1 中对授权的新限制使得使用 RCurl 变得困难.

I would like to connect to Twitter's Streaming API using RCurl in R, and also be able to filter keywords. However, new restrictions on authorization in Twitter API v1.1 is making using RCurl difficult.

以前,代码可以从此页面中获取:

Before, code could go something like this taken from this page:

 getURL("https://stream.twitter.com/1/statuses/filter.json", 
   userpwd="Username:Password",
   cainfo = "cacert.pem",
   write=my.function,
   postfields="track=bruins")

但现在,Twitter 的新 API 正在让用户使用 OAuth 进行授权.我有一个令牌和秘密,我只需要把它放在这个代码中进行授权.

But now, Twitter's new API is making users authorize with OAuth. I have a token and secret, I just need to place it in this code for authorization.

谢谢!

推荐答案

您可以使用 pacakge ROAuth 来实现.我假设您已经在 Twitter 上注册了您的应用程序并拥有一个 API 密钥.我从 Stack Overflow 上的其他问题拼凑起来(该问题和相关答案还包含一些指向其他相关问题的链接)和ROAuth 包的文档和 twitteR.>

You can do it with pacakge ROAuth. I assume you have registered your app with Twitter and have an API key. I pieced this together from other questions on Stack Overflow (that question and associated answers also contains some links to other contributing questions) and the documentation for package ROAuth and twitteR.

library(RCurl)
library(twitteR)
library(ROAuth)

requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL = "http://api.twitter.com/oauth/access_token"
authURL = "http://api.twitter.com/oauth/authorize"
consumerKey = "myconsumerkeystring"
consumerSecret = "myconsumersecretstring"
Cred <- OAuthFactory$new(consumerKey=consumerKey,
                             consumerSecret=consumerSecret,
                             requestURL=requestURL,
                             accessURL=accessURL, 
                             authURL=authURL)
    #The next command provides a URL which you will need to copy and paste into your favourite browser
    #Assuming you are logged into Twitter you will then be provided a PIN number to type into the R command line
Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
    # Checks that you are authorised
registerTwitterOAuth(Cred)

我相信流 API 的使用由包 处理streamR

I believe that use of the streaming API is handled by the package streamR

HTH

这篇关于R 中带有 Twitter Streaming API 的 Oauth(使用 RCurl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆