Skip to main content

Library for content creation automation on Facebook

Project description

Python Easy Facebook API

Make requests to the facebook graph api easily.

Docs

Instantiate the class

from python_easy_facebook_apy import FbApi

# We recommend seriously use a library like python_dotenv to store the facebook access token and the page id.
# To learn how to get access_token and page_id read "Get Access Token and Page ID" in the end of this file.
access_token = "<your access token>"
page_id = "<your page id>"

api = FbApi(
    access_token=access_token
    page_id=page_id
)

# ...

To add a Text Post

post = api.text_post(
    content="<your post content>"
)

To add a Photo Post

post = api.photo_post(
    # You can use any framework or library like Flask or Django to store your image and upload to facebook
    # Example:
    # image_url="https://example.com/image.jpg"
    image_url="<your image url>"
)

To add a Text with Photo Post

post = api.text_with_photo_post(
    content="<your post content>"
    image_url="<your image url>"
)

Submitting the post

# Makes the POST request to the Facebook Graph API
response = post.submit_post()

# Prints the id of the post, or instead, if you had an error with the `access token` or with the `page id` it will show the error details
print(response.json())

Use custom endpoints

To use custom endpoints you can import the class FacebookService

from python_facebook_easy_api import FacebookService

"""
At the moment, this library only make post requests, but it's planned add support to all methods

Example:
POST - https://graph.facebook.com/<page_id>/<custom_endpoint>
payload: {
    # The rest of the body
    **body,
    'access_token': access_token
}
"""

custom_endpoint = FacebookService(
    access_token="<your access token>",
    page_id="<your page id>",
    custom_endpoint="<the facebook graph api endpoint>" # Example: feed
)

# This creates a post with the text "hi"
response = custom_request.submit_request(
    {
        'message': 'hi'
    }
)

print(response.json())
# ...

# Else you can create your own class extending the facebook service
# Example:
class MyCustomPost(FacebookService):
    def __init__(self, ...):
        FacebookService.__init__(
            self,
            custom_endpoint="..."
            # ...
        )

    def submit_post(self):
        body = {
            # ...
        }

        self.submit_request(body)

Getting the Access Token

  • Firstly, go to Facebook Developer

  • Create a developer account

  • Then, create an application and select that the app type is Business app, or something like this

  • After you created an application, hover with the mouse the Tools menu and select Api Graph Explorer

  • In the App Meta field, select your app that you already created

  • Then, in the permissions field, you need to select theese permissions:

    • pages_show_list
    • pages_read_engagement
    • pages_manage_posts
    • public_profile
  • After selected the permissions, click on the Generate Access Token button, you will see a new window that you have to agree everything and select the Facebook Page that you want to connect, NOTE: below the name of any page, is the page id, you must copy and save on a secure place, if you're using dotenv (I seriously recommend use that), you must save the page id, with a key like PAGE_ID=<your page id> in a .env file.

  • And, finally, the last step, after generating the token, you must click on the User or page field and select your page, the token will change and you have to copy it and save to use on your app (that token is really sensitive, you have to store it on a environment variable, using dotenv)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_easy_facebook_api-1.0.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file python_easy_facebook_api-1.0.1.tar.gz.

File metadata

File hashes

Hashes for python_easy_facebook_api-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c1df5546ca9e8a8699a9aa523646e4fdd9d84ceb651eeb24e8e0d9f88dbee899
MD5 1442b8badd71f050e96159d2fff0bf58
BLAKE2b-256 bfa858fbf82433c4e512e9bd035725f8c345538a735b080cb14990e3a93fe60a

See more details on using hashes here.

File details

Details for the file python_easy_facebook_api-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_easy_facebook_api-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3a88a084de7b260f8d72eb9004e4913c8406ccb480cda485e41f71840025248d
MD5 290a3cf5aaeda71b127f69928c298fc1
BLAKE2b-256 cf45d5d9154ad681d60811c1b47e368c1ebb042cd24ffda322e364238dded184

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page