我在我的 rails 应用程序中使用fb_graph来获取和设置访问令牌(如果它通过 facebook 进行身份验证)。我有一个邀请控制器,用户必须在其中进行身份验证才能邀请朋友。我的代码如下所示:
def facebook
if params[:code]
set_oauth_token(params[:code])
elsif params[:error]
redirect_to landing_path, :alert => "Access Denied"
return
end
if current_user.oauth_token.nil?
redirect_to client.authorization_uri(
:scope => "user_about_me, email, publish_stream, user_location, user_interests, user_birthday, user_likes, user_hometown, offline_access"
)
end
private
def set_oauth_token(token)
client.authorization_code = params[:code]
access_token = client.access_token! :client_auth_body
user = FbGraph::User.me(access_token).fetch
current_user.oauth_token = access_token
current_user.save(:validate => false)
end
def client
FbGraph::Auth.new(ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], :redirect_uri => invites_facebook_url).client
end
但我得到了错误:
FbGraph::InvalidRequest at /invites/facebook
OAuthException :: An active access token must be used to query information about the current user.
错误在以下行:
user = FbGraph::User.me(access_token).fetch
我试图寻找解决方案并修改了代码,但仍然无法解决问题。oauth 令牌无效。
请帮我找到解决方案。非常感谢!!