我正在尝试构建一个使用OmniAuth gem的应用程序,以便Twitter用户可以登录。
我按照教程中的步骤操作,我很确定,我已经做好了一切。
但是在Twitter的回调中我得到了这个臭名昭着的错误消息:
NoMethodError
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
在深入研究omniauth gem后,我发现,在OAuth#callback_phase
会话中不包含任何oauth信息
def callback_phase
::Rails.logger.info "session: #{session.inspect}"
::Rails.logger.info "consumer: #{consumer.inspect}"
request_token = ::OAuth::RequestToken.new(consumer, session['oauth'][name.to_s].delete('request_token'), session['oauth'][name.to_s].delete('request_secret'))
...
end
回拨后记录
session: {"session_id"=>"190523311f7a63fe796558691b1d4fff"}
consumer: #<OAuth::Consumer:0x103735b50 @secret="...", @http=#<Net::HTTP api.twitter.com:443 open=false>, @key="...", @options={:access_token_path=>"/oauth/access_token", :proxy=>nil, :http_method=>:post, :site=>"https://api.twitter.com", :request_token_path=>"/oauth/request_token", :scheme=>:header, :oauth_version=>"1.0", :signature_method=>"HMAC-SHA1", :authorize_path=>"/oauth/authenticate"}>
所以看起来会话内容在从twitter到omniauth的途中没有填充或丢失。
有没有人有想法,会导致什么行为?
我的Gemfile:
gem 'mongrel', '1.2.0.pre2'
gem "rails", "~> 3.0.6"
gem "mysql"
gem 'omniauth'
gem 'settingslogic' # config/application.yml
gem 'will_paginate', '~> 3.0.beta'
提前感谢您的帮助。
答案 0 :(得分:1)
在我的session_store.rb
我有这样的事情:
# Be sure to restart your server when you modify this file.
PersonalAccount::Application.config.session_store :cookie_store, :key => '_app_session', :secure => true
但在我的开发环境中,我没有使用SSL,因此OmniAuth无法存储其会话。
删除:secure => true
解决了我的问题。