8

I have tried with a sample code that I found on google..

import facebook

def main():
   # Fill in the values noted in previous steps here
    cfg = {
    "page_id"      : "XXXXXXXXXXXXXX",  # Step 1
    "access_token" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"   # Step 3
    }

    api = get_api(cfg)
    msg = "Hello, world!"
    status = api.put_wall_post(msg)

def get_api(cfg):
     graph = facebook.GraphAPI(cfg['access_token'])
     # Get page token to post as the page. You can skip 
     # the following if you want to post as yourself. 
     resp = graph.get_object('me/accounts')
     page_access_token = None
    for page in resp['data']:
        if page['id'] == cfg['page_id']:
            page_access_token = page['access_token']
    graph = facebook.GraphAPI(page_access_token)
    return graph

if __name__ == "__main__":
     main()

But I am getting this error:

AssertionError: Write operations require an access token on line status = api.put_wall_post(msg).

Can some one help me in solving the issue?

images

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
sowmya
  • 221
  • 1
  • 3
  • 15

3 Answers3

5

To write a post to facebook using python.we need access_token for it.

graph = facebook.GraphAPI(access_token="XXXXXXXX")
print graph
#to post to your wall
graph.put_object("me", "feed", message="Posting on my wall1!")
#to get your posts/feed
feed = graph.get_connections("me", "feed")
post = feed["data"]
print post
#to put comments for particular post id
graph.put_object(post["id"], "comments", message="First!")
sowmya
  • 221
  • 1
  • 3
  • 15
  • Could you do an example like this putting an object to one of the pages managed by the user ? – Liviu Dec 10 '18 at 09:42
2

Hope, the above code works fine if you provide Page Id and Access Token. Please follow below steps to get the access token and page Id.

  1. Go to the Graph API Explorer

2.Choose your app from the dropdown menu

3.Click "Get Access Token"

4.Choose the manage_pages permission (you may need the user_events permission too, not sure)

5.Now access the me/accounts connection and copy your page's access_token

6.Click on your page's id

7.Add the page's access_token to the GET fields

8.Call the connection you want (e.g.: PAGE_ID/events)

This topic was already discussed in Facebook Access Token for Pages

sandeep kumar
  • 43
  • 1
  • 6
  • Iam able to get acees token but failed to get page's id..please tell me how to get it on the screen @sandeep kumar – sowmya Feb 02 '18 at 09:49
  • 1
    To access your Facebook's Page ID: 1.Open your Facebook page. 2.Click the About tab. 3.Scroll down to the bottom of the Page Info section. 4.Next to Facebook Page ID, you can find your page ID. – sandeep kumar Feb 02 '18 at 10:08
  • I didnt find any page id in my facebook page – sowmya Feb 02 '18 at 10:14
  • find out about your page id with the graph api explorer then. you just need to make a call to the page name (it´s in the url). – andyrandy Feb 02 '18 at 11:14
1

Verified as of 2020 March:

!pip install facebook-sdk==2.0.0

then:

import facebook

def main():
   # Fill in the values noted in previous steps here
   cfg = {
   "page_id"      : "1xxxxx48480xxxx",  # Step 1
   "access_token" : "xxxxxxxxxxxxxxxxxxxxxxxnp3QApxv12gjGnV99BNnhxxxxxxxxxx"   # Step 3
    }

    api = get_api(cfg)
    msg = "Hello, world!"
    status = api.put_wall_post(msg)

def get_api(cfg):

    graph = facebook.GraphAPI(cfg['access_token'])
    resp = graph.get_object('me/accounts')
    page_access_token = None
    for page in resp['data']:
    if page['id'] == cfg['page_id']:
        page_access_token = page['access_token']
    graph = facebook.GraphAPI(page_access_token)
    return graph

if __name__ == "__main__":
 main()

Considering the permissions : We need manage_pages publish_pages permissions.

page id you can find at the bottom of the about section of your page.

nofoobar
  • 2,826
  • 20
  • 24
  • Hi, do you have verified your accout as individual developer or company for your facebook app review? – itajackass Apr 06 '20 at 20:17
  • I have registered as an individual developer. – nofoobar Apr 07 '20 at 10:48
  • thank you.so... for individual dev, no restriction about manage_pages and publish_pages? – itajackass Apr 07 '20 at 11:19
  • 1
    I'm getting the below error any idea? Seems to me like Facebook doesn't want us to develop on this API isn't? It gets not so easy. status = api.put_wall_post(msg) File "C:\Users\SG0216333\AppData\Local\Continuum\anaconda3\envs\riverEnv\lib\site-packages\facebook\__init__.py", line 163, in put_wall_post **attachment) File "C:\Users\SG0216333\AppData\Local\Continuum\anaconda3\envs\riverEnv\lib\site-packages\facebook\__init__.py", line 140, in put_object assert self.access_token, "Write operations require an access token" – sgvolpe Jun 02 '20 at 12:08
  • 1
    I see that this module hasn't been updated since 2018. https://github.com/sns-sdks/python-facebook seems to be more modern but I haven't tried it yet. – thinkvitamin Mar 04 '23 at 12:07