Ashish Ashish - 2 months ago 25
R Question

Fetch data from twitter in r

Can you help me in fetching data from twitter its been days. Researched a lot cant solve this.

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


It directs me to authorize and I key in the code and press enter

The next step

setup_twitter_oauth(access_token,access_secret)


I get this error. How to solve this.

[1] "Using browser based authentication"

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

Answer

Have you set up an app on Twitter (https://apps.twitter.com/)? This is how I got it working, you do the authorisation there which gives the relevant keys and then with twitteR it's as simple as

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

And then to retrieve a timeline:

userTimeline(user = "@BBCBreaking")
Comments