megazord
megazord

Reputation: 3210

How to programmatically post to my own wall on Facebook?

I'm trying to make a simple program to post something to my wall on Fcebook in Python. I tried using this example from pythonforfacebook:

import facebook
oauth_access_token = "****"
graph = facebook.GraphAPI(oauth_access_token)
me = "****"
profile = graph.get_object(me)
graph.put_object(me, "feed", message="I am writing on my wall!")

I tried going to the developer center and making an App. For "Select how your app integrates with Facebook",I chose "Website with Facebook Login" (I don't actually have a website, I just have a program that should post to my facebook wall, but this is the closest option that matches what I want to do...).

However, when I run this, it says:

$ python a.py 
Traceback (most recent call last):
  File "a.py", line 7, in <module>
    graph.put_object(me, "feed", message="I am writing on my wall!")
  File "/usr/lib64/python2.7/site-packages/facebook.py", line 140, in put_object
    post_args=data)
  File "/usr/lib64/python2.7/site-packages/facebook.py", line 298, in request
    raise GraphAPIError(response)
facebook.GraphAPIError: (#200) The user hasn't authorized the application to perform this action

How can I log in as the user and authorize my App? Am I even taking the right approach? I don't actually have a website, I just have a program, for the website field I literally entered "http://google.com" and it worked.

Upvotes: 6

Views: 3157

Answers (3)

Nishat Baig
Nishat Baig

Reputation: 176

For those of who stumble upon this post after April 24th, Facebook has stopped support for such API calls.

As of April 24,2018, the publish_actions permission has been removed. Please see the Breaking Changes Changelog for more details. To provide a way for your app users to share content to Facebook, we encourage you to use our Sharing products instead.

Upvotes: 1

Sahil Mittal
Sahil Mittal

Reputation: 20753

You must authorize the user for publish_action/publish_stream permission to post on your wall.


  • If your only task is to post on the wall/ get the basic info; you can use the APP Access Token instead of user access token for performing the action once you've authorized the app. To get the App Access Token, you can uuse-

    GET /oauth/access_token?
      client_id={app-id}
      &client_secret={app-secret}
      &grant_type=client_credentials
    

    Or, directly from here. But do not hard code it in your app (for security reasons)

  • Or, if you want to perform other actions too on the user's behalf you can extend the user token to 2 months validity. Read here for more details

Upvotes: 2

megazord
megazord

Reputation: 3210

I made it work by getting a new token with permissions to post on my wall (specifically, the "publish_actions" permission). I did this by using the Graph API Explorer, selecting my application in the dropdown menu, pressing "Get Access Token" and checking "publish_actions". The only problem is the token expires after an hour.

Upvotes: 3

Related Questions