有没有人有与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
描述您的开发环境。(系统,捆绑列表)。 – rogal111
我们需要看代码.. – user482594
我认为如果回调网址不是您指定的那个,它可能不会来自http://localapp.dev/ – pguardiario