OmniAuth Facebook过期令牌错误 [英] OmniAuth Facebook expired token error

查看:94
本文介绍了OmniAuth Facebook过期令牌错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 => 登录!
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验证访问令牌:会话与当前存储的会话不匹配,这可能是因为用户在会话创建之后更改了密码或者出于安全原因更改了会话。)

我看到过期的令牌问题最近在OmniAuth中得到解决:



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



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

唯一可行的解​​决方案是每当用户登录时创建一个新用户(我不喜欢这个解决方案):

  def创建
auth = request.env [omniauth.auth]
user = User.create_with_omniauth(auth)
session [:user_id] = user.id
redirect_to root_url,: notice => 登录!
end

谢谢!

  class SessionsController< / p> 

; ApplicationController
def create
auth = request.env [omniauth.auth]
user = User.find_by_provider_and_uid(auth [provider],auth [uid])。 | U |
u.update_attributes(:token => auth [credentials] [token])如果u
end || User.create_with_omniauth(auth)
session [:user_id] = user.id
redirect_to root_url,:notice => 登录!
end


I am using OmniAuth to get access to Facebook in my app. I am using the fb_graph gem: https://github.com/nov/fb_graph to post to Facebook. I am running omniauth-0.3.0 on Heroku for this app. The token that I save when the user is created is changed when the user logs in sometime later.

Code for creating user

    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 

The User model is:

  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

I am now seeing this error on about 30% users-

 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.)

I saw that the expired token issue has been recently fixed in OmniAuth:

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

I used this code which tries to refresh the access token. However, I still get the same error. Can someone point to what I am missing? Is there some other way I could update the token every time the user logs in?

The only solution which has worked is to create a new User everytime the User logs in (I don't like this solution at all):

  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

Thanks!

解决方案

You can simply update the token when you create the session.

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 Facebook过期令牌错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