rtweet create_token missing value where TRUE/FALSE needed - r

So I have been collecting tweets for some months on an external Linux Centos 7.6.1810 server but for some reasons, I can not do it anymore.
So my code is
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_secret = 'xxx'
options(httr_oauth_cache=T)
rtweet::create_token(
app = "appname",
consumer_key = consumer_key,
consumer_secret = consumer_secret,
access_token = access_token,
access_secret = access_secret)
It has been working fine for months but some reasons, now I get the error
Error in if (file == "") stop("'file' must be non-empty string") :
missing value where TRUE/FALSE needed
If i look at the parameters of the function create_token, the only parameter that I haven't put is set_renv but even with that parameter, it does not work.
However, it work fine on my local windows computer... any suggestions?

I had the same issue.
The culprit was the file .rtweet_token.rds which is created every time the authentication is required. Actually, if the file already exists, then a new file is created by appending a number to it (.rtweet_token1.rds , .rtweet_token2.rds, etc).
It happened that I already had 1000 instances of these files and for some reason a new file was not being created.
The solution was to delete all the instances of the file.

Removing the file .rtweet_token.rds did the trick!

You have to delete ALL of the .rtweet_token.rds files which you can do.
If you are in RStudio, you can go to Files, Home, More, and Show Hidden Files. You will then see all of files. Delete them all and then it will work again.

Related

TwitteR scraping with R error, fixing ideas?

I'm a bit desperate with my problem as I checked all the existing sources and nothing worked for me.
The issue:
I'm trying to scrape twitter with R in Rstudio, I'm on the very first stage of auth with API keys:
library(twitteR)
library(ROAuth)
library(openssl)
library(httpuv)
library(devtools)
origop <- options("httr_oauth_cache")
options(httr_oauth_cache = TRUE)
api_key <- "*****"
api_secret <- "******"
access_token <- "*****"
access_secret <- "******"
setup_twitter_oauth(api_key, api_secret, access_token, access_secret)
I'm using keys with essential access, v2 access level (which in theory shouldn't be the reason for the bug, but who knows?..)
This code gives the error:
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
What I tried:
1. Restarting the session after installing the packages
2. Fixing httr bug by running:
library(devtools)
devtools::install_version("httr", version="0.6.0", repos="http://cran.us.r-project.org")
3. Fixing possible compatibility issue by running:
devtools::install_github("jrowen/twitteR", ref = "oauth_httr_1_0")
4. running all of the above, restarting R and even restarting the laptop 3 times.
5. Checked my firewall - it's off.
6. Checked my default browser and being logged in in it into twitter - done.
7. Tried re-generating my access keys and re-running all of the above - didn't help.
I don't know what else to try, does anybody have any idea how to fix this issue?
Thanks a million in advance!
Aright, so after some checking here's the solution (if anybody ever needs it):
use library (rtweet)
apply and get approved for elevated access. That was definitely the problem that didn't let me go ahead.

rtweet - Warning: 32 - Could not authenticate you

