import os
      import requests
      import json
      
      PAGE_ACCESS_TOKEN = "<to get from https://developers.facebook.com/>"
      INSTAGRAM_BUSINESS_ACCOUNT = "<to get by using curl -i -X GET "https://graph.facebook.com/v10.0/{facebook_page_id}?fields=instagram_business_account&access_token={access-token}""
      # See https://levelup.gitconnected.com/automating-instagram-posts-with-python-and-instagram-graph-api-374f084b9f2b
      def post_instagram(message, image):
          #Post the Image
          image_location_1 = '<insert_url>'
          post_url = 'https://graph.facebook.com/v10.0/{}/media'.format(INSTAGRAM_BUSINESS_ACCOUNT)
          payload = {
              'image_url': image_location_1,
              'caption': message,
              'access_token': PAGE_ACCESS_TOKEN
          }
          r = requests.post(post_url, data=payload)
          print(r.text)
          result = json.loads(r.text)
          if 'id' in result:
              creation_id = result['id']
              second_url = 'https://graph.facebook.com/v10.0/{}/media_publish'.format(INSTAGRAM_BUSINESS_ACCOUNT)
              second_payload = {
                  'creation_id': creation_id,
                  'access_token': PAGE_ACCESS_TOKEN
              }
              r = requests.post(second_url, data=second_payload)
              print('--------Just posted to instagram--------')
              print(r.text)
          else:
              print('HOUSTON we have a problem')
      

      Python language logo
      Post picture on instagram

      instagram
      Social Media Posting

      Post a picture on Instagram using Python.

      The full explanation of how to get all tokens can be found on this blog post

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.