How to get most popular Facebook post in R

给你一囗甜甜゛ 提交于 2020-01-05 04:08:15

问题


I am trying to use the following code to get posts from a page on Facebook. I get an error even though the query works when I type it in a browser. This is the error I get:

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

Any thoughts are greatly appreciated!

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


This might be a problem of how the URL is being formatted. If the options argument is not specified, the resulting URL would look like: /me/photos&access_token=.... Here, the path would be /me/photos&access_token which probably is not a valid URL component as per Facebook API.

I think the following changes to the facebook function would fix this:

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

Now, even if the options argument is missing, the resulting URL would look like: /me/photos?access_token=....



来源:https://stackoverflow.com/questions/15046111/how-to-get-most-popular-facebook-post-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!