Oauth with Twitter Streaming API in R (using RCurl)

ぃ、小莉子 提交于 2019-11-27 01:43:06

问题


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")

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.

Thanks!


回答1:


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)

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

HTH



来源:https://stackoverflow.com/questions/15058485/oauth-with-twitter-streaming-api-in-r-using-rcurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!