ROAuth 不再用于支持 httr? [推特 API]
Posted
技术标签:
【中文标题】ROAuth 不再用于支持 httr? [推特 API]【英文标题】:ROAuth no longer used in favor of httr? [Twitter API]ROAuth 不再用于支持 httr? [推特 API]
【发布时间】:2013-09-01 00:36:07
【相关技术】:@tags@
【问题描述】:
我在 AWS“Ubuntu Server 12.04.2 LTS”上运行 R Studio,并通过我的浏览器访问 R Studio。
当我尝试使用带有代码的包ROAuth
在 Twitter API 上进行身份验证时:
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)
registerTwitterOAuth(credential)
说后我得到一个错误
Error in registerTwitterOAuth(credential) :
ROAuth is no longer used in favor of httr, please see ?setup_twitter_oauth
但是我找不到任何进一步的解释..
【问题讨论】:
【参考方案1】:
显然,在我发布之前,twitteR 包已更改,因此新的身份验证方法是
setup_twitter_oauth(CUSTOMER_KEY, CUSTOMER_SECRET, ACCESS_TOKEN, ACCESS_secret, credentials_file=NULL)
见https://github.com/geoffjentry/twitteR
【讨论】:
【参考方案2】:setup_twitter_oauth()
函数有一些问题。我运行了以下代码,它对我有用,没有任何错误。
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()
编辑:
刚刚对我的应用程序 access_token 的问题进行了排序,现在 setup_twitter_oauth
函数运行良好。
如果您的api_key
、api_secret
、acsess_token
、access_token_secret
生成时没有错误,请尝试以下代码使用 R 进行 Twitter 身份验证。
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】:以下内容对我有用:
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】:这是对我有用的 R 脚本:
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】:我在使用 setup_twitter_oauth 函数时遇到了一些问题。 在运行 setup_twitter_oauth 函数或加载凭据文件后,我运行以下代码并没有获得对象。
然后我只加载 cred_1.RData 文件,我得到一个似乎没有用的 sig objetc。
我在 windows 上并安装了 github 版本的包。
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)
【讨论】:
以上是关于ROAuth 不再用于支持 httr? [推特 API]的主要内容,如果未能解决你的问题,请参考以下文章
RCurl::getURL 有效,httr::GET 从对端接收数据时失败
R爬虫总结 | RCurl/httr(请求)→XML/xml2/rvest(解析)