/** * This function will update the refresh token by the specific user */ public function refreshAndUpdateAccessToken() { $service_bgl360 = new ServiceBgl360($this->wpdb, $this->user_id); $time = new Time(); //Get specific access token $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser($this->user_id); // $this->authentication_bgl360->clear(); // print_r( $userCurrentAccessToken ); // echo "Current logged in access token " . $userCurrentAccessToken[0]['access_token'] . '<br>'; // echo "Current logged in refresh token " . $userCurrentAccessToken[0]['refresh_token'] . '<br>'; // echo "Is access token expired?<br>" . $userCurrentAccessToken[0]['id'] . ' <BR>'; // @todo get access token by refresh token smsf database. // echo "Token is automatically refreshed <br>"; // echo "refresh token " . $userCurrentAccessToken[0]['refresh_token'] . '<br>'; // Set the refresh token $this->authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']); // Set the access token $this->authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']); // Get the refresh token data composed by the set access and refresh tokens $response = $this->authentication_bgl360->getRefreshTokenData($this->authentication_bgl360->getRefreshTokenUrl()); // print_r($response); // echo "This is refresh url => " . $this->authentication_bgl360->getRefreshTokenUrl() . '<br>'; // If there is something wrong with the refresh token provided then response will include "invalid_token" if ($response['error'] == 'invalid_token') { echo "<span style='color:red' >Refresh token is already used to retrieved new access token </span><br>"; //delete the current user's access tokens if ($service_bgl360->deleteAccessToken($this->user_id)) { echo "user id: " . $this->user_id . " <-- deleted <br>"; } } else { echo "<span style='color:green' > New access token updated</span>"; // Update access token and refresh token by the user in SMSF database $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime())); } }
$authentication_bgl360->setRedirectUri('https://app.thesmsfacademy.com.au/bgl360-authenticate.php'); $authentication_bgl360->setAccessTokenUri($authentication_bgl360->getAccessTokenUri()); if ($service_bgl360->isExistAccessToken()) { // Get access token from database $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser(); // If access token is expired then refresh user's current token if ($service_bgl360->isUserAccessTokenExpired($userCurrentAccessToken[0]['expires_in'])) { echo "Access Token is expired <br>"; $authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']); $authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']); $response = $authentication_bgl360->getRefreshTokenData(); if ($response['error'] == 'invalid_token') { echo "Refresh token is already used to retrieved new access token <br>"; } else { // Update access token, refresh token, expired_at and updated_at $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime())); } } else { // Do nothing echo "Access Token is not expired <br>"; } } else { $accessToken = $authentication_bgl360->getAccessToken(); /** * If retrieving access token got an error then should redirect the bgl360 application * I think this can be explore more later */ if ($accessToken['error'] == 'invalid_grant') { echo "Invalid grant <br>"; /** * has invalid response