Sha*_*rao 3 ruby facebook rubygems ruby-on-rails omniauth
我正在使用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
Run Code Online (Sandbox Code Playgroud)
用户模型是:
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
Run Code Online (Sandbox Code Playgroud)
我现在在大约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.)
Run Code Online (Sandbox Code Playgroud)
我看到最近在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
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 7
您可以在创建会话时更新令牌.
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5580 次 |
最近记录: |