Publish Post on Page through Facebook API

So, here comes very interesting task. As I mentioned in my previous blog about #LuvLdh Event, this blog is related to that only.

Me and my friend Amrit was given a task to make a web app which will allow user to login through facebook, upload images or document and automatically publish it on page.

This blog will cover very small part of the problem i.e. how user can publish post on a page. We used Facebook SDK for Python.

We followed following links to  solve it.

https://facebook-sdk.readthedocs.io/en/latest/api.html

http://nodotcom.org/python-facebook-tutorial.html

https://stackoverflow.com/questions/24052651/python-facebook-sdk-post-to-page

Installation

Install facebook module of Python as given in https://facebook-sdk.readthedocs.io/en/latest/install.html

Generate Access Token

  1. Login to facebook account.
  2. In new tab, browse to https://developers.facebook.com/
  3. Go to “Tools and Support” and then “Graph API Explorer”.
  4. Click “Get Token” and then “Get User Access Token”. It will prompt out permission options.
  5. In order to publish post on your account, Check box “publish actions” permission and then click “Get Access Token”. It will ask for whom should see the post(preferably public). To know more about permissions browse to https://developers.facebook.com/docs/facebook-login/permissions

Congratulations, you have generated your long string of access token.

import facebook

graph = facebook.GraphAPI(access_token="your_generated_token", version=2.10)

 

The above command will create new object “graph” with your own access token.

graph.put_photo(image=open('img.jpg', 'rb'),
                message='Look at this cool photo!')

This will publish photo on your account. Likewise to publish text or anything follow given links.

Publish Post as a Normal User into a Page.

Now recreate Token with more permissions. This time check box

Scopes user_location, user_likes, user_events, user_photos, user_about_me, user_posts, email, publish_actions, manage_pages, pages_show_list, publish_pages, public_profile

 

Here “public_profile” means to view your post to public. This permission is must. This is obtain when we click “get access token” it ask for to show the post to [public, friends,  only me]. Select Public. It took complete one day to debug the problem.:P

Now check the Page_id of the page in which you want to publish. It is displayed in URL or you also see in the “About” Section of the page.

Create the new object just like “graph” object created with new access token.

graph.put_wall_post(message=’Really Worked!!’, profile_id=’your_page_id’)

Likewise, an admin of the page can also publish the post. Rather an admin can select any option among [public, friends,  only me].

Make sure in the General settings of the page there is no bar for a normal user and everything is set to public(if applicable).

Now check post to the page Community section. You will be able to see the post.:)

For any kind of doubt feel free to comment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s