4

RのRCurlを使用してTwitterのストリーミング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")

しかし現在、Twitterの新しいAPIにより、ユーザーはOAuthで認証できるようになっています。私はトークンとシークレットを持っています。認証のためにこのコードに配置する必要があります。

ありがとう!

4

1 に答える 1

7

あなたは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)

ストリーミングAPIの使用はパッケージによって処理されると思いますstreamR

HTH

于 2013-02-25T07:54:39.703 に答える