2013-02-23 25 views
1

我想使用下面的代碼從Facebook上的頁面獲得帖子。即使查詢在瀏覽器中輸入時出現錯誤,我也會收到錯誤。這是我得到的錯誤:如何獲得最受歡迎的Facebook發佈在R

WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unknown path components: 

任何想法都非常感謝!

# go to 'https://developers.facebook.com/tools/explorer' to get your access token 
access_token <- "### token ###" 

require(RCurl) 
require(rjson) 

cafile <- system.file("CurlSSL", "cacert.pem", package = "RCurl") 

options(RCurlOptions = list(verbose = TRUE, followlocation = TRUE, timeout = 100, useragent = "R")) 


# set the curl options 
curl <- getCurlHandle() 
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem", 
              package = "RCurl"), 
         ssl.verifypeer = FALSE, verbose = TRUE, cookiejar = 'my_cookies.txt', 
         cookiefile = 'my_cookies.txt', followlocation = TRUE,                   
         useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3')) 
curlSetOpt(.opts = list(proxy = 'proxyserver:port'), curl = curl) 


# Facebook json function copied from original (Romain Francois) post 
facebook <- function(path = "me", access_token, options){ 
if(!missing(options)){ 
options <- sprintf("?%s", paste(names(options), "=", unlist(options), collapse = "&", sep = "")) 
} else { 
options <- "" 
} 
data <- getURL(sprintf("https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token)) 
fromJSON(data) 
} 


### TED FACEBOOK PAGE 
# http://www.facebook.com/TED 
# TED's Facebook ID 29092950651 can be found on http://graph.facebook.com/TED 

ted <- list() 
i<-0 
next.path <- "29092950651/posts" 

# download all TED posts 
while(length(next.path)!=0) { 
    i<-i+1 
    ted[[i]] <- facebook(path=next.path , access_token=access_token) 
    next.path <- sub("https://graph.facebook.com/","",ted[[i]]$paging$'next') 
} 
ted[[i]] <- NULL 

# parse just video links posted by TED 
parse.count.ted <- function(x) 
    if (x$type=="link" & x$from$id=="29092950651") x$likes$count else NA 
parse.link.ted <- function(x) 
    if (x$type=="link" & x$from$id=="29092950651") x$link else NA 
ted.counts <- unlist(sapply(ted, parse.master, f=parse.count.ted)) 
ted.links <- unlist(sapply(ted, parse.master, f=parse.link.ted)) 

# see three most popular talks 
ted.links[order(ted.counts,decreasing=TRUE)][1:3] 
+1

您可以將您的問題歸結到您的代碼中您收到錯誤的特定位置? – 2013-02-23 23:16:41

回答

3

這可能是URL格式化的問題。如果未指定options參數,則生成的URL將如下所示:/me/photos&access_token=...。在這裏,path應該是/me/photos&access_token,這可能不是根據Facebook API有效的URL組件。

我覺得到facebook功能如下變化將解決這個問題:

require(RCurl) 
require(rjson) 

facebook <- function(path = "me", access_token = token, options){ 
    if(!missing(options)){ 
     options <- sprintf( 
          "?%s&", 
          paste( 
           names(options), "=", unlist(options), 
           collapse = "&", sep = "" 
           ) 
          ) 
    } else { 
     options <- "?" 
    } 

    urlTemplate <- "https://graph.facebook.com/%s%saccess_token=%s" 
    data <- getURL( 
        sprintf( 
          urlTemplate, 
          path, 
          options, 
          access_token 
          ) 
        ) 
    fromJSON(data) 
} 

現在,即使options參數丟失,產生的URL看起來像:/me/photos?access_token=...

+0

非常感謝!現在我收到以下錯誤: 錯誤在myposts [[1]] $ paging $「next」: $操作符對原子向量無效 – Lauren 2013-02-24 19:21:15

+0

我沒有看過由'facebook'調用返回的JSON數據,所以我無法對此發表評論。但是,這聽起來像是一個單獨的問題。你能否在另一個問題上發佈JSON數據(和'myposts [[1]]',可能),並接受這個答案,如果它解決了當前的問題? – 2013-02-24 21:39:23

+0

非常感謝!這裏是一個單獨的問題的鏈接: http://stackoverflow.com/questions/15069078/parsing-facebook-json-results – Lauren 2013-02-25 14:18:42