Search code examples
rfacebook-graph-apicurlrcurlhttr

Converting cURL Facebook API POSTs into R and storing the result


There are variations on this in existing posts, but specifically for the Facebook API I'd like to be able to use cURL to POST photos to the API using R and store the returned ID.

An example would be:

curl -i -X POST \
-d "url=https://wheremyimageishosted.com/test_photo.png" \
-d 'published=false' \
-d "access_token=<my_access_token>" \
"https://graph.facebook.com/v2.10/me/photos"

And what is returned is in the format of:

{"id":"1234123412341234"} .

I know there are various packages for cURL for R, but they're not the easiest to understand for an R intermediate, but cURL beginner.

Thus, the two asks here are:

  1. What would the above cURL code be using something like httr or RCurl (and the best package to use)?
  2. How can I store the numeric value returned as a variable?

Thanks.


Solution

  • Not sure why someone would downvote me, but to answer my own question, I'd recommend the wonderful httr package for converting cURL into R. I've got it to work for a number calls to and from the Facebook API. The last stumbling block for me was to remember to use

    encode = "json"
    

    ... where appropriate. Details are here:

    https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html