sns-sdks / python-facebook

A simple Python wrapper for facebook graph api :sparkles: :cake: :sparkles: .
https://sns-sdks.github.io/python-facebook/
328 stars 86 forks source link

Examples - How to post an image to Instagram Business? #194

Closed eugene-bce closed 2 years ago

eugene-bce commented 2 years ago

I'm trying to understand how to use this libary to create a post for Instagram Business. Is there some documentation with this already, or if not, can you point me in the right direction please?

MerleLiuKun commented 2 years ago

As for current version.

First you need know the api docs for publish a media.

Available docs:

Then I think you can use the GraphAPI class to post a media. just a demo for publish a image media:

Initial class

from pyfacebook import GraphAPI

api = GraphAPI(access_token="access token")  # notice the access token need have publish permissions.
object_id = "business ID"   # target business account id

Then create a container to publish.

resp = api.post_object(
    object_id=object_id,
    connection="media",
    params={
        "image_url": "public url for image",
        "caption": "Image published by api",
    }
)
print(resp)
# {'id': '17952987976782688'}

Then publish the container.

publish_resp = api.post_object(
    object_id=object_id,
    connection="media_publish",
    params={
        "creation_id": "17952987976782688",
    }
)
print(publish_resp)
# {'id': '17899702154554435'}

Now you can see the media has published success.