0

我将为我的 rails 应用程序实现 facebook 和 vkontakte 登录(注册),并且使用 facebook 一切正常(登录和重新登录都可以),按照手册 here完成

但是由于 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

怎么了?

4

2 回答 2

1

你试过 Ryan Bates 制作的omniauth railscasts 吗?

http://railscasts.com/episodes/360-facebook-authentication - 这里是 facebook

http://railscasts.com/episodes/235-omniauth-part-1 http://railscasts.com/episodes/236-omniauth-part-2

这些用于来自不同社交网络的身份验证。我开始使用 VK api,所以如果您在实施这些 railscast 时遇到问题 - 请告诉我。

于 2012-11-07T08:16:38.963 回答
1

检查您的服务器主机是否像在 vk 中注册的一样。对于开发,您可以编辑 /etc/hosts 并通过此主机访问您的站点。

示例:站点是 inmyhouse.su

/etc/hosts: 127.0.1.1 local.inmyhouse.su

于 2012-12-14T21:53:26.563 回答