I'm enthusiastic and passionate about web development and design in all it's forms and love to build application with efficient and simple methods

Tuesday, June 23, 2015

Use arsduo/koala in Rails for Facebook Connect

10:44 PM Posted by Codeinterpreter No comments
 Koala is a Facebook library for Ruby, supporting the Graph API (including the batch requests and photo uploads), the REST API, realtime updates, test users, and OAuth validation.

Installation

In Gemfile:
gem "koala", "~> 2.0"
gem 'oauth', '~> 0.4.7'

Never forget to bundle install

Build Callback URL route:
 get '/facebook_callback' => 'facebook#callback', as: :facebook_callback

Facebook Configurations

Get App ID and Secret Key:

  1. Go to https://developers.facebook.com/
  2. In My Apps, add or go to your New App
  3. Get your App ID and App secret

App Configurations

In config/environments/{{your-environments}}  add this line

  ENV["facebook_app_id"] = "{{your-app-id}}"
  ENV["facebook_secret_key"] = "{{your-secret-id}}"
  ENV["facebook_callback"] = "{{your-facebook-callback}}"

REST API With OAUTH

This process will built redirect function to permission page user account in Facebook and return with App Token and App Token Secret. Facebook doesn't recognize localhost with port, if we want to built this connectivity it's better to do it in staging or live servers. Only to get Token and Secret Token Code.
// Connect
facebook_connect = Koala::Facebook::OAuth.new({{your-app-id}},{{your-secret-id}}, {{callback URL}})

// This function will automatically redirecting user to Facebook permission page and redirect it back to your Callback URL
redirect_to facebook_connect.url_for_oauth_code(:permissions => ["publish_actions", "user_posts"])

About Permission can be found in here https://developers.facebook.com/docs/facebook-login/permissions/v2.2

In Return Process built this functions

// Get Token Accces Code
code = facebook.get_access_token(params[:code])

// Get Token Access Secret Code
app_code = facebook.get_app_access_token

// Sample of Get my profile from Facebook
client = Koala::Facebook::API.new(code, {{your -secret-key}})
me = client.get_object("me")

// Save code and app_code to database. This tokens needed for REST API Process

REST API From Facebook Get Status, Profile,etc

Get Status and Profile


// Connect
client = Koala::Facebook::API.new({{ Token Accces }}, {{Token Access Secret}} )
// Get Profile and Feed/Status
facebook_data = client.get_connections("me", "feed", {}, api_version: "v2.0")

Post Status and Comment


// Connect
client = Koala::Facebook::API.new(g{{ Token Accces }}, {{Token Access Secret}})
// Post Status
post_content = client.put_wall_post(params[:description])



0 comments:

Post a Comment