skip to Main Content

As of the 21st October 2019 Instagram prevented you from creating and managing your clients from within Instagram and everything is now managed through Facebook Developer, no real issue with that…

In my instance all that is required is to show a single user profile images. I got this working fairly quickly, however the Access Token just expires every 2 hours, then I need to re-authorise it again… This was never an issue before… You authorised once and it would just work.

Take this working code:

function fetchData($url){
  $inst = curl_init($url);
  curl_setopt($inst, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($inst, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($inst, CURLOPT_SSL_VERIFYPEER, 0);
  $output = curl_exec($inst);
  curl_close($inst);

  return $output;
}

$insta_access_token = 'MY_ACCESS_TOKEN';
$insta_count = '6';

$instagram_result = fetchData("https://graph.instagram.com/me/media?fields=id,username,media_url&access_token={$insta_access_token}&count={$insta_count}");
$instagram_result = json_decode($instagram_result);
print_r($instagram_result);

This works fine until the access token expires.

I have seen other tricks around not using the API at all located here: How can I get a user's media from Instagram without authenticating as a user?

This uses PHP to extract profile contents and is the only viable solution from my research to display a basic image gallery from a single use profile:

$html = file_get_contents('https://instagram.com/apple/');
preg_match('/_sharedData = ({.*);</script>/', $html, $matches);
$profile_data = json_decode($matches[1])->entry_data->ProfilePage[0]->graphql->user;

Am I missing something or is there no way to keep a valid access token running once authenticated once for basic image display?

Cheers.

2

Answers


  1. All what you need is to extend your access token duration by making a GET request to this endpoint (long lived access token is valuable for 60 days from what i know)

    url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&%20client_id='Your_App_Id'&client_secret='Your_App_secret'&fb_exchange_token='Your_Short_Lived_Access_token''
    

    Hope this is what you were looking for =)

    Login or Signup to reply.
  2.  require_once __DIR__ . '/vendor/autoload.php';
    
    // facebook credentials array
    $creds = array(
        'app_id' => FACEBOOK_APP_ID,
        'app_secret' => FACEBOOK_APP_SECRET,
        'default_graph_version' => 'v3.2',
        'persistent_data_handler' => 'session'
    );
    
    // create facebook object
    $facebook = new FacebookFacebook( $creds );
    
    // helper
    $helper = $facebook->getRedirectLoginHelper();
    
    // oauth object
    $oAuth2Client = $facebook->getOAuth2Client();
    
    if ( isset( $_GET['code'] ) ) { // get access token
        try {
            $accessToken = $helper->getAccessToken();
        } catch ( FacebookExceptionsFacebookResponseException $e ) { // graph error
            echo 'Graph returned an error ' . $e->getMessage;
        } catch ( FacebookExceptionsFacebookSDKException $e ) { // validation error
            echo 'Facebook SDK returned an error ' . $e->getMessage;
        }
    
        if ( !$accessToken->isLongLived() ) { // exchange short for long
            try {
                $accessToken = $oAuth2Client->getLongLivedAccessToken( $accessToken );
            } catch ( FacebookExceptionsFacebookSDKException $e ) {
                echo 'Error getting long lived access token ' . $e->getMessage();
            }
        }
    
    
        $accessToken = (string) $accessToken;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search