可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm running R Studio on an AWS "Ubuntu Server 12.04.2 LTS" and accessing R Studio via my browser.
When I try to authenticate at the Twitter API using the package ROAuth
with the code:
credential<-OAuthFactory$new(consumerKey="xxxxx",
consumerSecret="xxxxx",
requestURL="https://api.twitter.com/oauth/request_token",
accessURL="https://api.twitter.com/oauth/access_token",
authURL="https://api.twitter.com/oauth/authorize")
credential$handshake()
registerTwitterOAuth(credential)
I get an error after registerTwitterOAuth(credential)
saying
Error in registerTwitterOAuth(credential) :
ROAuth is no longer used in favor of httr, please see ?setup_twitter_oauth
However I can't find any further explanation..
回答1:
Apparently the twitteR package was changed right before I posted this, so the new way to authenticate is
setup_twitter_oauth(CUSTOMER_KEY, CUSTOMER_SECRET, ACCESS_TOKEN, ACCESS_secret, credentials_file=NULL)
see https://github.com/geoffjentry/twitteR
回答2:
I had some issues with setup_twitter_oauth()
function. I ran the following code and it worked for me without any error.
library(RCurl)
require(twitteR)
library(ROAuth)
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
api_Key <- "XXXXXXX"
api_Secret <- "XXXXXXXXXXXXXXXXX"
twitCred <- OAuthFactory$new(consumerKey=api_key,
consumerSecret=api_secret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL
)
twitCred$handshake()
EDIT:
Just now sorted the issues I had with my app's access_token, now setup_twitter_oauth
function is working perfectly.
Try the below code for Twitter authentication with R, if your api_key
, api_secret
, acsess_token
, access_token_secret
are generated without errors.
api_key = "XXXXXXXXX" // your api_key
api_secret = "XXXXXXXXXX" // your api_secret
access_token = "XXXXXXXXXX" // your access_token
access_token_secret = "XXXXXXXXXX" // your access_token_sceret
setup_twitter_oauth(api_key,api_secret,access_token,
access_token_secret)
回答3:
The following worked for me:
packages <- c("twitteR","ROAuth")#"openssl","base64enc"
### checking if packages are already installed and installing if not
check.install.load.Package<-function(package_name){
if(!package_name%in%installed.packages()){
install.packages(package_name)
}
library(package_name,character.only = TRUE)
}
for(package in packages){
check.install.load.Package(package)
}
api_key = "XX" # your api_key
api_secret = "XX" # your api_secret
access_token = "XX" # your access_token
access_token_secret = "XX" # your access_token_sceret
credential<-OAuthFactory$new(consumerKey=api_key,
consumerSecret=api_secret,
requestURL="https://api.twitter.com/oauth/request_token",
accessURL="https://api.twitter.com/oauth/access_token",
authURL="https://api.twitter.com/oauth/authorize")
credential$handshake()
setup_twitter_oauth(api_key,api_secret,access_token,
access_token_secret)
search.string <- "#RohingyaTerrorReality"
no.of.tweets <- 60
RohingyaTerrorReality.Tweets <- searchTwitter(search.string, n=no.of.tweets,lang="en",)
df <- do.call("rbind", lapply(RohingyaTerrorReality.Tweets, as.data.frame))
View(df)
回答4:
Here is the R script that worked for me:
library("twitteR")
library("ROAuth")
download.file(url= "http://curl.haxx.se/ca/cacert.pem", destfile= "cacert.pem")
credentials <- OAuthFactory$new(consumerKey='XXXXXXXXXXXXXXXXXX',
consumerSecret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
requestURL='https://api.twitter.com/oauth/request_token',
accessURL='https://api.twitter.com/oauth/access_token',
authURL='https://api.twitter.com/oauth/authorize')
credentials$handshake(cainfo="cacert.pem")
save(credentials, file="twitter authentication.Rdata")
load("twitter authentication.Rdata")
setup_twitter_oauth(credentials$consumerKey, credentials$consumerSecret,
credentials$oauthKey, credentials$oauthSecret)
回答5:
I´m having some issues using the setup_twitter_oauth function.
I run the following code and don´t get an object after the setup_twitter_oauth function is run or the credentials file loaded.
Then i just load the cred_1.RData file and i get a sig objetc that doesn´t seem to be useful.
I´m on windows and installed the github version of the package.
rm(list = ls(,all=T))
setwd("D:\\TWEETS\\cuenta_1")
library(twitteR)
library(httr)
# Credenciales
consumer_key = "xxxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxx"
access_token = "xxxxxxxxxxxxxxxxxxx"
access_secret = "xxxxxxxxxxxxxxxx"
credentials_file = "cred_1.RData"
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret, credentials_file)
load_twitter_oauth("cred_1.RData")
ls()
> ls()
[1] "access_secret" "access_token" "consumer_key" "consumer_secret"
[5] "credentials_file"
> load("cred_1.RData")
>
> ls()
[1] "access_secret" "access_token" "consumer_key" "consumer_secret" "credentials_file" "load_twitter_oauth" "set_oauth_sig"
[8] "sig"
>
> print(sig)
Config:
List of 1
$ signature:function (method, url)