通过 R 在 Facebook API 上出现未知错误。

Unknown error on Facebook API through R.

我试图通过 RFacebook 从 facebook 页面下载所有帖子,但是当该页面有大量帖子(超过 400 条左右)时,脚本停止,返回错误

"Error in callAPI(url = url, token = token) : An unknown error has occurred." at the line where I call the getPage.

library(Rfacebook)
library(stringr)
load("fb_oauth")
token=fb_oauth
page<-getPage("bicocca", token, n = 100000, since = NULL, until = NULL, feed = TRUE)
noSpaceMsg<-str_replace_all(page$message, "[\r\n]" , "")
output<-as.data.frame(cbind(page$from_name,page$id, noSpaceMsg, page$created_time, page$type, page$link, page$likes_count, page$comments_count, page$shares_count))
colnames(output)<-c("username","msgid", "message", "created_time", "type", "link", "likes", "comments", "shares")
write.csv(output, "bicocca.csv", row.names=FALSE)

问题出在哪里?我该如何解决?

这似乎是 API 的问题,而不是 R 包的问题。当我尝试在 Graph API Explorer here 中进行查询时,我也遇到了错误。不知道为什么。

解决这个问题的一种方法是逐月查询,将 getPage 函数包装在 try 命令中:

page <- 'bicocca'
dates <- seq(as.Date("2010/10/01"), as.Date("2015/04/20"), by="month")
n <- length(dates)-1

df <- list()

for (i in 1:n){
    cat(as.character(dates[i]), " ")
    try(df[[i]] <- getPage(page, token, since=dates[i], until=dates[i+1]))
    cat("\n")
}

df <- do.call(rbind, df)

这不会给你所有的帖子,但可能是大部分。