2011-11-26 63 views
0

有沒有人有與OmniAuth的經驗& Facbeook?我剛剛從我的應用程序中使用硬編碼應用程序ID &密鑰移至ENV['FACEBOOK_APP_ID']方法。我知道這些值是正確的,因爲當我嘗試登錄到我的應用程序時,我得到了正確應用程序的權限對話框,但是當Facebook重定向到開發模式下的應用程序時,出現錯誤「身份驗證錯誤:無效證書」。OmniAuth與Facebook的發展失敗

然而,在生產中,對於Facebook應用程序設置的生產版本,一切正常。

# initializers/omniauth.rb 
Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], :scope => 'email,offline_access,user_work_history' 
end 

#sessions_controller.rb 
class SessionsController < ApplicationController 
    def new 
    redirect_to '/auth/facebook' 
    end 

    def callback 
    auth = request.env["omniauth.auth"] 
    logger.info auth.inspect 
    user = User.where(:provider => auth['provider'], :uid => auth['uid']).first || User.create_with_omniauth(auth) 
    session[:user_id] = user.id 
    session[:token] = auth['credientials']['token'] 
    redirect_to root_url, :notice => "Signed in!" 
    end 

    def destroy 
    session[:user_id] = nil 
    # current_user = nil 
    # request.env['omniauth.auth'] = nil 
    redirect_to root_url, :notice => 'Signed out!' 
    end 

    def failure 
    logger.info request.inspect 
    redirect_to root_url, :alert => "Authentication error: #{params[:message].humanize}" 
    end 

    def auth; request.env['omniauth.auth'] end 
end 

#user.rb 
    # Authentication Stuff 
class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    # ... 
    def self.create_with_omniauth(auth) 
    begin 
     create! do |user| 
     user.provider = auth['provider'] 
     user.uid = auth['uid'] 
     if auth['info'] 
      user.name = auth['info']['name'] if auth['info']['name'] # Twitter, Google, Yahoo, GitHub 
      user.email = auth['info']['email'] if auth['info']['email'] # Google, Yahoo, GitHub 
     end 
     if auth['extra']['raw_info'] 
      user.name = auth['extra']['raw_info']['name'] if auth['extra']['raw_info']['name'] # Facebook 
      user.email = auth['extra']['raw_info']['email'] if auth['extra']['raw_info']['email'] # Facebook 
      user.employer = auth['extra']['raw_info']['work'][0]['employer'] if auth['extra']['raw_info']['work'] # Facebook 
     end 
     end 
     rescue Exception 
     raise Exception, "cannot create user record" 
    end 
    end 
end 

然後出現這種情況:

Started GET "/auth/facebook" for 127.0.0.1 at 2011-11-26 11:53:38 +0200 

Started GET "/auth/facebook/callback?code=AQBapjqIJixqmSxjj-i61WJtJN-ncCBUM_mPyhunqY4esQsaX7wiU794wMSOWT6oRJ0TMl-N5eqNh2MHuap0Oey4i6ef0F8281zQ6V1Vhct3g" for 127.0.0.1 at 2011-11-26 11:53:40 +0200 

Started GET "/auth/failure?message=invalid_credentials" for 127.0.0.1 at 2011-11-26 11:53:42 +0200 
    Processing by SessionsController#failure as HTML 
    Parameters: {"message"=>"invalid_credentials"} 
Redirected to http://localapp.dev/ 
Completed 302 Found in 1ms 

有任何想法,我缺少的是什麼?我要強調的是,我這個錯誤之前所做的唯一的變化是不是在我ombiauth.rb初始化有provider :facebook, "XXXXXX", "XXXXXXXXXXXXXXX",我把它從ENV

獲取這些值-Avishai

# Gemfile 
source 'http://rubygems.org' 

gem 'rails', '3.0.9' 
gem "airbrake" 
gem "bson_ext" 
gem "mongoid", ">= 2.0.2" 
gem "omniauth", "~> 1.0" 
gem "omniauth-facebook" 
gem "carrierwave", "0.5.4" 
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid' 
gem "geocoder" 
gem 'will_paginate' 
gem "rmagick" 
gem "jquery-rails" 
gem "mongoid-history" 
gem "mongoid-rating"  
gem "flash_cookie_session" 
group :development, :test do 
    gem "ya2yaml" 
    gem "chronic" 
end 
+0

描述您的開發環境。(系統,捆綁列表)。 – rogal111

+0

我們需要看代碼.. – user482594

+0

我認爲如果回調網址不是您指定的那個,它可能不會來自http://localapp.dev/ – pguardiario

回答

2

你實際定義ENV在您的本地應用程序中?這通常是在部署到heroku時執行的操作,但如果您忘記在本地設置變量,那麼當您運行dev版本時,它們將不會在那裏。

+0

是的,我定義了它,但是我剛在定義文件中發現了一個語法錯誤,導致FACEBOOK_SECRET_KEY被髮送爲零。哎呀.. – Avishai

1

由於Karmajunkie是說,你有一個Facebook.yml文件來定義您的憑證,包括:

production: 
    app_id: '12341234etcetcetc' 
    secret_key: '123456788etcetcetc' 
    callback_url: http://your_app_url.heroku.com/ 
相關問題