0

I am trying to schedule an R script to run, using rtweet to hit the Twitter API and then using RPOSTgreSQL to load the data into a table once a day.

I was able to successfully use taskscheduleR to create the task. However, when it runs I get an error that says...

<credentials> oauth_token, oauth_token_secret
---
Requesting token on behalf of user...
Error: API user token required. see http://rtweet.info/articles/auth.html for instructions
Execution halted

Here is my entire code with a few things masked due to API credentials and database info, passwords, etc

#load all packages
library(rtweet)
library(sqldf)
library(dplyr)
library("RPostgreSQL")

#connect to Twitter API
create_token(
  app = "masked for stack",
  consumer_key = "masked for stack",
  consumer_secret = "masked for stack",
  access_token = "masked for stack",
  access_secret = "masked for stack")

## get user IDs of accounts following 
followers=get_followers("masked for stack", n = 1000)

## lookup data on those accounts
followers_data=lookup_users(followers$user_id)

#add the date of the run
followers_data$date=Sys.Date()

#extract columns
twitter_followers=dplyr::select(followers_data,"date","screen_name","name","location","description","followers_count",
                                "friends_count","listed_count","statuses_count","favourites_count","verified")

# create a connection
# save the password that we can "hide" it as best as we can by collapsing it
pw <- {
  "masked for stack"
}

# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
# note that "con" will be used later in each connection to the database
con <- dbConnect(drv, dbname = "masked for stack",
                 host = "localhost", port = 5432,
                 user = "postgres", password = pw)
rm(pw) # removes the password

# writes df to the PostgreSQL database
dbWriteTable(con, "twitter_followers", 
             value = twitter_followers, append = TRUE, row.names = FALSE)

My code runs perfectly fine doing everything on my own manually. It seems that rtweet doesn't like getting ran by windows scheduler.

Any ideas? `

Marc
  • 11
  • 2

1 Answers1

1

So after hours of digging I figured out that something was not working well with the scheduleR app. It may have been creating a task in windows scheduler that wouldn't run when my laptop was on AC power I'm not sure. Either way I did a workaround like this.

Created a text file and saved as a .bat extension with the following format:

"C:\Program Files\R\R-3.5.2\bin\x64\R.exe" CMD BATCH file_location.R

From there I scheduled in Windows scheduler. Make sure you go through all the options and don't choose a basic task. If you do a basic one you may not allow it to run on laptop battery power.

Marc
  • 11
  • 2