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
         * then redirect to application authenticate the data
         */
        $authentication_bgl360->redirectUrlToAuthorizationPage();
    } else {
        echo "Insert new access token<br>";
        // If user don't have access token yet - so new access token must be inserted
        $service_bgl360->insertNewAccessToken(array('user_id' => $current_user->ID, 'access_token' => $accessToken['access_token'], 'token_type' => $accessToken['token_type'], 'refresh_token' => $accessToken['refresh_token'], 'expires_in' => $accessToken['expires_in'], 'scope' => $accessToken['scope'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
    } else {
        // @todo get access token by refresh token smsf database.
        echo "Token is automatically refreshed <br>";
        $authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']);
        $authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']);
        $response = $authentication_bgl360->getRefreshTokenData();
        print_r($response);
        if ($response['error'] == 'invalid_token') {
            echo "Refresh token is already used to retrieved new access token <br>";
        } else {
            //Update access token and refresh token
            $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
        }
    }
} else {
    $accessToken = $authentication_bgl360->getAccessToken();
    PRINT_R($accessToken);
    if ($accessToken['error'] == 'invalid_grant') {
        echo "invalid grant so redirect <br>";
        /**
         * has invalid response
         * then redirect to application authenticate the data
         */
        $authentication_bgl360->redirectUrlToAuthorizationPage();
    } else {
        echo "inserting new data<br>";
        /**
         * If user don't have access token yet - so must be inserted
         */
        $service_bgl360->insertNewAccessToken(array('user_id' => $current_user->ID, 'access_token' => $accessToken['access_token'], 'token_type' => $accessToken['token_type'], 'refresh_token' => $accessToken['refresh_token'], 'expires_in' => $accessToken['expires_in'], 'scope' => $accessToken['scope'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
    }