How to get twitteR PIN code during R oauth authentication? - r

I’m trying to use the twitteR package .
I use this code to register my credentials:
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL = "https://api.twitter.com/oauth/access_token"
authURL = "https://api.twitter.com/oauth/authorize"
consumerKey = "------------"
consumerSecret = "-----------"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=requestURL,
accessURL=accessURL,
authURL=authURL)
download.file(url="http://curl.haxx.se/ca/cacert.pem",
destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")
I then get the link to twitter:
To enable the connection, please direct your web browser to: http://api.twitter.com/oauth/authorize?oauth_token=xxxx
but it returns an "Error 403" page.
So I try to edit the URL to https:// instead of http://, I follow the link and within the twitter page,
and then click on the “Authorize app” button.This sends me back to the site I listed as the callback URL.
I don’t see the PIN number I was expecting anywhere.
Hoping somebody could help me.

It may be the https, see if this code works as it does for me:
### 1. Register your Twitter app here https://apps.twitter.com/app/new
### 2. Get your api_key and api_secret, fill in below
### 3. Source this file, copy the Twitter Authentication URL
### 4. Input the PIN every session you need access to Twitter API
require(twitteR)
api_key = "XXXXX"
api_secret = "XXXXX"
TwitterOAuth<-function(){
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=api_key,
consumerSecret=api_secret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
options(RCurlOptions = list(cainfo = system.file("CurlSSL",
"cacert.pem",
package = "RCurl")))
twitCred$handshake()
registerTwitterOAuth(twitCred)
}
TwitterOAuth()
I did a post on it here.

Related

Error: oauth_listener() needs an interactive environment in R

I am trying to a R script through command line which connects to twitter. When I run the code through R-Studio it is able to connect to twitter and get the tweets. However when I run the script through command line I get the following error:
Error: oauth_listener() needs an interactive environment.
To resolve this, I ran once through R-studio and saved the environment variables in the following manner:
if(file.exists("./token_file"))
{
load("./token_file")
}
else
{
token <- setup_twitter_oauth(consumerKey,consumerSecret,access_token=NULL, access_secret=NULL)
save(token,file="./token_file")
}
Now I get an error:
Error in get_oauth_sig() : OAuth has not been registered for this session
I tried saving the authentication session as well by:
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
if(file.exists("./auth_file"))
{
load("./auth_file")
}
else
{
my_oauth <- OAuthFactory$new(consumerKey = consumerKey, consumerSecret = consumerSecret,
requestURL = requestURL, accessURL = accessURL, authURL = authURL)
save(my_oauth,file ="./auth_file")
}
However I still get the above mentioned error. Is there a way to get R to connect to twitter through command line?
I am using twitteR package.
I search for stream in twitter by using the function
searchTwitter("love", n=10)
I believe you need to use a different authentication model than the interactive login. I'm not an expert on these things unfortunately but it seems that from the Twitter documentation you want "Application-only authentication".
https://dev.twitter.com/oauth/application-only

I've registered my oath about 5 times now, but... (twitteR package R)

I'm attempting to mine twitter data in R, and am having trouble getting started. I created a twitter account, an app in twitter developers, changed the settings to read, write, and access, created my access token, and followed instructions to the letter in registering it:
My code:
> library(twitteR)
> download.file(url="http://curl.haxx.se/ca/cacert.pem",
+ destfile="cacert.pem")
> requestURL <- "https://api.twitter.com/oauth/request_token"
> accessURL <- "https://api.twitter.com/oauth/access_token"
> authURL <- "https://api.twitter.com/oauth/authorize"
> consumerKey <-"my key" #took this part out for privacy's sake
> consumerSecret <- "my secret" #this too
> twitCred <- OAuthFactory$new(consumerKey=consumerKey, consumerSecret = consumerSecret, requestURL = requestURL, accessURL = accessURL, authURL = authURL)
> twitCred$handshake(cainfo="cacert.pem")
To enable the connection, please direct your web browser to:
https://api.twitter.com/oauth/authorize?oauth_token=zxgHXJkYAB3wQ2IVAeyJjeyid7WK6EGPfouGmlx1c
When complete, record the PIN given to you and provide it here: 0010819
> registerTwitterOAuth(twitCred)
[1] TRUE
> save(list="twitCred", file="twitteR_credentials")
And yet, this:
> s <- searchTwitter('#United', cainfo="cacert.pem")
[1] "Unauthorized"
Error in twInterfaceObj$doAPICall(cmd, params, "GET", ...) :
Error: Unauthorized
I'm about to have a temper tantrum. I'd be extremely grateful if someone could explain to me what is going wrong, or, better yet, how to fix it.
Thank you.

How to get the accounts listed in a twitter public list?

I'm looking for a solution about collecting in R all the accounts (users screennames) of a member's public list on Twitter.
The twitteR package doesn't help here (actually, I hope). It doesn't give any tool to explore twitter public lists (something I didn't know until today, but somehting I've learned on the dedicaced mailing list)
So I suppose I need to use something like RCurl. But I've no idea how. And it seems (for google) that nobody has never tryed it (since the API 1.1).
The list I try to get is (for example) : AssembleeNat/deputes
And I precise that I'm actually correctly authentificated in R with my own twitter account :
require(twitteR)
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- "xxx"
consumerSecret <- "xxx"
Cred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=requestURL,
accessURL=accessURL,
authURL=authURL)
Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
registerTwitterOAuth(twitCred)
> TRUE
accounts_list <- ... ?
Any help would be appreciated ! :)

