【问题标题】:Empty Session after Omniauth Twitter CallbackOmniauth Twitter 回调后的空会话
【发布时间】:2011-05-25 03:15:00
【问题描述】:

我正在尝试构建一个使用 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 的过程中没有填充或丢失。

有没有人知道,什么会导致这种行为?

我的宝石文件:

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'

提前感谢您的帮助。

【问题讨论】:

  • 其实,是的。经过漫长而令人沮丧的搜索,我从 session_store 配置中删除了 :secure => true 。这很有意义,因为我不使用 SSL。

标签: ruby-on-rails twitter omniauth


【解决方案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 =&gt; true 解决了我的问题。

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 2012-05-21
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多