Skip to content

Commit

Permalink
Add feature Login with Instagram
Browse files Browse the repository at this point in the history
  • Loading branch information
vickzkater committed Nov 7, 2020
1 parent 6c5217d commit 2d03316
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_CALLBACK_URL="${APP_URL_SITE}/auth/facebook/callback"

INSTAGRAM_CLIENT_MODULE=true
INSTAGRAM_CLIENT_ID=
INSTAGRAM_CLIENT_SECRET=
INSTAGRAM_CALLBACK_URL="${APP_URL_SITE}/auth/instagram/callback"

FCM_SERVER_KEY=
FCM_SENDER_ID=

Expand Down
35 changes: 18 additions & 17 deletions app/Http/Controllers/Admin/system/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function redirect_to_provider($driver)
if ($driver == 'instagram') {
$appId = env('INSTAGRAM_CLIENT_ID');
$redirectUri = urlencode(env('INSTAGRAM_CALLBACK_URL'));
return redirect()->to("https://api.instagram.com/oauth/authorize?app_id={$appId}&redirect_uri={$redirectUri}&scope=user_profile,user_media&response_type=code");
return redirect()->to("https://api.instagram.com/oauth/authorize?client_id={$appId}&redirect_uri={$redirectUri}&scope=user_profile,user_media&response_type=code");
} else {
return Socialite::driver($driver)->redirect();
}
Expand All @@ -246,6 +246,8 @@ public function redirect_to_provider($driver)
public function handle_provider_callback($social, Request $request)
{
if ($social == 'instagram') {
// https://stackoverflow.com/questions/59142407/laravel-integrate-with-instagram-api-after-october-2019
// https://developers.facebook.com/docs/instagram-basic-display-api/getting-started
$code = $request->code;
if (empty($code)) {
return redirect()
Expand All @@ -255,7 +257,7 @@ public function handle_provider_callback($social, Request $request)

$appId = env('INSTAGRAM_CLIENT_ID');
$secret = env('INSTAGRAM_CLIENT_SECRET');
$redirectUri = urlencode(env('INSTAGRAM_CALLBACK_URL'));
$redirectUri = env('INSTAGRAM_CALLBACK_URL');

// Set API URL - retrieve the data (Get access token)
$url = 'https://api.instagram.com/oauth/access_token';
Expand All @@ -270,30 +272,29 @@ public function handle_provider_callback($social, Request $request)
// Hit API - using method POST
$response = $this->guzzle_post_public($url, $params);

if ($response->getStatusCode() != 200) {
if (isset($response->code)) {
return redirect()
->route('admin.login')
->with('error', 'Unauthorized login to Instagram');
}

$content = $response->getBody()->getContents();
$content = json_decode($content);

$accessToken = $content->access_token;
$userId = $content->user_id;
// get access token for exchange it for a token
$accessToken = $response->access_token;

// Set API URL - retrieve the data (Get user info)
$url = "https://graph.instagram.com/me?fields=id,username,account_type&access_token={$accessToken}";
$url = "https://graph.instagram.com/me?fields=id,username,account_type,media_count&access_token={$accessToken}";
// Hit API - using method GET
$response = $this->guzzle_get_public($url);

$content = $response->getBody()->getContents();
$oAuth = json_decode($content);

// Get instagram user name
$username = $oAuth->username;

dd($content, $userId, $username, $oAuth);
$oAuth = $response;

// *dumping $oAuth
// {#448 ▼
// +"id": "17841401572570570"
// +"username": "vickzkater"
// +"account_type": "PERSONAL"
// }

dd($oAuth);
} else {
$user = Socialite::driver($social)->user();

Expand Down

0 comments on commit 2d03316

Please sign in to comment.