/** * 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())); } }