Laravel package connecting to Instagram's new API Basic Display. Retrieving personal user's posts and keep them in cache, with specific commands or with Scheduler, and taking charge of refreshing the Instagram token every two months.
To use the Instagram Basic Display API, you will need to register a Facebook app and configure Instagram Basic Display. Follow the getting started guide.
Install the package in your application by running
composer require chill-pills/laravel-instagram-basic-feed
Add and complete these lines in your .env file
INSTAGRAM_VALID_OAUTH_URI=
INSTAGRAM_APP_ID=
INSTAGRAM_SECRET_KEY=
INSTAGRAM_ACCESS_TOKEN=
For the INSTAGRAM_VALID_OAUTH_URI
entry, you will use the same URI you used in the Valid OAuth Redirect URIs field when you created the Instagram App. We will retrieve the INSTAGRAM_ACCESS_TOKEN
in the next steps. (Ensure all the other env entries are complete)
We will need to go over the following steps to ensure our package is configure to work correctly;
We need to generate a link which redirects the user to the Instagram “Authorization Window". You can generate the url by running the following command:
php artisan instagram-feed:get-authorization-url
Copy & Enter the url in your browser (Your authorization window link should look something like this)
https://api.instagram.com/oauth/authorize
?client_id={INSTAGRAM_APP_ID}
&redirect_uri={INSTAGRAM_VALID_OAUTH_URI}
&scope=user_profile,user_media
&response_type=code
Next, you will be shown the Authorization Window
Click on Authorize
You will be redirected to the INSTAGRAM_VALID_OAUTH_URI
. If you look in your browser’s URL bar, you should notice the URL has an authorization code that has been appended to the redirect URL. Something like this:
{INSTAGRAM_VALID_OAUTH_URI}?code=AQBv...Xw#_
Copy the authorization code, The authorization code in the redirect URL is everything after code=
up to (but not including) the #_
at the end.
Now with the authorization code we are going to generate access token we can use in our application. Use the command below generate the token. We generate long-lived access_token
because those are valid for 60 days. Replace the authorization_code
with the code from the last step.
php artisan instagram-feed:setup-new-access-token <authoriczation-code>
Awesome! Now, with this long-lived access_token, you can make requests to the API for the next 60 days. You can also refresh the token, extending for another 60 days as long as the token is not expired and is at least 24 hours old (and has not been revoked by your Instagram user deauthorizing your app).
There's two commands that lets you fetch your feed or renew the Access Token.
php artisan instagram-feed:crawl
php artisan instagram-feed:refresh-key
You can add the following scheduler's command to take care of executing those commands automatically
protected function schedule(Schedule $schedule)
{
$schedule->command(InstagramCrawlFeed::class)->hourly();
$schedule->command(InstagramRefreshAccessToken::class)->monthly();
}
Don't forget to enable the CRON on your machine for Laravel Basic Scheduler in your app/Console/Kernel.php file
* * * * * cd /home/path/to/your/project && php artisan schedule:run >> /dev/null 2>&1
To show the Instagram feed on your page, you can just add the following Blade command to include the partial to your page.
@include('instagram-basic-feed::instagram-post')
You can pass to the @include a hashtag used to search through the posts
@include('instagram-basic-feed::instagram-post', ['hastag' => '#duckhunt'])
If you want to modify the view displaying the instagram posts itself
php artisan vendor:publish --tag=instagram-basic-feed-view
The MIT License (MIT). Please see License File for more information.