ramsey / laravel-oauth2-instagram by ramsey

A Laravel 5 service provider for league/oauth2-instagram
10,675
29
6
Package Data
Maintainer Username: ramsey
Maintainer Contact: ben@benramsey.com (Ben Ramsey)
Package Create Date: 2015-12-27
Package Last Update: 2020-01-15
Language: PHP
License: MIT
Last Refreshed: 2024-04-22 03:01:49
Package Statistics
Total Downloads: 10,675
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 29
Total Watchers: 6
Total Forks: 9
Total Open Issues: 3

ramsey/laravel-oauth2-instagram

Source Code Latest Version Software License PHP Version Build Status Coverage Status Total Downloads

ramsey/laravel-oauth2-instagram is a Laravel 5 service provider for league/oauth2-instagram.

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require ramsey/laravel-oauth2-instagram

After requiring the package with Composer, you'll need to add the following to the providers array in config/app.php:

Ramsey\Laravel\OAuth2\Instagram\InstagramServiceProvider::class

Then, add the following to the aliases array in the same file:

'Instagram' => Ramsey\Laravel\OAuth2\Instagram\Facades\Instagram::class

Now, run the following to properly set up the package with your Laravel application:

php artisan vendor:publish

Finally, register your application with Instagram and add your client ID, client secret, and redirect URI to config/instagram.php.

Examples

Create an authorization URL and redirect users to it in order to request access to their Instagram account:

$authUrl = Instagram::authorize([], function ($url, $provider) use ($request) {
    $request->session()->put('instagramState', $provider->getState());
    return $url;
});

return redirect()->away($authUrl);

In the route for the redirect URI, check the state and authorization code, and use the code to get an access token. Store the token to the session or to the user's profile in your data store.

if (!$request->has('state') || $request->state !== $request->session()->get('instagramState')) {
    abort(400, 'Invalid state');
}

if (!$request->has('code')) {
    abort(400, 'Authorization code not available');
}

$token = Instagram::getAccessToken('authorization_code', [
    'code' => $request->code,
]);

$request->session()->put('instagramToken', $token);

Use the access token to make authenticated requests to Instagram.

$instagramToken = $request->session()->get('instagramToken');

$instagramUser = Instagram::getResourceOwner($instagramToken);
$name = $instagramUser->getName();
$bio = $instagramUser->getDescription();

$feedRequest = Instagram::getAuthenticatedRequest(
    'GET',
    'https://api.instagram.com/v1/users/self/feed',
    $instagramToken
);

$client = new \GuzzleHttp\Client();
$feedResponse = $client->send($feedRequest);
$instagramFeed = json_decode($feedResponse->getBody()->getContents());

Contributing

Contributions are welcome! Please read CONTRIBUTING for details.

Copyright and License

The ramsey/laravel-oauth2-instagram library is copyright © Ben Ramsey and licensed for use under the MIT License (MIT). Please see LICENSE for more information.