10

KoalaをOmniauthで動作させようとしています。ユーザーモデルはOmniauthを使用してFacebookにログインし、コアラをクライアントとして使用して、アプリを使用しているユーザーの友達のリストを取得したいと思います。トークンを適切に保存していないようです:

コントローラ

@friends = Array.new
 if current_user.token
   graph = Koala::Facebook::GraphAPI.new(current_user.token)
   @profile_image = graph.get_picture("me")
   @fbprofile = graph.get_object("me")
   @friends = graph.get_connections("me", "friends")
end

DBスキーマ

create_table "users", :force => true do |t|
  t.string   "provider"
  t.string   "uid"
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "token"
end

ユーザーモデルは

def self.create_with_omniauth(auth)  
  create! do |user|  
    user.provider = auth["provider"]  
    user.uid = auth["uid"]  
    user.name = auth["user_info"]["name"]  
  end  
end

Koala.rb初期化子には次のものがあります。

module Facebook
  CONFIG = YAML.load_file(Rails.root.join("config/facebook.yml"))[Rails.env]
  APP_ID = CONFIG['app_id']
  SECRET = CONFIG['secret_key']
end

Koala::Facebook::OAuth.class_eval do
  def initialize_with_default_settings(*args)
    case args.size
      when 0, 1
        raise "application id and/or secret are not specified in the config" unless Facebook::APP_ID && Facebook::SECRET
        initialize_without_default_settings(Facebook::APP_ID.to_s, Facebook::SECRET.to_s, args.first)
      when 2, 3
        initialize_without_default_settings(*args) 
    end
  end 

  alias_method_chain :initialize, :default_settings 
end

セッションコントローラには次のものがあります。

  def create  
    auth = request.env["omniauth.auth"]  
    user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
    session[:user_id] = user.id  

    session['fb_auth'] = request.env['omniauth.auth']
    session['fb_access_token'] = omniauth['credentials']['token']
    session['fb_error'] = nil

    redirect_to root_url 
  end  
4

3 に答える 3

12

すでにご存知のように、fb_access_tokenは現在のセッションでのみ利用可能であり、コアラでは利用できないという問題があります。

ユーザーモデルに「トークン」を格納する列がありますか?そうでない場合は、ユーザーモデルにその列があることを確認してください。ユーザーモデルにその列がある場合、ユーザーを作成するときに何かをその列に格納する必要があります(Userクラスのcreate_with_omniauthメソッド)。Facebookからの承認が成功すると、トークンフィールドにfacebookoauthトークンが入力されていることがわかります。入力されている場合は、コアラコードが機能するはずです。この場合、Facebookのクレデンシャルをセッションに保存する必要はありません。

ただし、Facebookからオフラインアクセスを取得していない場合(つまり、アクセスが短時間しか提供されない場合は、Facebookの資格情報をセッションに保存するのが理にかなっています。この場合、「current_user.token」ではなくsession["を使用する必要があります。 fb_auth_token"]代わりにコアラを使用します。

お役に立てれば!

したがって、オフラインアクセス(Facebook認証の長期保存)が必要な場合は、モデルコードを変更してfb_auth_tokenを次のように保存します

# User model
def self.create_with_omniauth(auth)  
  create! do |user|  
    user.provider = auth["provider"]  
    user.uid = auth["uid"]  
    user.name = auth["user_info"]["name"]  
    user.token = auth['credentials']['token']
  end  
end

# SessionsController
def create  
  auth = request.env["omniauth.auth"]  
  user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
  # Note i've also passed the omniauth object      

  session[:user_id] = user.id  
  session['fb_auth'] = auth
  session['fb_access_token'] = auth['credentials']['token']
  session['fb_error'] = nil

  redirect_to root_url 
end 

短期間のアクセスがある場合は、セッションを使用するように「他の」コントローラーを変更してください

# The other controller
def whateverthissactionis
  @friends = Array.new
  if session["fb_access_token"].present?
    graph = Koala::Facebook::GraphAPI.new(session["fb_access_token"]) # Note that i'm using session here
    @profile_image = graph.get_picture("me")
    @fbprofile = graph.get_object("me")
    @friends = graph.get_connections("me", "friends")
  end
end
于 2011-08-11T10:14:14.917 に答える
0

これをテストできる場合は、書き込みを避けてください。

https://github.com/holden/devise-omniauth-example/blob/master/config/initializers/devise.rb

私はそのアプリを基礎として成功裏に使用しました。

いくつかの問題を修正しましたが、githubにはコミットしていません。しかし、それらは非常にマイナーです。サンプルアプリは機能すると思います。

あなたの問題はコアラではないかもしれませんが、トークンが保存されていないため、何もクエリしたり、Facebookに接続したりすることはできません。

于 2011-08-10T14:29:19.010 に答える
0

あなたの問題は、あなたがコアラに間違ったものを渡していることであるように見えます:

if @token = current_user.token
   @graph = Koala::Facebook::GraphAPI.new(oauth_callback_url)
   @friends = @graph.get_connections("me", "friends")
end

次のように変更してみてください。

@friends = Array.new # I think it returns a straight array, might be wrong.
if current_user.token
   graph = Koala::Facebook::GraphAPI.new(current_user.token)
   @friends = graph.get_connections("me", "friends")
end
于 2011-08-10T15:50:43.773 に答える