【问题标题】:OmniAuth Facebook expired token errorOmniAuth Facebook 令牌过期错误
【发布时间】:2011-11-29 13:07:43
【问题描述】:

我正在使用 OmniAuth 在我的应用程序中访问 Facebook。我正在使用 fb_graph gem:https://github.com/nov/fb_graph 发布到 Facebook。我在 Heroku 上为这个应用程序运行 omniauth-0.3.0。我在创建用户时保存的令牌会在用户稍后登录时更改。

创建用户的代码

    class SessionsController < ApplicationController  
    def create  
     auth = request.env["omniauth.auth"]  
     user = User.find_by_provider_and_uid(auth["provider"], auth["uid"])||           
     User.create_with_omniauth(auth)
       session[:user_id] = user.id  
       redirect_to root_url, :notice => "Signed in!"  
         end 

用户模型是:

  def self.create_with_omniauth(auth)  
    create! do |user|  
    user.provider = auth["provider"]  
    user.uid = auth["uid"]  
    user.name = auth["user_info"]["name"] 
    user.token = auth["credentials"]["token"]
    end
   end

我现在在大约 30% 的用户身上看到了这个错误 -

 FbGraph::InvalidToken (OAuthException :: Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.)

我看到最近在 OmniAuth 中修复了过期令牌问题:

https://github.com/soopa/omniauth/commit/67bdea962e3b601b8ee70e21aedf5e6ce1c2b780

我使用了这段代码来尝试刷新访问令牌。但是,我仍然遇到同样的错误。有人可以指出我所缺少的吗? 是否有其他方法可以在用户每次登录时更新令牌?

唯一有效的解决方案是每次用户登录时创建一个新用户(我根本不喜欢这个解决方案):

  def create  
    auth = request.env["omniauth.auth"] 
    user = User.create_with_omniauth(auth)
    session[:user_id] = user.id  
    redirect_to root_url, :notice => "Signed in!"  
  end

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby facebook rubygems omniauth


    【解决方案1】:

    您可以在创建会话时简单地更新令牌。

    class SessionsController < ApplicationController  
    def create  
      auth = request.env["omniauth.auth"]  
      user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]).tap do |u|
               u.update_attributes(:token => auth["credentials"]["token"]) if u
             end || User.create_with_omniauth(auth)
      session[:user_id] = user.id  
      redirect_to root_url, :notice => "Signed in!"  
    end 
    

    【讨论】:

    • 感谢您的回复。我有一个问题 - 我正在使用omniauth 使用FB 登录。 FB 令牌会不时更改,因此当用户在登录后很长时间使用应用程序时,令牌可能会更改。为了遇到这种情况,我现在正在使用“offline_access”权限,以便 FB 生成长期令牌。有没有办法在没有“offline_access”的情况下解决这个问题。在发布到 Facebook 之前(登录后的任何时间),它可能需要我的应用程序通过omniauth 请求更新令牌。有没有其他方法可以解决这个问题?
    • 根据我的经验,只有在设置了“offline_access”的情况下令牌发生更改是用户在 Facebook 上更改密码时。除此之外,代币在一年多的时间里保持不变。当密码更改时,他们必须再次登录才能获得新的令牌,如上。
    • 我同意你说的。使用“offline_access”设置,令牌没有改变。但是,我想知道如何在没有设置“offline_access”的情况下处理它? (“offline_access”是用户的附加权限,因此可能会导致一些用户不希望应用“随时访问他们的数据”)
    • 我看到了对 omniauth 的更改,它尝试刷新过期的令牌 - github.com/soopa/omniauth/commit/…。我不确定如何在我的代码中使用它。
    • 上一条评论中链接的新 URL:github.com/intridea/omniauth/commit/…
    【解决方案2】:

    在你回答这个问题之前我使用了类似的解决方案-

      class SessionsController < ApplicationController  
      def create  
     auth = request.env["omniauth.auth"]  
     user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) ||  User.create_with_omniauth(auth)  
    
     user.update_attributes(:token => auth["credentials"]["token"])
    
     session[:user_id] = user.id  
     redirect_to root_url, :notice => "Signed in!"  
    
      end
    

    【讨论】:

      【解决方案3】:

      在这种情况下,我们不能使用带有以下方法的 FBGraph gem 刷新令牌吗?

      auth = FbGraph::Auth.new(CLIENT_ID, CLIENT_SECRET)
      auth.exchange_token! access_token # Needs fb_graph 2.3.1+
      auth.access_token # => new token
      

      但是,这不会延长令牌的有效期,而是将令牌替换为新令牌。到期时间将保持不变。用 FB 检查过,他们可能不允许将 FB 令牌的有效期延长超过 60 天。 令牌的最长有效期为 60 天。

      参考:https://github.com/nov/fb_graph/wiki/Authentication#wiki-extend-token-expiry

      【讨论】:

        猜你喜欢
        • 2012-05-27
        • 2012-05-02
        • 1970-01-01
        • 2012-04-28
        • 1970-01-01
        • 2013-02-18
        • 1970-01-01
        • 2016-01-25
        • 1970-01-01
        相关资源
        最近更新 更多