Skip to content

Commit

Permalink
Compatibility with new Instagram API requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
jbclaudio committed Jan 6, 2025
1 parent ebf5d21 commit 3b018ec
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Instagram.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<?php


namespace JustBetter\InstagramFeed;


use JustBetter\InstagramFeed\Exceptions\BadTokenException;
use JustBetter\InstagramFeed\Exceptions\HttpException;
use Illuminate\Support\Facades\Config;

class Instagram
{
const REQUEST_ACCESS_TOKEN_URL = "https://api.instagram.com/oauth/access_token";
const GRAPH_USER_INFO_FORMAT = "https://graph.instagram.com/%s?fields=id,username&access_token=%s";
const GRAPH_USER_INFO_FORMAT = "https://graph.instagram.com/%s?fields=id,media_count,username&access_token=%s";
const EXCHANGE_TOKEN_FORMAT = "https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret=%s&access_token=%s";
const REFRESH_TOKEN_FORMAT = "https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=%s";
const MEDIA_URL_FORMAT = "https://graph.instagram.com/%s/media?fields=%s&limit=%s&access_token=%s";
const MEDIA_FIELDS = "caption,id,media_type,media_url,thumbnail_url,permalink,children{media_type,media_url},timestamp";


private $client_id;
private $client_secret;
private $redirect_uri;
Expand All @@ -40,8 +37,17 @@ public function authUrlForProfile($profile)
{
$client_id = $this->client_id;
$redirect = $this->redirectUriForProfile($profile->id);
$scopes = ['instagram_business_basic'];

$parameters = [
'client_id' => $client_id,
'redirect_uri' => $redirect,
'response_type' => 'code',
'scope' => urlencode(implode(',', $scopes)),
'state' => $profile->identity_token,
];

return "https://api.instagram.com/oauth/authorize/?client_id=$client_id&redirect_uri=$redirect&scope=user_profile,user_media&response_type=code&state={$profile->identity_token}";
return 'https://www.instagram.com/oauth/authorize/?' . http_build_query($parameters);
}

private function redirectUriForProfile($profile_id)
Expand All @@ -65,7 +71,7 @@ public function requestTokenForProfile($profile, $auth_request)

public function fetchUserDetails($access_token)
{
$url = sprintf(self::GRAPH_USER_INFO_FORMAT, $access_token['user_id'], $access_token['access_token']);
$url = sprintf(self::GRAPH_USER_INFO_FORMAT, $access_token['access_token'] ?? $access_token['access_code']);
return SimpleClient::get($url);
}

Expand Down

0 comments on commit 3b018ec

Please sign in to comment.