在r中从twitter获取数据

时间:2016-09-17 09:26:18

标签: r twitter

你可以帮助我从Twitter获取数据吗?研究很多都无法解决这个问题。

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

它指示我授权并输入代码并按Enter键

下一步

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 个答案:

答案 0 :(得分:1)

您是否在Twitter(https://apps.twitter.com/)上设置了应用?这就是我如何使它工作,你在那里进行授权,提供相关的密钥,然后使用twitteR它就像

一样简单
<div ng-controller="AppCtrl" ng-cloak="" ng-app="MyApp" layout-fill layout="column">
  <div style="background:blue" flex="40" layout="row" layout-align="end end">
    <md-button class="md-fab" aria-label="Eat cake" style="margin-bottom:-26px"">
      <md-icon md-svg-src="img/icons/cake.svg"></md-icon>
    </md-button>
  </div>
  <div style="background:green" flex>
  </div>
</div>

然后检索时间表:

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