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.


You can use an app token instead of a user token to post to a wall as long as the user already granted the proper permissions to your application.

That is, in timeline

  • User grants app access to post with publish_actions
  • User access token supplied by Graph Login Flow
  • At this point, you can either use the user access token or application access token
  • In addition,

    foo = @graph.get_object('me')
    

    is not a POST request. It says, get the object from the graph named me . Further me will not resolve to anything if you are using an application token because there is no way for the application to tell which "me" in all the users in the app you are referring to. Thus you need to refer to the app scoped id for the user.

    eg

    foo = @graph.get_object('4')
    

    Where 4 is a numerical app scoped ID (4 will not work in your case you need to figure out the correct ID for your application). The correct call in koala will be something like

    foo = @graph.put_connections("4", "feed", :message => "I am writing on my wall!")


    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")
    
    链接地址: http://www.djcxy.com/p/32666.html

    上一篇: 来自事件的大图像?

    下一篇: 通过考拉发布到我自己的Facebook墙上