2013-10-16 31 views
1

我使用的設計加上使用omniauth-google-oauth2寶石的應用ominauth電子郵件等什麼,但我的身份驗證哈希只與電子郵件,即使這回是我的範圍:我沒有得到比從omniauth - 谷歌 - 的oauth2

config.omniauth :google_oauth2, GOOGLE_ID, GOOGLE_SECRET, 
{ 
    :scope => "userinfo.email, userinfo.profile, plus.me", 
    :prompt => "select_account consent", 
    :image_aspect_ratio => "square", 
    :image_size => 50 
} 

這是我的config\initializers\devise.rb文件。

我可以用Google登錄它顯示這些是我的應用程序需要的權限,但我在回調中獲得的所有內容都是電子郵件。

任何想法?

回答

0

試試這個本應該爲你工作...........

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"], 
    { 
     :name => "google", 
     :scope => "userinfo.email, userinfo.profile, plus.me, http://gdata.youtube.com", 
     :prompt => "select_account", 
     :image_aspect_ratio => "square", 
     :image_size => 50 
    } 
end 

對於色器件,你也應該確保你有一個OmniAuth回調控制器設置

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

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

並綁定到或創建我使用的用戶

def self.find_for_google_oauth2(access_token, signed_in_resource=nil) 
    data = access_token.info 
    user = User.where(:email => data["email"]).first 

    unless user 
     user = User.create(name: data["name"], 
      email: data["email"], 
      password: Devise.friendly_token[0,20] 
      ) 
    end 
    user 
end 
+0

「:omniauthable」與裝置和後我不需要任何的YouTube控制。 – thealbinosmurf

+0

@thealbinosmurf你在建立一個應用程序嗎? –

+0

@Mehta不是移動應用 – thealbinosmurf