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
Đó là vì bạn không lưu trữ mã thông báo trong phương thức create_with_omniauth. – Tom