skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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!")
    

  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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search