Ruby on rails 3 Rails OAutheException::必须使用活动访问令牌来查询有关当前用户的信息
Iam在我的rails应用程序中使用来获取和设置访问令牌(如果它通过facebook验证)。我有一个邀请控制器,用户必须通过身份验证才能邀请朋友。我的代码如下所示:Ruby on rails 3 Rails OAutheException::必须使用活动访问令牌来查询有关当前用户的信息,ruby-on-rails-3,facebook-oauth,fb-graph,Ruby On Rails 3,Facebook Oauth,Fb Graph,Iam在我的rails应用程序中使用来获取和设置访问令牌(如果它通过facebook验证)。我有一个邀请控制器,用户必须通过身份验证才能邀请朋友。我的代码如下所示: def facebook if params[:code] set_oauth_token(params[:code]) elsif params[:error] redirect_to landing_path, :alert => "Access Denied" return
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令牌无效
请帮我找到解决办法。非常感谢 最后我得到了一个错误,那就是在回调之后,你必须再次点击facebook才能获得oauth令牌。我已经修改了我的代码,它工作了 用户模型 用户控制器
您以前是否编写过类似的代码,而且还可以使用?@kspacja我找到了解决方案并发布了我的答案。你可以查一下。谢谢@swati:)。。这真的很有帮助。
def self.auth(redirect_url)
FbGraph::Auth.new(ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], :redirect_uri => redirect_url)
end
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?
client = User.auth(invites_facebook_url).client
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
end
private
def set_oauth_token(token)
client = User.auth(invites_facebook_url).client
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