Skip to content

Instantly share code, notes, and snippets.

@darylenriquez
Created February 8, 2017 05:40
Show Gist options
  • Save darylenriquez/d93ada35b9354d27260042598e0eed0e to your computer and use it in GitHub Desktop.
Save darylenriquez/d93ada35b9354d27260042598e0eed0e to your computer and use it in GitHub Desktop.
Using Koala to fetch and put data on Facebooks Graph API
// This is just sample
app_id: 492347987394
secret: erweoryc98b48348753285873624875c83
# Setup on initializers:
module Facebook
SCOPES = 'manage_pages, pages_show_list, publish_pages'
CONFIG = YAML.load_file("facebook.yml")
APP_ID = CONFIG['app_id']
SECRET = CONFIG['secret']
class Authentication < Koala::Facebook::OAuth
def initialize(*args)
case args.size
when 0, 1
raise "application id and/or secret are not specified in the config" unless APP_ID && SECRET
super(APP_ID.to_s, SECRET.to_s, args.first)
when 2, 3
super(*args)
end
end
end
end
# To request for permission for a facebook account, We use the following to generate the url:
Facebook::Authentication.new.url_for_oauth_code(:callback => new_token_url, scope: Facebook::SCOPES, auth_type: 'rerequest')
# in the new token url action, We can get the access token by:
@user_token = Facebook::Authentication.new(new_token_url).get_access_token(params[:code])
# We can then use the user token to get page tokens
@graph = Koala::Facebook::API.new(@user_token)
@items = @graph.get_connections('me', 'accounts')
@page_token = @items.first['access_token']
# We can check if the token has proper permissions using the perms key
@items.first['perms'] # should include CREATE_CONTENT
# We can then use the page token to fetch data
@page_graph = Koala::Facebook::API.new(@page_token)
@page_graph.get_object('me')
# => {"name"=>"Local Company", "id"=>"1831095567158318"}
# To get the feeds (posts in the page):
@page_graph.get_connections('me', 'feed')
# To specify fields:
@page_graph.get_connections('me', 'feed', fields: 'id, created_time, from')
# To include likes and comments:
FIELDS = 'id, created_time, from, likes.summary(true), comments.summary(true){total_count}'
@page_graph.get_connections('me', 'feed', fields: FIELDS)
# To get a single object:
@page_graph.get_object('1831095567158318_1844481365819738')
@page_graph.get_object('1831095567158318_1844481365819738', fields: FIELDS)
# To include metadata:
@page_graph.get_object('1831095567158318_1844481365819738', fields: FIELDS, metadata: true)
# NOTE: metadata is usefull for inspecting the item
# To get multiple objects:
@page_graph.get_objects(['1831095567158318_1844481365819738', '1831095567158318_1844434542491087'])
# or batch them:
list = ['1831095567158318_1844481365819738', '1831095567158318_1844434542491087']
@page_graph.batch do |graph|
list.each do |item|
graph.get_object(item)
end
end
# Batch can also work like this:
@page_graph.batch do |graph|
graph.get_objects(['1831095567158318_1844481365819738', '1831095567158318_1844434542491087'])
graph.get_object('1831095567158318_1844481365819738', fields: FIELDS, metadata: true)
graph.get_object('me')
end
# You can put data like this:
@page_graph.put_comment('me', 'This is a post')
@page_graph.put_comment('1831095567158318_1844481365819738', 'This is a comment')
# To add/remove like:
@page_graph.put_like('1831095567158318_1844481365819738')
@page_graph.delete_like('1831095567158318_1844481365819738')
# Debugging a Token:
@app_oauth = Koala::Facebook::OAuth.new(Facebook::APP_ID, Facebook::SECRET)
@app_token = @app_oauth.get_app_access_token
@app_graph = Koala::Facebook::API.new(@app_token)
@debug_data = @app_graph.debug_token(@user_token)
# By debugging data, we can validate if the user allowed the necessary permissions.
# This can check whether the user revokes a permission after allowing it
@debug_data['data']['scopes'] # should contain 'publish_pages', 'manage_pages', 'pages_show_list'
# By debugging data, we can also check the issue date and the expiration date
@debug_data['issued_at']
@debug_data['expires_at']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment