2016-09-17 83 views
2

你能幫我從twitter獲取數據嗎?研究了很多不能解決這個問題。從twitter獲取數據在r

library(twitteR) 
library(ROAuth) 
library(RCurl) 
library(pacman) 
library(devtools) 
library(rjson) 
library(httr) 

    consumerKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
    consumerSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
    access_token <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
    access_secret <- "Xxxxxxxxxxxxxxxxxxxxxxxxxxx" 



if (!require('pacman')) install.packages('pacman') 
pacman::p_load(twitteR, ROAuth, RCurl) 

options(RCurlOptions = list(cainfo = system.file('CurlSSL', 'cacert.pem', package = 'RCurl'))) 

reqURL = 'https://api.twitter.com/oauth/request_token' 
accessURL = 'https://api.twitter.com/oauth/access_token' 
authURL = 'https://api.twitter.com/oauth/authorize' 

twitCred = OAuthFactory$new(consumerKey = consumerKey, consumerSecret = consumerSecret, requestURL = reqURL, accessURL = accessURL, authURL = authURL) 
twitCred$handshake(cainfo = system.file('CurlSSL', 'cacert.pem', package = 'RCurl')) 

它指引我授權和代碼,然後按I鍵進入

下一步

setup_twitter_oauth(access_token,access_secret) 

我得到這個錯誤。如何解決這個問題。

[1] 「使用基於瀏覽器的認證」

Error in init_oauth1.0(self$endpoint, self$app, permission =self$params$permission, : Unauthorized (HTTP 401) 

回答

1

你已經設定了在Twitter(https://apps.twitter.com/)的應用程序?這是我得到了它的工作,你做的授權有這給相關按鍵,然後與Twitter這是因爲

setup_twitter_oauth(consumer_key = consumer_key, 
       consumer_secret = consumer_secret, 
       access_token = access_token, 
       access_secret = access_secret) 

由於操作簡單,然後檢索時間表:

userTimeline(user = "@BBCBreaking") 
+0

我已經安裝了應用程序已經發布,但我認爲我能夠生成一些推文。謝謝!! – Ashish