Last active
January 10, 2017 16:23
-
-
Save zosiu/abf8be0afb9177ccdebc to your computer and use it in GitHub Desktop.
Facebook Graph API w/ Koala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### app access token | |
graph = Koala::Facebook::OAuth.new APP_ID, APP_SECRET | |
graph.get_app_access_token # => APP_TOKEN | |
### create test users with permissions | |
test_users = Koala::Facebook::TestUsers.new app_id: APP_ID, secret: APP_SECRET | |
user = test_users.create true, 'email,user_birthday,user_posts', name: 'Bob' | |
# => | |
# { "id" => USER_ID, | |
# "access_token" => USER_ACCESS_TOKEN, | |
# "login_url" => USER_LOGIN_URL, | |
# "email" => USER_EMAIL, | |
# "password" => USER_PASSWORD } | |
test_users.create_network 5, true, 'email,user_birthday,user_posts,user_photos' # => they will all be friends` | |
test_users.delete_all # you can also destroy everyone | |
### debug token | |
graph = Koala::Facebook::API.new APP_TOKEN, APP_SECRET | |
graph.debug_token token | |
# => | |
# { "data" => | |
# { "app_id" => APP_ID, | |
# "application" => APP_NAME, | |
# "expires_at" => 1426514400, | |
# "is_valid" => true, | |
# "scopes" => ["public_profile", "basic_info", "email", "user_birthday", "user_friends", "user_posts"], | |
# "user_id" => USER_ID | |
# } | |
# } | |
### generate a long-lived token | |
oauth = Koala::Facebook::OAuth.new APP_ID, APP_SECRET | |
oauth.exchange_access_token short_lived_token # => LONG_LIVED_TOKEN | |
### get posts with image (normal size not just the 130x130 thumbnail) | |
graph = Koala::Facebook::API.new USER_ACCESS_TOKEN, APP_SECRET | |
graph.get_connections 'me', 'posts', fields: 'message,story,type,attachments' | |
# profile picture: "https://graph.facebook.com/#{user_facebook_uid}/picture?width=1500&height=1500" | |
### permissions | |
graph.get_connection 'me', 'permissions' | |
# => | |
# [{"permission"=>"public_profile", "status"=>"granted"}, | |
# {"permission"=>"email", "status"=>"granted"}, | |
# {"permission"=>"user_birthday", "status"=>"granted"}, | |
# {"permission"=>"user_photos", "status"=>"granted"}] | |
# => | |
# [{"permission"=>"public_profile", "status"=>"granted"}, | |
# {"permission"=>"email", "status"=>"granted"}, | |
# {"permission"=>"user_photos", "status"=>"granted"}, | |
# {"permission"=>"user_birthday", "status"=>"declined"}, | |
# {"permission"=>"user_posts", "status"=>"declined"}] | |
### albums with cover photos | |
offset = 0 | |
limit = 200 | |
_cover_ids, cover_photos = graph.batch do |batch_api| | |
batch_api.get_connection 'me', 'albums', { offset: offset, limit: limit, fields: 'cover_photo' }, batch_args: { name: 'cover-ids', omit_response_on_success: false } | |
batch_api.get_objects "{result=cover-ids:$.data.*.cover_photo}", fields: 'album{name,count},source' | |
end | |
albums = cover_photos.each_with_object([]) do |(_c_id, data), ret| | |
ret << { id: data['id'], | |
name: data['album']['name'], | |
picture_count: data['album']['count'], | |
cover_url: data['source'] } | |
end | |
### getting picture_urls from album | |
pic_limit = 100 | |
pic_offset = 0 | |
album_photos = graph.get_object album_id, fields: "photos.limit(#{pic_limit}).offset(#{pic_offset}){source}" | |
photo_urls = album_photos['photos']['data'].map { |p| p['source'] } | |
# page RTU | |
# https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment