nov / fb_graph

This gem doesn't support FB Graph API v2.0+. Please use fb_graph2 gem instead.
MIT License
1.04k stars 191 forks source link

How to get the user access token? #349

Closed jeena closed 10 years ago

jeena commented 10 years ago

I am trying to use fb_auth to post to my own timeline. To do that I need a user access token. I found, after quite some time, some code which would get me the token from https://github.com/nov/fb_graph#authentication (I wonder why this is not at the top of the readme):

client = fb_auth.client
client.authorization_code = params[:code]
access_token = client.access_token! :client_auth_body 

I see that params[:code] in my callback resource is a long string with characters and numbers, so it should be right. But the last line crashes my app with:

Rack::OAuth2::Client::Error

and this:

rack-oauth2 (1.0.7) lib/rack/oauth2/client.rb:119:in `handle_error_response'
rack-oauth2 (1.0.7) lib/rack/oauth2/client.rb:96:in `handle_response'
rack-oauth2 (1.0.7) lib/rack/oauth2/client.rb:70:in `access_token!'

I don't understand what is wrong, could someone explain to me what I am doing wrong?

nov commented 10 years ago

Inspect Rack::OAuth2::Client::Error. https://github.com/nov/rack-oauth2/blob/master/lib/rack/oauth2/client/error.rb

jeena commented 10 years ago

Ok thanks it seems after rewriting it like showed in some other example it started to work. Now I only have the problem that I can't figure out how to post public posts with feed!, the documentation doesn't say anything about that and if I just do `feed!(message: "hello world") it is only visible to my friends.

nov commented 10 years ago

You can pass any params as described in the official Graph API document in rubyish way. In this case, privacy object is what you need to set. https://developers.facebook.com/docs/graph-api/reference/user/feed/

So in rubyish way, it would be

me.feed!(
  message: 'Only to my friends',
  privacy: {
    value: :ALL_FRIENDS
  }
)