我在Ruby on Rails应用程序上使用Koala gem
我在模型上有下面的代码,我用它来通过Koala获取数据:
@graph = Koala::Facebook::GraphAPI.new(token_secret)
friends = @graph.get_connections("me", "friends")其中token_secret来自我的users表中的一个字段,保存在登录时。
它工作得很好,但几分钟后我得到:
Koala::Facebook::APIError (OAuthException: Error validating access token: Session has expired at unix time 1327438800. The current unix time is 1327442037.):我在前面找到了用Facebook JS SDK中的方法更新这个令牌的方法,但是这个获取好友列表的方法是在控制器上调用的。
如何使用考拉续费token_secret?这个是可能的吗?
发布于 2013-05-28 10:51:55
我想我应该回答这个问题,因为这是我刚刚遇到的需要做的事情。
Koala不久前添加了对交换访问令牌的支持,如下所示:https://github.com/arsduo/koala/pull/166
因此,我的用户模型现在具有如下内容:
def refresh_facebook_token
# Checks the saved expiry time against the current time
if facebook_token_expired?
# Get the new token
new_token = facebook_oauth.exchange_access_token_info(token_secret)
# Save the new token and its expiry over the old one
self.token_secret = new_token['access_token']
self.token_expiry = new_token['expires']
save
end
end
# Connect to Facebook via Koala's oauth
def facebook_oauth
# Insert your own Facebook client ID and secret here
@facebook_oauth ||= Koala::Facebook::OAuth.new(client_id, client_secret)
end发布于 2012-02-19 05:55:44
如果您正在尝试获取Facebook网站应用程序的oauth_token,则需要使用基于重定向的Oauth过程。这有点复杂。对于canvas应用程序来说,它更简单。您仍然可以对canvas应用程序使用基于重定向的过程,但最好从signed_request解析它。
每次用户在Facebook中加载你的应用程序时,他们都会登陆你的第一个带有"signed_request“参数的页面。这个加密的字符串必须在控制器中使用Koala对象进行解析。从结果中,您可以获得一个新的oauth_token,它应该在大约两个小时内有效。我是这样做的。
#Create a new koala Oauth object.
oauth = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET)
#Get the new oauth_token from the signed request.
your_new_oauth_token = oauth.parse_signed_request(params[:signed_request])["oauth_token"]https://stackoverflow.com/questions/8995035
复制相似问题