I notice a few people have encountered issues with rtweet authorization. I followed this vignette which assumes people are familiar with authentication protocols. Newbies such as myself, struggle with this.
vignette("auth", package = "rtweet")`
I stored all keys and tokens in my .Renviron file. This is the code I use to access keys and tokens:
require(rtweet)
require(httpuv)
api_key <- Sys.getenv("TWITTER")
api_secret_key <- Sys.getenv("TWITTER_SECRET")
access_token <- Sys.getenv("ACCESS_TOKEN")
access_token_secret <- Sys.getenv("ACCESS_SECRET")
I then create a token using this code:
token <- create_token(
app = "my_app",
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = access_token,
access_secret = access_token_secret)
Next, I check if a token has been created:
get_token()
I get this response:
<Token>
<oauth_endpoint>
request: https://api.twitter.com/oauth/request_token
authorize: https://api.twitter.com/oauth/authenticate
access: https://api.twitter.com/oauth/access_token
<oauth_app> my_app
key: XXXXXXXXXXXXXXXXXXXX
secret: <hidden>
<credentials> oauth_token, oauth_token_secret
---
I restarted R and run a query as follows:
tweet_data <- search_tweets("#plasticrecycling",
n = 2000,
include_rts = F,
lang = "en")
And end up with this error:
Warning: 32 - Could not authenticate you.
Warning message:
Could not authenticate you.
Can someone please explain why this is happening? A similar issue was reported here https://github.com/ropensci/rtweet/issues/439 but it was closed without a clear answer. I am not sure if create_token() is a once-off thing or must be initiated each time I use R. I notice this added to my .Renviron file:
TWITTER_PAT=/Users/bob/.rtweet_token.rds
Thanks in advance.
Loading the latest development version of rtweet solves the problem I was encountering:
remotes::install_github("rOpenSci/rtweet")
packageVersion("rtweet")
[1] ‘0.7.0.9026’
This version makes it easier to authenticate things.
auth <- rtweet_app(bearer_token = Sys.getenv("BEARER_TOKEN"))
auth_save(auth, "my_app")
auth_list()
[1] "my_app"
auth_as(auth = "my_app")
tweets <- search_tweets("#plasticrecycling",
include_rts = F,
n = 1000,
lang = "en")
One only has to run the rtweet_app() once and then save the output. Every time one restarts R and loads a Twitter script, one simply has to execute auth_as(auth = "your_app")
I was confused by OAuth1.0 versus OAuth2.0 on the Twitter API page. It also did not help reading the wrong online manual pages for a different version of rtweet!
Kudos to llrs for helping me resolve my issue.

Error when using R to get credentials from Windows Cred Vault

I receive the following error...
Error in b_wincred_i_get(target) :
Windows credential store error in 'get': Element not found.
...when running the following script in R v3.5.1 (R Studio v1.1.456) and keyring v1.1.0. I'm attempting this on a new setup so not sure if this could be as simple as a firewall issue or the like. The script errors out when attempting to get the password (key_get(json_data$service, json_data$user)). I've tried manually plugging in the service and username into the key_get method (instead of using the variables from the config file) but get the same error. The config file is a json file that holds all of the connection details except the password, which obviously gets retrieved from the Windows Credentials Vault. Any help in figuring out a fix for this is greatly appreciated.
library(RJDBC)
library(keyring)
library(jsonlite)
postgres.connection <- function(json_data){
print("Creating Postgres driver...")
pDriver <- JDBC(driverClass=json_data$driver, classPath="C:/Users/Drivers/postgresql-42.2.4.jar")
print("Connecting to Postgres...")
server <- paste("jdbc:postgresql://", json_data$host, ":", json_data$port, "/", json_data$dbname, sep="")
pConn <- dbConnect(pDriver, server, json_data$user, key_get(json_data$service, json_data$user))
return(pConn)
}
json_data <- fromJSON("C:/Users/Configs/Config.json", simplifyVector = TRUE, simplifyDataFrame = TRUE)
json_data_connection <- json_data$postgres$local_read
pc <- postgres.connection(json_data_connection)
You mentioned it's a new setup. Is the password already in credentials? To check, go to 'Control Panel'>'User Accounts'>'Credential Manager'>'Windows Credentials', is it under 'Generic Credentials'?
I can get the same error by running:
key_get('not-valid-service', 'some-user')

Spotifyr Error: INVALID_CLIENT: Invalid redirect URI

I am trying to use the spotifyr package to extract the data from Spotify however any function that requires an access token is not working (e.g. get_my_recently_played(limit = 5)). I get the following error when I run it: INVALID_CLIENT: Invalid redirect URI.
I have even tried going to My Application and changing the Redirect URIs to 'http://localhost:8888/callback/' but it still gives the same error. What might I be missing here? Thank you!
Have you tried:
Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')
access_token <- get_spotify_access_token()
per the readme at https://github.com/charlie86/spotifyr?
UPDATE:
This works for me with the version of `spotifyr` available on CRAN
Sys.setenv(SPOTIFY_CLIENT_ID = 'your_client_id_here')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'your_client_secret_here')
access_token <- get_spotify_access_token()
library(spotifyr)
get_artist_audio_features('the beatles')
Try to set this redirect URL in Spotify API Client settings to http://localhost:1410/
I had the same issue and this URL works for me now

How to open Excel 2007 File from Password Protected Sharepoint 2007 site in R using RODBC or RCurl?

I am interested in opening an Excel 2007 file in R 2.11.1 using RODBC. The Excel file resides in the shared documents page of a MOSS2007 website. I currently download the .xlsx file to my hard drive and then import to R using the following code:
library(RODBC)
con<-odbcConnectExcel2007("C:/file location/file.xlsx")
data<-sqlFetch(con, "worksheet name")
close(con)
When I type in the web url for the document into the odbcConnectExcel2007 connection, an error message pops up with:
ODBC Excel Driver Login Failed: Invalid internet Address.
followed by the following message in my R console:
ERROR: Could not SQLDriverConnect
Any insights you can provide would be greatly appreciated.
Thanks!
**UPDATE**
The site I am attempting to download from is password protected. I tried another method using the method 'getUrl' in the package RCurl:
x = getURL("http://website.com/file.xlsx", userpwd = "uname:pw")
The error that I receive is:
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
embedded nul in string: 'PK\003\004\024\0\006\0\b\0\0\0!\0dA»ï\001\0\0O\n\0\0\023\0Ò\001[Content_Types].xml ¢Î\001( \0\002\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
I have no idea what this means. Any help would be appreciated. Thanks!
Two solutions worked for me.
If you do not need to automate the script that pulls the data, you can map a network drive pointing to the sharepoint folder from which you want to extract the Excel document.
If you need to automate a script to pull the Excel file every couple of minutes, I recommend sending your authentication credentials in a request that automatically saves the file to a local drive. From there you can read it into R for further data wrangling.
library("httr")
library("openxlsx")
user <- <USERNAME>
password <- <PASSWORD>
url <- "https://sharepoint.company/file_to_obtain.xlsx"
httr::GET(url,
authenticate(user, password, type="ntlm"),
write_disk("C:/tempfile.xlsx", overwrite = TRUE))
df <- openxlsx::read.xlsx("C:/tempfile.xlsx")
You can obtain the correct URL to the file by clicking on the sharepoint location and removing "?Web=1" after the file ending (xlsx, xlsb, xls,...). USERNAME and PASSWORD are usually windows credentials. It helps storing them in a key manager (such as:
library("keyring")
keyring::key_set_with_value(service = "Windows", username = "Key", password = <PASSWORD>)
and then authenticating via
authenticate(user, kreyring::key_get("Windows", "Key"), type="ntlm")
in some instances it may be sufficient to pass
authenticate(":", ":", type="ntlm")
if only your Windows credentials are required and the code is running from your machine.

Resources