0

我正在尝试安排 R 脚本运行,使用 rtweet 访问 Twitter API,然后使用 RPOSTgreSQL 每天一次将数据加载到表中。

我能够成功地使用 taskscheduleR 来创建任务。但是,当它运行时,我收到一条错误消息...

<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

这是我的整个代码,由于 API 凭据和数据库信息、密码等原因,有些东西被掩盖了

#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)

我的代码运行得非常好,我自己手动完成所有事情。似乎 rtweet 不喜欢被 Windows 调度程序运行。

有任何想法吗?`

4

1 回答 1

0

因此,经过数小时的挖掘,我发现 scheduleR 应用程序无法正常工作。它可能在 Windows 调度程序中创建了一个任务,当我的笔记本电脑使用交流电源时,它不会运行我不确定。无论哪种方式,我都做了这样的解决方法。

创建了一个文本文件并保存为 .bat 扩展名,格式如下:

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

从那里我安排在 Windows 调度程序中。确保您完成所有选项并且不要选择基本任务。如果你做一个基本的,你可能不允许它在笔记本电脑电池电源上运行。

于 2019-03-02T03:58:18.730 回答