This code requests an access token from Instagram, passes it into a function to get a long-lived access token and with this, gets a user's information and posts.
It does get the post, but on refresh I'm getting the error message { ["error_type"]=> string(14) "OAuthException" ["code"]=> int(400) ["error_message"]=> string(37) "This authorization code has been used" }
.
I think that's because if ( $params['access_token'] )
never fires.
But I'm not sure why when in the elseif
statement ( $params['get_code'] )
the variable for the access token gets updated.
Said elseif
statement only fires if I set a variable and pass in a token, like $accessToken = 'ACCESS-TOKEN';
. But since even the long-lived token expires after 60 days, I'm trying to find a solution that detects in how many seconds expiration is due and based on this, retrieves a new token. But to make this work, it first needs to recognize that there is a token even when a user refreshes the page or returns to it later...
This code gets the tokens:
private function _setUserInstagramAccessToken( $params ) {
if ( $params['access_token'] ) { // this never fires
// we have an access token
$this->_userAccessToken = $params['access_token'];
$this->hasUserAccessToken = true;
$this->userId = $params['user_id'];
echo "access token available!";
} elseif ( $params['get_code'] ) { // this always fires
echo "no access token found, go get one";
// try to get access token and user id, then update respective vars
$userAccessTokenResponse = $this->_getUserAccessToken();
$this->_userAccessToken = $userAccessTokenResponse['access_token']; //store access token
$this->hasUserAccessToken = true;
$this->userId = $userAccessTokenResponse['user_id']; //store user id
// get long lived access token
$longLivedAccessTokenResponse = $this->_getLongLivedUserAccessToken();
$this->_userAccessToken = $longLivedAccessTokenResponse['access_token'];
$this->_userAccessTokenExpires = $longLivedAccessTokenResponse['expires_in'];
}
}
private function _getUserAccessToken() {
$params = array(
'endpoint_url' => $this->_apiBaseUrl . 'oauth/access_token',
'type' => 'POST',
'url_params' => array(
'app_id' => $this->_appId,
'app_secret' => $this->_appSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->_redirectUrl,
'code' => $this->_getCode
)
);
$response = $this->_makeApiCall( $params );
return $response;
}
private function _getLongLivedUserAccessToken() {
$params = array(
'endpoint_url' => $this->_graphBaseUrl . 'access_token',
'type' => 'GET',
'url_params' => array(
'client_secret' => $this->_appSecret,
'grant_type' => 'ig_exchange_token',
)
);
$response = $this->_makeApiCall( $params );
return $response;
}
This function makes the API call:
private function _makeApiCall( $params ) {
$ch = curl_init();
$endpoint = $params['endpoint_url'];
if ( 'POST' == $params['type'] ) {
// post request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params['url_params'] ) );
curl_setopt( $ch, CURLOPT_POST, 1 );
} elseif ( 'GET' == $params['type'] && !$params['url_params']['paging'] ) {
// get request
$params['url_params']['access_token'] = $this->_userAccessToken;
//add params to endpoint
$endpoint .= '?' . http_build_query( $params['url_params'] );
}
// general curl options
curl_setopt( $ch, CURLOPT_URL, $endpoint );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $ch );
curl_close( $ch );
$responseArray = json_decode( $response, true );
if ( isset( $responseArray['error_type'] ) ) {
var_dump( $responseArray );
die();
} else {
return $responseArray;
}
}
Above my HTML, I have defined:
<?php
require_once ('ig_conf.php');
// if there is an auth 'code' in the URL, get it and pass it along
// if there is NO auth 'code' in the URL, leave blank and init auth process
$params = array(
'get_code' => isset( $_GET['code'] ) ? $_GET['code'] : '',
'access_token' => $accessToken,
'user_id' => '123456789' // insert actual user ID here to make code work
);
// init class for handling instagram, from ig_config.php
// pass on 'get_code' as $params into instagram_basic_display_api
// ...this gets the authorization url
$ig = new instagram_basic_display_api( $params );
?>
Help is much appreciated. If more code is required, I can provide.
Aucun commentaire:
Enregistrer un commentaire