10

मैं कोमा को Omniauth के साथ काम करने की कोशिश कर रहा हूं। उपयोगकर्ता नाम Omniauth का उपयोग करके फेसबुक के साथ लॉग इन करता है और मैं ऐप का उपयोग कर रहे उपयोगकर्ता के दोस्तों की सूची खींचने के लिए कोआला को क्लाइंट के रूप में उपयोग करना चाहता हूं।कोमाला को 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 

डीबी स्कीमा

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 

उत्तर

12

है जैसे आप पहले से ही पता समस्या यह है कि यह है:

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

निम्नलिखित करने के लिए इसे बदलने का प्रयास करें:

0

बचें लेखन जब आप सिर्फ इस परीक्षण कर सकते हैं:

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

मैं का उपयोग किया है कि सफलता के साथ एक आधार के रूप में देता है।

मैंने कुछ मुद्दों को ठीक किया है लेकिन हालांकि गिटूब पर प्रतिबद्ध नहीं है। लेकिन वे बहुत मामूली हैं। मुझे लगता है कि उदाहरण ऐप काम करता है।

आपकी समस्या कोआला नहीं हो सकती है लेकिन तथ्य यह है कि टोकन सहेजा नहीं गया था, इसलिए आप कुछ भी पूछ नहीं सकते या फेसबुक से कनेक्ट भी नहीं कर सकते।

@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

आपका मुद्दा है कि आप कोआला में गलत बात गुजर रहे हैं लग रहा है fb_access_token केवल वर्तमान सत्र में उपलब्ध है और कोआला के लिए उपलब्ध नहीं है।

क्या आपके उपयोगकर्ता मॉडल में "टोकन" स्टोर करने के लिए एक कॉलम है? यदि नहीं, तो सुनिश्चित करें कि आपके पास उपयोगकर्ता मॉडल में वह कॉलम है। जब आपके पास उपयोगकर्ता मॉडल में वह कॉलम होता है, तो जब आप उपयोगकर्ता बनाते हैं तो आपको उसमें कुछ स्टोर करने की आवश्यकता होगी (उपयोगकर्ता वर्ग में create_with_omniauth विधि)। फेसबुक से सफल प्राधिकरण के बाद आपको यह पता होना चाहिए कि टोकन फ़ील्ड फेसबुक ओथ टोकन के साथ पॉप्युलेट है। यदि यह आबादी है, तो आपके कोआला कोड को काम करना चाहिए। इस मामले में सत्र में फेसबुक प्रमाण-पत्रों को स्टोर करने की आवश्यकता नहीं है।

यदि आपको फेसबुक से ऑफ़लाइन पहुंच नहीं मिल रही है (जिसका अर्थ है कि पहुंच केवल थोड़ी अवधि के लिए प्रदान की जाती है, तो सत्र में फेसबुक प्रमाण-पत्र संग्रहीत करना समझ में आता है। इस मामले में आपको "current_user.token" का उपयोग नहीं करना चाहिए। लेकिन सत्र ["fb_auth_token"] कोआला के साथ।

आशा है कि इससे मदद मिलती है!

तो तुम (फेसबुक प्राधिकरण के दीर्घकालिक भंडारण) ऑफ़लाइन पहुँच चाहते हैं, 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

क्योंकि आप create_with_omniauth विधि में टोकन भंडारण नहीं कर रहे हैं यह है। – Tom

संबंधित मुद्दे

 संबंधित मुद्दे