twitteR Error when authorizing token

I am trying to register an OAuth token and I am running into an error that doesn't seem to be explained anywhere I can find.
require("ROAuth")
require("twitteR")
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
Here is where I would plug in my consumer key and consumer secret which I obtained properly.
consumer_key <- "XXXXXXXXXXXXXXXXXXXXX"
consumer_secret <- "YYYYYYYYYYYYYYYYYYYYY"
This is the first attempt:
twitCred <- OAuthFactory$new(consumerKey = consumer_key, consumerSecret = consumer_secret, requestURL = requestURL,
accessURL = accessURL, authURL = authURL)
But I get the following error:
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object '.setDummyField' not found
Other posts suggested I use the following:
getTwitterOAuth(consumer_key, consumer_secret)
But I get the same error. I can't seem to find the error reproduced anywhere else which is making me suspect it has nothing to do with the twitteR or ROAuth packages. I'm new to these so any advice on how to fix the error is greatly appreciated.
I had the same problem. The following sequence of commands solved the problem:
consumerKey <- ...
consumerSecret <- ...
twitCred <- getTwitterOAuth(consumer_key= consumerKey, consumer_secret=consumerSecret)
searchTwitter()
Notice I didn't use registerTwitterOAuth(twitCred). I think if you wanna save and later load twitCred you have to use registerTwitterOAuth(twitCred) after loading.
I hope this helps.
As is mentioned in the question:
Other posts suggested I use the following:
getTwitterOAuth(consumer_key, consumer_secret)
But I get the same error.
So pbahr's answer wouldn't have helped I take it?
I was getting the same issue using both OAuthFactory$new and getTwitterOAuth but fixed it when I upgraded my R version from 3.0.0 to 3.0.2
I'm not sure if the version is the main cause of the issue but upgrading it worked for me.

Avoid The PIN step in ROAuth handshake if I'm the only user?

Question: Is there a way to avoid having to manually enter a PIN when doing an OAuth handshake?
Context: When making a ROAuth handshake, I am asked to enter a PIN which I obtain by following a link:
rm(list=ls())
library("twitteR")
library("ROAuth")
Credentials <- OAuthFactory$new(
consumerKey = "...",
consumerSecret = "...",
oauthKey = "...",
oauthSecret = "...",
requestURL = "https://api.twitter.com/oauth/request_token",
authURL = "https://api.twitter.com/oauth/authorize",
accessURL = "https://api.twitter.com/oauth/access_token")
Credentials$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
Which outputs:
Credentials$handshake(cainfo = system.file("CurlSSL", "cacert.pem",
package = "RCurl")) To enable the connection, please direct your web
browser to: https://api.twitter.com/oauth/authorize?oauth_token=...
When complete, record the PIN given to you and provide it here:
And I enter a PIN.
I would like to avoid this step because everytime I run the script in a new R session I have to manually open the browser to retrieve the PIN. I am the only person using this script.
After you perform the handshake you can save the object to a file ...
save(Credentials, file="credentials.RData")
At a later date you can use load() to pull the file in and use your original Credentials object.

Resources