Reputation: 1275
Iam using fb_graph in my rails application to get and set the access token if it authenticates with facebook. I have an Invites controller where a user will have to authenticate to invite friends. My code looks like this:
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
But Iam getting the error:
FbGraph::InvalidRequest at /invites/facebook
OAuthException :: An active access token must be used to query information about the current user.
The error is at the following line:
user = FbGraph::User.me(access_token).fetch
I tried to look for a solution and modified the code but still couldn't able to resolve the problem. Its all that the oauth token is not valid.
Please help me find a solution. Many thanks!!
Upvotes: 0
Views: 1339
Reputation: 1275
Finally I got the error wchich was that after the callback you have to hit the facebook again to get the oauth token. I have modified my code and it worked.
User model
def self.auth(redirect_url)
FbGraph::Auth.new(ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], :redirect_uri => redirect_url)
end
User controller
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
Upvotes: 1