10

Tôi đang cố gắng để Koala làm việc với Omniauth. Mô hình người dùng đăng nhập bằng Facebook bằng Omniauth và tôi muốn sử dụng Koala làm ứng dụng khách để kéo danh sách bạn bè của người dùng đang sử dụng ứng dụng. Tôi dường như không được lưu thẻ đúng cách:Làm thế nào để Koala chơi độc đáo với Omniauth?

khiển

@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 Schema

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 

mô hình Người dùng có

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 initializer có:

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 

phiên điều khiển có:

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 

Trả lời

12

Vấn đề như bạn đã biết là fb_access_token chỉ có sẵn trong phiên hiện tại và không có sẵn cho Koala.

Mô hình người dùng của bạn có cột để lưu "mã thông báo" không? Nếu không, hãy đảm bảo bạn có cột đó trong mô hình người dùng. Khi bạn có cột đó trong mô hình người dùng, bạn sẽ cần phải lưu trữ một cái gì đó trong đó tại thời điểm bạn tạo người dùng (phương thức create_with_omniauth trong lớp User). Sau khi ủy quyền thành công từ facebook, bạn sẽ thấy rằng trường mã thông báo được điền bằng mã thông báo facebook oauth. Nếu nó được phổ biến, thì mã Koala của bạn sẽ hoạt động. Trong trường hợp này, không cần phải lưu trữ thông tin đăng nhập facebook trong phiên.

Nếu bạn không truy cập ngoại tuyến từ Facebook (điều này có nghĩa là quyền truy cập chỉ được cung cấp trong một thời gian ngắn, khi đó lưu trữ thông tin đăng nhập facebook trong phiên làm việc có ý nghĩa. Trong trường hợp này, bạn không nên sử dụng "current_user.token" nhưng phiên ["fb_auth_token"] thay thế bằng Koala.

Hy vọng điều này sẽ hữu ích!

Vì vậy, nếu bạn muốn truy cập ẩn (lưu trữ dài hạn uỷ quyền facebook), thay đổi mã mô hình của bạn để lưu trữ fb_auth_token như sau

# 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 

Nếu bạn có quyền truy cập ngắn hạn sau đó thay đổi "khác" điều khiển của bạn để sử dụng phiên

# 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

Tránh viết khi bạn chỉ có thể kiểm tra này ra:

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

Tôi đã sử dụng rằng ứng dụng như một cơ sở với sự thành công.

Tôi đã khắc phục một số sự cố nhưng chưa cam kết về github. Nhưng những thứ đó rất nhỏ. Tôi nghĩ ứng dụng ví dụ hoạt động.

Sự cố của bạn có thể không phải là Koala nhưng thực tế là mã thông báo không được lưu để bạn không thể truy vấn bất kỳ điều gì hoặc thậm chí kết nối với Facebook.

0

vấn đề của bạn có vẻ như là bạn đang đi qua những điều sai trái vào Koala:

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

Hãy thử thay đổi nó như sau:

@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

Đó là vì bạn không lưu trữ mã thông báo trong phương thức create_with_omniauth. – Tom

Các vấn đề liên quan