2012-09-05 45 views
0

我要實現我的Rails應用程序Facebook和VKontakte等登錄(註冊)的麻煩,並與Facebook一切正常(登錄名和regitering是確定),如手工完成 hereRails的omniauth,並制定與VK

但隨着VKontakte等一些錯誤,我仍然會得到 「空」

這裏是我的代碼:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 

    if @user.persisted? 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" 
     sign_in_and_redirect @user, :event => :authentication 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    def vkontakte 
    @user = User.find_for_vkontakte_oauth(request.env["omniauth.auth"], current_user) 
    if @user.persisted? 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Vkontakte" 
     sign_in_and_redirect @user, :event => :authentication 
    else 
     session["devise.vkontakte_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
end 

和型號:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, 
     :token_authenticatable, :lockable, :timeoutable, :omniauthable #:confirmable, 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :provider, :uid, :name 
def self.find_for_facebook_oauth(auth, signed_in_resource=nil) 
    user = User.where(:provider => auth.provider, :uid => auth.uid).first 
    unless user 
     user = User.create(name:auth.extra.raw_info.name, 
          provider:auth.provider, 
          uid:auth.uid, 
          email:auth.info.email, 
          password:Devise.friendly_token[0,20] 
          ) 
    end 
    user 
    end 

    def self.new_with_session(params, session) 
    super.tap do |user| 
     if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 
     user.email = data["email"] if user.email.blank? 
     end 
     if data = session["devise.vkontakte_data"] && session["devise.vkontakte_data"]["extra"]["raw_info"] 
     user.email = data["email"] if user.email.blank? 
     end 
    end 
    end 



    def self.find_for_vkontakte_oauth(auth, signed_in_resource=nil) 
    user = User.where(:provider => auth.provider, :uid => auth.uid).first 
    unless user 
     user = User.create(name:auth.info.name, 
          provider:auth.provider, 
          uid:auth.uid, 
          email:auth.extra.raw_info.domain+'@vk.com', 
          password:Devise.friendly_token[0,20] 
          ) 
    end 
    user 
    end 

怎麼了?

回答

1

檢查您的服務器主機是否像在vk中註冊一樣。對於開發,您可以編輯/ etc/hosts並通過該主機訪問您的站點。

例如:網站是inmyhouse.su

/etc/hosts文件:127.0.1.1 local.inmyhouse.su