10

我試圖讓考拉與Omniauth一起工作。用戶模型使用Omniauth使用Facebook登錄,我想使用Koala作爲客戶端來拉取正在使用該應用程序的用戶朋友列表。我似乎沒有被正確保存令牌:如何讓考拉玩Omniauth?

控制器

@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 

會話控制器具有:

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

我已經使用該應用與成功的基礎:

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 

回答

12

就像你已經知道的問題是, fb_access_token僅在當前會話中可用並且不可供Koala使用。

您的用戶模型是否有存儲「令牌」的列?如果不是,那麼確保在用戶模型中有該列。在用戶模型中有該列時,您需要在創建用戶時在其中存儲一些內容(User類中的create_with_omniauth方法)。在經過facebook的成功授權後,您應該發現令牌字段已填充了facebook oauth標記。如果它被填充,那麼你的考拉代碼應該工作。在這種情況下,不需要在會話中存儲Facebook憑證。

但是,如果您沒有從Facebook獲得離線訪問(這意味着訪問只提供了很短的時間,那麼將facebook憑據存儲在會話中是合理的,在這種情況下,您不應該使用「current_user.token」但會議[「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 
0

避免書寫時,你可以測試了這一點。

我修復了一些問題,但還沒有在github上提交。但這些都很小。我認爲示例應用程序的作品。

你的問題可能不是考拉,但事實上,令牌沒有保存,所以你不能查詢任何事情甚至連接到Facebook。

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 
+0

這是因爲您沒有將該令牌存儲在create_with_omniauth方法中。 – Tom