R Authentication error when looping search_tweets function (rtweet package) over vector of Twitter handles

家住魔仙堡 提交于 2019-12-03 07:18:28

On the first error:

"Warning: 32 - Could not authenticate you. Error in vector("list", ntimes) : invalid 'length' argument"

in general, you'll encounter this error if you are using an older version of rtweet.

Why?

When Twitter updates their API's, they sometimes change the structure of the API GET requests. rtweet has to reformat their requests each time this happens, requiring you to use the latest version of rtweet to maintain connectivity to the Twitter API. Interestingly, some API calls will still succeed as those calls to the Twitter API are unchanged.

The error you encountered is referenced @ TwitterCommunity.com.

Obtaining the latest version of rtweet

To obtain the latest version of rtweet you can use the devtools package (after installing it).

## install devtools package if it's not already
if (!requireNamespace("devtools", quietly = TRUE)) {
  install.packages("devtools")
}

## install dev version of rtweet from github
devtools::install_github("mkearney/rtweet")

## load rtweet package
library(rtweet)

Related Errors

Error in vector("list", n.times) : invalid 'length' argument In addition: Warning message: rate limit exceeded.

A good place to look for tracked errors is the Github package tracking list on github for the rtweet package.

Token Security

This is an aside comment but my sense is you may also want to share your complete code without your API keys. You can do this in R by using ~/.Reviron.

# Reload .Renviron
# Do this to capture any edits to Environment variables
readRenviron("~/.Renviron")

# Generate a token
token <- create_token(
  app = "rtweet_51672443_test_application",
  consumer_key = Sys.getenv("RTWEET_CONSUMER_KEY"),
  consumer_secret = Sys.getenv("RTWEET_CONSUMER_SECRET_KEY")
) 

where .Renviron contains:

RTWEET_CONSUMER_KEY="<Insert Consumer Key obtained from Titter>"
RTWEET_CONSUMER_SECRET_KEY="<Insert Consumer Secret Key obtained from Titter>"

I hope the above helps point you in the right direction.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!