Use Python to post a message on Instagram.

ยท

2 min read

To post a message or an image on Instagram using Python, you can utilize the Instagram Graph API. Here's a step-by-step guide for achieving this:


Step 1: Set Up Facebook and Instagram Accounts

  1. Link Instagram to a Facebook Page: This is required because Instagram Graph API works with Instagram Business or Creator accounts linked to Facebook Pages.

  2. Create a Facebook App: Visit the Facebook Developers Portal to create an app that will allow you to access Instagram features.


Step 2: Get API Access Tokens

  1. Navigate to your app in the Facebook Developer Portal.

  2. Add Instagram Graph API under your app's products.

  3. Obtain a User Access Token with the required permissions:

    • instagram_basic

    • instagram_content_publish

    • pages_read_engagement

    • pages_manage_posts


Step 3: Find Your Instagram Business Account ID

  1. Use the /me/accounts endpoint in the Graph API Explorer to find your linked page ID.

  2. Use the /PAGE_ID?fields=instagram_business_account endpoint to retrieve your Instagram account ID.


Step 4: Python Code to Post

Below is an example script to post an image with a caption:

import requests

# Your access token and Instagram account ID
ACCESS_TOKEN = "your_access_token"
INSTAGRAM_ACCOUNT_ID = "your_instagram_account_id"

def post_to_instagram(image_url, caption):
    # Step 1: Upload Image to Instagram
    url = f"https://graph.facebook.com/v16.0/{INSTAGRAM_ACCOUNT_ID}/media"
    payload = {
        'image_url': image_url,  # URL of the image you want to post
        'caption': caption,
        'access_token': ACCESS_TOKEN
    }
    response = requests.post(url, data=payload)
    result = response.json()

    if 'id' in result:
        creation_id = result['id']
        print(f"Media uploaded. Creation ID: {creation_id}")
    else:
        print(f"Error: {result}")
        return

    # Step 2: Publish the Post
    publish_url = f"https://graph.facebook.com/v16.0/{INSTAGRAM_ACCOUNT_ID}/media_publish"
    publish_payload = {
        'creation_id': creation_id,
        'access_token': ACCESS_TOKEN
    }
    publish_response = requests.post(publish_url, data=publish_payload)
    publish_result = publish_response.json()

    if 'id' in publish_result:
        print(f"Post published successfully! Post ID: {publish_result['id']}")
    else:
        print(f"Error: {publish_result}")

# Example usage
post_to_instagram(
    image_url="https://example.com/path-to-your-image.jpg",
    caption="Hello from Python! ๐Ÿ #InstagramAPI"
)

Step 5: Notes

  1. Ensure your image URL is accessible and publicly available.

  2. Instagram only allows posts to business or creator accounts, not personal accounts.

  3. You need to maintain the validity of your Access Token or use the Refresh Token API.


Applications

  • Schedule Instagram posts.

  • Automate content management for marketing.

  • Integrate Instagram posting into your broader automation pipeline.

Leverage this Python script to elevate your Instagram game! ๐Ÿš€

ย