Reputation: 2884
I am running a twitter sentiment analysis in R and have followed the examples from the twitteR package in setting up the OAuth parameters like so:
library(ROAuth)
library(twitteR)
library(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"
consumerKey <- "xxxxxxxxxxxxxxxxxxxxxxx"
consumerSecret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
twitCred$handshake()
registerTwitterOAuth(twitCred)
I also want to maintain the authentication in the R environment so that I do not need to do the handshake every time I start and load this script. For instance, I run this piece of code and save the environment. Then upon restarting R to run functions such as searchTwitter
, I get this error:
Error in twInterfaceObj$doAPICall(cmd, params, "GET", ...) :
OAuth authentication is required with Twitter's API v1.1
I thought that the registerTwitterOAuth(twitCred)
is what saves my authentication details in the R environment but I guess I am wrong. If I do everything manually, the authentication as well as my twitter scraping works fine but I want to move towards automating this script on a linux server eventually.
What must I do so that the authentication stays in the R environment so that I can run automated scripts?
I am running R v.3.1.1 x64 on a Windows 7 x64 machine.
Upvotes: 0
Views: 352
Reputation: 12860
Also you might havre a look at this: https://stackoverflow.com/a/29505711
In addition to hrbrmstr suggesion of saveing credentials in your very own user folder outside of the script doing the work! ... consider this for 'headless' authentication:
library(httr)
options("httr_oauth_cache"=FALSE)
# 1. Find OAuth settings for twitter:
# https://dev.twitter.com/docs/auth/oauth
oauth_endpoints("twitter")
# 2. Register an application at https://apps.twitter.com/
# Make sure to set callback url to "http://127.0.0.1:1410"
#
# Replace key and secret below
myapp <- oauth_app("twitter",
key = "fookey",
secret = "foosecret"
)
# 3. Get OAuth credentials
access_token="footoken"
access_secret="footokensecret"
twitter_token <-
Token1.0$new(
endpoint = NULL,
params = list(as_header = TRUE),
app = myapp,
credentials = list(
oauth_token = access_token,
oauth_token_secret = access_secret
)
)
# 4. Use API
req <- GET("https://api.twitter.com/1.1/statuses/home_timeline.json",
config(token = twitter_token))
stop_for_status(req)
content(req)
Upvotes: 0
Reputation: 78822
Something like:
save(twitCred, file="~/.twitteR_creds")
Then to get it back for future use:
load(""~/.twitteR_creds")
registerTwitterOAuth(twitCred)
(I'm using ~/.twitteR_creds
on linux/OS X, but try to do something similar on Windows so your API keys/creds say out of the code area that you might end up sharing with others.
Upvotes: 1