Search code examples
rubyfacebook-graph-apisinatrakoala

Facebook Graph API for websites using Ruby Koala gem in Sinatra


I want to implement Facebook login for web apps. All I need is the basic public information of a user for the account creation steps.

This is what I have done:

  1. Created a basic Facebook app with nearly no custom permissions.
  2. Used the APP_ID and APP_SECRET in Koala to get access_token.
  3. Everything worked perfectly, I am able to login/logout.
  4. Just that the only information I am able to get back when I do: graph.get_object('me') is the logged in user's name and an id (It doesn't look like the default Facebook id).
  5. Surprised whether something changed in the new API, I tested the gem in the console using the access_token from graph explorer (where all permissions are enabled by default). And I get all data using the same method call.
  6. When I review what all the app gets while signing up; I see that the user's basic information, profile pic and other public data will be accessible to the app.

Any idea why this is so? It seems I am missing something obvious. The code is available in Github. But this is pretty much everything to it:

require 'bundler'
Bundler.require :default
Dotenv.load '.env'
require_relative './app/constants.rb'

module Banana
  class App < Sinatra::Base
    use Rack::Session::Cookie, secret: COOKIE_SECRET
    set :public_folder, File.dirname(__FILE__) + '/bower_components'

    get '/' do
      if logged_in?
        haml :welcome_in, layout: :layout
      else
        haml :log_in, layout: :layout
      end
    end

    get '/log_out' do
      session['oauth'] = nil
      session['access_token'] = nil

      redirect '/'
    end

    get '/log_in' do
      session['oauth'] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, "#{request.base_url}/call_back")
      redirect session['oauth'].url_for_oauth_code()
    end

    get '/call_back' do
      begin
        session['access_token'] = session['oauth'].get_access_token(params[:code])
      rescue
        redirect '/?error=user_denied'
      end
      redirect '/'
    end

    get '/test' do
      if logged_in?
        p graph.get_object("rakeshbs")
        "e"
      else
        redirect '/'
      end
    end

    def logged_in?
      !session['access_token'].nil?
    end

    def toggle_access
      logged_in? ? '/log_out' : '/log_in'
    end

    def graph
      @graph ||= Koala::Facebook::API.new(session['access_token'])
    end

    def errored?
      !params["error"].nil?
    end

    def user
      p graph.get_connections(:me, :photos) # This is just nil
      @user ||= OpenStruct.new(
        name: graph.get_object("me")["name"], # All I get here is just a hash with the name and an id!
        photo: 'http://semantic-ui.com/images/avatar/small/elliot.jpg'
      )
    end
  end
end

Solution

  • You should add fields parameter.

    Something like this: graph.get_object('me', { fields: 'id,first_name,last_name,gender,birthday,photos,email' })