Skip to content

How to get started with RFacebook

Miguel Tokumoto edited this page Aug 25, 2016 · 2 revisions

How-to get started with RFacebook

This small guide will guide you through the process of getting a Facebook App and proceeding to obtain a token with R.

Required packages: RFacebook, httr

Disclaimer: This guide works with httr (1.2.1) and RFacebook(0.6.6). The following steps might change due to Facebook UI/UX changes in the future. This guide follows @darogzic's guide for the fbRads package.

Get your Facebook App

  1. Go to http://developers.facebook.com and Add a New App from the menu on the upper right corner menu.
  2. Choose the basic setup.
  3. Put a Display Name (ie: rfacebook_experiment_1), put real Contact Email and choose the Business category for your app.
  4. Once in the app dashboard, you will see your App ID and App Secret, you will need those so I recommend you copy them and paste them into your code (declare them a variables). In the App Domains field, type in localhost.
  5. Go to Settings > Add Platform > Website and type in Site URL: http://localhost/1410
  6. On the left hand menu, go to Facebook Login and in the Valid OAuth redirect URIs field type http://localhost:1410/

Your R Code

Paste the following code:

library(httr)

oauth_endpoints("facebook")
AppID <- "Your app ID"
AppSecret <- "Your app secret"

app <- oauth_app('facebook', AppID, AppSecret)
fb_token <- oauth2.0_token(oauth_endpoints("facebook"), app,
                           scope = "read_insights",
                           type  = "application/x-www-form-urlencoded", 
                           cache = FALSE)
# This saves the token for future use without having to login everytime you want to use the package
save(fb_token, file = "~/Documents/fb_token")

Now you can call you token from other scripts:

load(~/Documents/fb_token)

And you're ready to go!

Remember, the functions on the package will ask for fb_token$credentials$token as the argument it needs to run. It'd be wise to declare it as a separate variable!