R-E*_*ast 4 api streaming r keyword rcurl
我想使用R中的RCurl连接到Twitter的Streaming API,并且还能够过滤关键字.但是,Twitter API v1.1中对授权的新限制使得使用RCurl变得困难.
getURL("https://stream.twitter.com/1/statuses/filter.json",
userpwd="Username:Password",
cainfo = "cacert.pem",
write=my.function,
postfields="track=bruins")
Run Code Online (Sandbox Code Playgroud)
但现在,Twitter的新API正在让用户通过OAuth进行授权.我有一个令牌和秘密,我只需要将它放在此代码中进行授权.
谢谢!
你可以用pacakge做到这一点ROAuth.我假设您已经在Twitter上注册了您的应用并拥有API密钥.我从Stack Overflow上的其他问题(这个问题和相关答案也包含一些其他贡献问题的链接)以及包ROAuth和文件的文档中拼凑出来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)
Run Code Online (Sandbox Code Playgroud)
我相信流程API的使用由包处理 streamR
HTH