Search code examples
facebook-graph-apikoala

Posting to my own facebook wall via Koala


I am trying to post to my own facebook wall. So I created an "app" in my personal facebook page, and got the app_id, app_secret, etc.

I then did this code:

@oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
@token = @oauth.get_app_access_token
@graph = Koala::Facebook::API.new(@token)
foo = @graph.get_object('me')

However, I get this error:

An active access token must be used to query information about the current user. [HTTP 400] (Koala::Facebook::AuthenticationError)

The token is valid, I checked. I need to post to my OWN wall, not a different user's. From what I've read in the documentation, I need an "app access key", not a "user access key" to do this. I am somewhat new to the facebook api list, so I think I'm missing something very basic.


Solution

  • My problem was that the user (in this case myself) has to allow access to my app to post to my wall.

    The full OAuth process is described well at http://developers.facebook.com/docs/authentication/

    But specifically, I need to get a URL that I have to visit and then say "yes" to the authnetication question. The code is here:

    @oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
    @oauth.url_for_oauth_code(:permissions => "publish_actions")
    

    The URL will look something like this:

    https://www.facebook.com/dialog/oauth?
        client_id={app_id}&
        redirect_uri={redirect-uri}&scope=publish_actions
    

    Note, the URL has to specify what permissions you want to request from the user (in this case, permission to post to the wall). This permission request is specified under the "scope" variable. Note some version of the facebook api allow posting through the "publish_stream" scope and other versions require the "publish_actions" scope. More information about the permissions available under scope variable is available here: https://developers.facebook.com/docs/facebook-login/permissions/v2.0

    When you visit the URL that is generated from the above statement, facebook will give you a message asking if that particular app has permission to post to your wall. You, of course, say "yes". After that, your facebook app can post to the facebook wall using the "app access token"

    After that, it's easy to post to the wall with your app access token. The code that works for me is:

    @oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
    @app_access_token = @oauth.get_app_access_token
    @graph = Koala::Facebook::API.new(@app_access_token)
    foo = @graph.put_connections(facebook_user_id, "feed", :message => "Test message")