Topic: please help me facebook application

anyone could help me ??
My problems are:
NoMethodError in PagesController#home

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

Rails.root: /home/tranvuthienhh/rails_project/Sutalk/SUtalk
Application Trace | Framework Trace | Full Trace

app/models/facebook.rb:91:in `getToken'
app/models/facebook.rb:11:in `initialize'
app/controllers/application_controller.rb:12:in `new'
app/controllers/application_controller.rb:12:in `facebook_login'
my code below:
def getToken(params = {}) 
    # dataSignedRequest = decode_facebook_hash(params["signed_request"])
    dataSignedRequest = getSignedRequest(params["signed_request"])
    token = dataSignedRequest["oauth_token"]
  end 
def initialize(params = {}) 
    @token = getToken params
    @oauth_url = getOauthUrl params

    return if loggedout?

    @facebook_user = getUser
    @onlineFriends = getOnlineFriends
    @friendsRandom = getFriendsRandom
  end
# Login to facebook if user is not already logged in
  def facebook_login
    parameters = getParams params
    @fb = Facebook.new parameters

    session[:facebook_id] = @fb::facebook_user["id"] if !@fb::facebook_user.nil?
       
    if @fb.loggedout?
                # Redirect to facebook login
      render :facebooklogin, :layout => false
    elsif !User.registered? @fb
      User.register @fb
    end   
  end

Last edited by thientv84 (2011-05-16 12:44:04)

Re: please help me facebook application

It looks like you have an instance of an empty array somewhere but there is not enough information in your post to accurately tell you where your problems lie but I'll take an educated punt at the issue being one of these two lines

    dataSignedRequest = getSignedRequest(params["signed_request"])
    token = dataSignedRequest["oauth_token"]

and I'm thinking that the object you have called params is an empty array whioch you will need to deal with accordingly (Very surprised that your test suite didn't pick up on this)

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: please help me facebook application

thanks jamesw for your reply
I've guessed that yet. But i'm just junior Ruby big_smile, so still I dont know.
here's getSignedReuest function
and lib included

require 'rubygems'
require 'rest_client'
def getSignedRequest( signed_request )
    encoded_sig, payload = signed_request.split('.')
    data = ActiveSupport::JSON.decode base64_url_decode(payload)
                  # Don't bother with security of checking authenticity of token
    return data
  end
Could you find out the error ??

Re: please help me facebook application

when I start server.
error appear :
The error occurred while evaluating nil.[]):
  app/models/facebook.rb:91:in `getToken'
  app/models/facebook.rb:11:in `initialize'
  app/controllers/application_controller.rb:12:in `new'
  app/controllers/application_controller.rb:12:in `facebook_login'
----------------------------------
file facebook.fb

require 'rubygems'
require 'rest_client'


class Facebook

  attr_accessor :token, :facebook_user, :friends,
                :oauth_url, :onlineFriends, :friendsRandom   

  def initialize(params = {}) 
    @token = getToken params
    @oauth_url = getOauthUrl params

    return if loggedout?

    @facebook_user = getUser
    @onlineFriends = getOnlineFriends
    @friendsRandom = getFriendsRandom
  end

  # get facebook oauth url
  def getOauthUrl params
    api = Sutalk::Application.config.facebook_api

    if params[:sid].nil?
      "#{api[:auth_url]}&redirect_uri=#{api[:canvas_page]}"   
    else
      "#{api[:auth_url]}&redirect_uri=#{api[:canvas_page]}?sid=#{params[:sid]}"   
    end
  end

  # logged out? Returns true if user is logged out.
  def loggedout?
     token.nil?
  end

  # get facebok user data
  def getUser
    user = RestClient.get 'https://graph.facebook.com/me/',
                          {:params => {:access_token => token}}
    ActiveSupport::JSON.decode user   
  end

  # get user's facebook friends
  def getFriends
    friends = RestClient.get 'https://graph.facebook.com/me/friends/',
                             {:params => {:access_token => token}}
    ActiveSupport::JSON.decode friends
  end

  # get online friends
  def getOnlineFriends

    fql = "SELECT name, uid, first_name, profile_url, pic_square " +
          "FROM user WHERE online_presence " +
          "IN ('active','idle') AND uid IN " +
          "(SELECT uid2 FROM friend WHERE uid1=me()" +
          "ORDER BY rand()" + ")"

    facebook_api_fql_url = "https://api.facebook.com/method/fql.query"

    friends = RestClient.get facebook_api_fql_url,
                            {:params => {:access_token => token,
                                         :query => fql,
                                         :format => "JSON"}}
    ActiveSupport::JSON.decode friends
  end

  # get all friends in random order
  def getFriendsRandom
    fql = "SELECT name, uid, first_name, profile_url, pic_square " +
          "FROM user WHERE uid " +
          " IN (SELECT uid2 FROM friend WHERE uid1 = me())"  +
          "ORDER BY rand()"

    facebook_api_fql_url = "https://api.facebook.com/method/fql.query"

    friends = RestClient.get facebook_api_fql_url,
                            {:params => {:access_token => token,
                                         :query => fql,
                                         :format => "JSON"}}
    ActiveSupport::JSON.decode friends
  end


  protected

  # get facebook token
  def getToken(params = {}) 
    # dataSignedRequest = decode_facebook_hash(params["signed_request"])
    dataSignedRequest = getSignedRequest(params["signed_request"])
    token = dataSignedRequest["oauth_token"]
  end 

  # get signed request
  def getSignedRequest( signed_request )
    encoded_sig, payload = signed_request.split('.')
    data = ActiveSupport::JSON.decode base64_url_decode(payload)
                  # Don't bother with security of checking authenticity of token
    return data
  end

  # decode base 64 url
  def base64_url_decode str
      encoded_str = str.gsub('-','+').gsub('_','/')
      encoded_str += '=' while !(encoded_str.size % 4).zero?
      Base64.decode64(encoded_str)
  end
end
-----------------------------
file /application_controller.rb
require 'rubygems'
require 'facebook'

class ApplicationController < ActionController::Base
  protect_from_forgery

  protected

  # Login to facebook if user is not already logged in
  def facebook_login
    parameters = getParams params
    @fb = Facebook.new parameters

    session[:facebook_id] = @fb::facebook_user["id"] if !@fb::facebook_user.nil?

    if @fb.loggedout?
                # Redirect to facebook login
      render :facebooklogin, :layout => false
    elsif !User.registered? @fb
      User.register @fb
    end   
  end

  # Get Paramters for Facebook Access Token
  def getParams params

    if params[:signed_request]
      session[:parameters] = params
    else
      session[:parameters]
    end
  end 
end
anyone could help me ??