我知道这个过期 token 的问题已经被问过很多次了。但是我找不到适合我情况的。基本上我想向用户发出 fb apprequest,无论他/她在 Facebook 上是在线还是离线。
此外,我对 Facebook 文档感到困惑。如果有人能回答我下面的问题,我将不胜感激。
Facebook 访问 token 在用户退出 Facebook 后立即过期,除非它是长期 token ?
假设用户 A 访问 token 已过期。我是否可以在用户 A 不返回我的 Canvas 应用程序的情况下获得新的访问 token ?
当我对用户进行身份验证时,我会得到一个短期 token ,然后我可以根据需要延长有效期,还是默认情况下我会得到一个长期 token ?
下面是我使用 Koala 发出 fb 请求的代码:
begin
graph = Koala::Facebook::API.new(access_token)
graph.put_object("me", "apprequests", {:message => "..."})
rescue Koala::Facebook::APIError
# Assume a user has a short-lived token and hasn't visit my application for a long time.
# Is it possible to get a NEW long-lived token here without the user going to my application again
# (assume the user did not remove my application) ? If Yes, how to do that using Koala ?
end
谢谢!
最佳答案
我遇到了 this post它改编了 Railscast on Facebook 中的代码展示如何将短期 token 换成 60 天的 token :
用户.rb
def self.from_omniauth(auth)
# immediately get 60 day auth token
oauth = Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"])
new_access_info = oauth.exchange_access_token_info auth.credentials.token
new_access_token = new_access_info["access_token"]
new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.image = auth.info.image
user.email = auth.info.email
user.oauth_token = new_access_token #originally auth.credentials.token
user.oauth_expires_at = new_access_expires_at #originally Time.at(auth.credentials.expires_at)
user.save!
end
end
关于ruby-on-rails - 尝试使用 Koala 进行 fb apprequest,但访问 token 已过期。我该如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924906/