This document offers instructions for automating the authorization and data retrieval process of the Instagram API using Python, specifically focusing on the Instagram Basic Display API. The provided code illustrates the steps to acquire an access token, retrieve media information, and store it in a JSON file. Furthermore, it includes guidance on generating SSL certificates and adding localhost to Valid OAuth Redirect URIs.
Before proceeding, ensure you have:
- Python installed on your system.
- OpenSSL installed for generating SSL certificates.
- An Instagram Developer account to obtain client ID and client secret.
The provided Python script instagramAPI_private
performs the following tasks:
- Imports necessary libraries and modules.
- Defines the Flask application and sets up SSL encryption.
- Generates the authorization URL and prompts the user to authorize the application.
- Handles the callback route to obtain an access token.
- Retrieves media information using the access token.
- Saves the media information to a JSON file.
To use instagramAPI_public
, you first need to ensure that your Instagram account is set to public. Then, in the App Dashboard under Instagram Basic Display settings, you can add your account as a tester. Once your account is added and set to public, you can generate a token that will be used in this code.
After obtaining the access token, you can directly run the provided Python script. Here's what the code does:
- It retrieves media information from the Instagram Basic Display API.
- The access token is used to authenticate the requests to the API.
- The script fetches media data such as ID, caption, media type, media URL, username, timestamp, permalink, and thumbnail URL.
- This information is stored in a dictionary for each media ID.
- Finally, the script writes the collected data to a JSON file named media_info.json.
Ensure to replace {public_user_token}
with your actual access token before running the code. After execution, the media information will be saved to media_info.json
.
To generate SSL certificates for local development, follow these steps:
- Open a terminal window.
- Run the following command to create a certificate with a passphrase:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
- Remove the passphrase from the key file using:
openssl rsa -in key.pem -out key_no_passphrase.pem
To add localhost as a Valid OAuth Redirect URI:
- Log in to your Instagram Developer account.
- Navigate to your app's settings.
- Find the "Valid OAuth Redirect URIs" section.
- Add https://127.0.0.1:5000/callback as a valid redirect URI.
- Save the changes.
- Run the Python script after configuring SSL certificates and adding the redirect URI.
- Open the provided authorization URL in your browser and authorize the application.
- After authorization, the script will retrieve media information and save it to a JSON file named
media_info.json
.
- After retrieving and replacing the authorization token, execute the Python script.
- The script will fetch media information and store it in a JSON file named
media_info.json
.
If you want to implement a social wall on your website using the JSON file generated from the Instagram API, you can use the code provided in the ChakraUI directory and follow these steps:
Include the InstagramFeed component in your React application where you want to display the Instagram media. An example is provided in the InstagramUsage.tsx file.
The InstagramFeed component reads data from the media_info.json file located in the same directory. Ensure that the JSON file is accessible to your React application.
The InstagramFeed component renders Instagram images or videos fetched from the JSON file. It dynamically adjusts the number of images displayed based on the screen size.
If you have more Instagram posts than initially displayed, users can click the "Load More" button to load additional posts.
Clicking on an Instagram post opens a modal displaying the full-size image or video along with the post caption and options to view on Instagram.
The InstagramClick component handles the modal functionality. It displays the media content and associated information when clicked.
Ensure that you have the necessary CSS styles and dependencies installed for Chakra UI and motion animation libraries used in the components.
By following these steps and incorporating the provided React components, you can easily create a visually appealing social wall on your website using Instagram media fetched from the JSON file. Adjust the styling and functionality as needed to fit your website's design and requirements.