Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a post on facebook using python

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

like image 793
sowmya Avatar asked Feb 02 '18 09:02

sowmya


People also ask

How to send a post to a Facebook page using Python?

Send a post to Facebook page using Facebook Graph API Access Token with Python You will also need a page ID — for the facebook page you would like to post to.You can get your page ID directly from your facebook page, under “about” tab. Copy this ID and paste it in your code. We will be using the requests library, install it below:

How do I post to a Facebook page?

In order to post to a page, we must create a new post node connected to our page’s node. We can do this using the Facebook API for authentication, and an official or third-party SDK to handle the graph manipulation. In this example the snippets will be in Python, but the concepts are similar across all the SDKs.

How to post to a Facebook page using Graph API?

Facebook’s Graph API represents pages, posts, comments, and other data as nodes in a graph. In order to post to a page, we must create a new post node connected to our page’s node. We can do this using the Facebook API for authentication, and an official or third-party SDK to handle the graph manipulation.

What can I do with the Facebook API?

You can use the API to: (1) Read the messages on your timeline, (2) send messages, (3) read posts, (4) create posts, edit posts or even delete posts. The API is a powerful tool to interact with your facebook account or page programatically, hence providing a tool to automate your facebook.


2 Answers

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!")
like image 78
sowmya Avatar answered Oct 24 '22 09:10

sowmya


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

like image 27
sandeep kumar Avatar answered Oct 24 '22 09:10

sandeep kumar