Skip to content

Instantly share code, notes, and snippets.

@iqbalrony
Last active April 4, 2023 12:30
Show Gist options
  • Save iqbalrony/7e3d1156baa053ae9b526df835d380f4 to your computer and use it in GitHub Desktop.
Save iqbalrony/7e3d1156baa053ae9b526df835d380f4 to your computer and use it in GitHub Desktop.
how to get access token for instagram basic display API
<?php
$endpoint = 'https://api.instagram.com/oauth/access_token';
// params the endpoint requires
$params = array(
'app_id' => 'your Instagram_APP_ID', // our instagram app id
'app_secret' => 'your Instagram_APP_SECRET', // our instagram app secret
'grant_type' => 'authorization_code',
'redirect_uri' => 'your redirect url', // our redirect uri
'code' => $_GET['code'] ? $_GET['code'] : '' // code instagram sent us in the URL
);
// open curl call
$ch = curl_init();
// set type to post and pass the fields along
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
curl_setopt( $ch, CURLOPT_POST, 1 );
// set curl url
curl_setopt( $ch, CURLOPT_URL, $endpoint );
// set other curl options
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// get response, close curl, make php array
$response = curl_exec( $ch );
// close cur
curl_close( $ch );
// make nice php array :D
$responseArray = json_decode( $response, true );
// print out the response array that contains the access token
echo '<pre>';
print_r( $responseArray );
echo '</pre>';
?>
<a href="https://api.instagram.com/oauth/authorize?app_id={app_ID}&redirect_uri=(redirect_uri}&scope=user_profile,user_media&response_type=code">
Authorize Instagram
</a>
<?php
if( isset($_GET['code']) ){
$post_array = array(
'app_id' => '232465684475141',
'app_secret' => '42d3e06926ea818ea4003ab90f31e457',
'grant_type' => 'authorization_code',
'redirect_uri' => 'https://raven.happymonster.dev/instagram-api/',
'code' => $_GET['code'] ? $_GET['code'] : ''
);
$headers = array();
$args = array( 'headers' => $headers, 'sslverify' => false, 'body' => $post_array );
$response = wp_remote_post('https://api.instagram.com/oauth/access_token', $args);
$response_body = '';
if( 200 == wp_remote_retrieve_response_code($response) ) {
$response_body = wp_remote_retrieve_body( $response );
$response_body = json_decode( $response_body );
//$response_body->access_token
}
echo "<pre>";
var_dump(wp_remote_retrieve_response_code($response),$response_body);
echo "</pre>";
}
$post_array = array(
'grant_type' => 'ig_exchange_token',
'client_secret' => '42d3e06926ea818ea4003ab90f31e457',
'access_token' => 'IGQVJXYmY5elJsdEVZAdnVnU05yWWFtYmJ5RU82U2dzUnhveC1seU5qc1FNMlRhMUVmTEZAUMXI2YUQ1ekhRMll2ZA1RLQkVkODJQZAG4zNkxZAbWdwbjR3UHJpbDdVLUUzQnM5LUNZAQWR6c0dXdlFCVEtLMmN1VEpWNTdVQWVF',
);
$headers = array();
$args = array( 'method' => 'GET', 'headers' => $headers, 'sslverify' => false, 'body' => $post_array );
$refresh = wp_remote_post('https://graph.instagram.com/access_token', $args);
$refresh_body = '';
if( 200 == wp_remote_retrieve_response_code($refresh) ) {
$refresh_body = wp_remote_retrieve_body( $refresh );
$refresh_body = json_decode( $refresh_body );
//$response_body->access_token
}
echo "<pre>";
var_dump( $refresh_body );
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment