Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 439
megazord Avatar asked Sep 06 '13 18:09

megazord


People also ask

How do you post programmatically on Facebook?

Set the photo you want to upload as the source parameter (must be local to the server running the code) and make a POST request to /{page_id}/photos using the page access token (NOT the application access token!). So it would be along the lines of: FB. api('/{page_id}/photos', 'post', { source: 'path/to/image.

Is the Facebook API free?

In the newest version of the Graph API (v2. 9), we're announcing features that simplify development, making it even easier to build apps and experiences with Facebook. We're providing free access to over 140 million places around the world, the same data that powers Facebook, Instagram, and Messenger.


2 Answers

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.

like image 136
megazord Avatar answered Oct 07 '22 17:10

megazord


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

like image 38
Sahil Mittal Avatar answered Oct 07 '22 19:10

Sahil Mittal