Md Saif Uddin
Md Saif Uddin

Reputation: 341

Quickbooks PHP api lose access token after 60 minutes

I am using Quickbooks V3 PHP SDK it works but after 60 minutes QuickBooks update the new access token key how can I retrieve that key?

I don't want disconnection after 60 minutes. Waiting for your reply. Advance thanks all.

$get_data = get_option( 'r_opts');
 
$dataService = DataService::Configure(array(
  'auth_mode' => 'oauth2',
  'ClientID' => $get_data['ClientID'],
  'ClientSecret' => $get_data['ClientSecret'],
  'accessTokenKey' =>  $get_data['accessTokenKey'],
  'refreshTokenKey' => $get_data['refresh'],
  'QBORealmID' => $get_data['QBORealmID'],
  'baseUrl' => $get_data['url'],
  'mode' => $get_data['mode']
));

try{

    $dataService->setLogLocation("/Users/bsivalingam/Desktop/newFolderForLog");
    $OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();
    $OAuth2LoginHelper->setLogForOAuthCalls(true, false, "/Users/bsivalingam/Desktop/newFolderForLog");
    $accessToken = $OAuth2LoginHelper->refreshToken();
    $error = $OAuth2LoginHelper->getLastError();

if ($error) {

    echo "False"; 

}

$dataService->updateOAuth2Token($accessToken);
$CompanyInfo = $dataService->getCompanyInfo();
$error = $dataService->getLastError();

}catch(Exception $err){

    echo "False"; 

}

Upvotes: 0

Views: 345

Answers (1)

hppycoder
hppycoder

Reputation: 1026

The safest way to use oAuth2 is to become familiar with the Refresh token approach documented - https://intuit.github.io/QuickBooks-V3-PHP-SDK/authorization.html?highlight=refresh#refresh-your-oauth-2-0-token

You can either call to the refresh directly, or if you upgrade to version 4.0.5+ they provide the following:

<?php
require 'vendor/autoload.php';

use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2LoginHelper;

//The first parameter of OAuth2LoginHelper is the ClientID, second parameter is the client Secret
$oauth2LoginHelper = new OAuth2LoginHelper("YOUR_CLIENT_ID","YOUR_CLIENT_SECRET");
$accessTokenObj = $oauth2LoginHelper->
                    refreshAccessTokenWithRefreshToken("L011529701359ECWqJtK0Co0wFhpsDBevQNbYmhYsiORcr9goo");
$accessTokenValue = $accessTokenObj->getAccessToken();
$refreshTokenValue = $accessTokenObj->getRefreshToken();
echo "Access Token is:";
print_r($accessTokenValue);
echo "RefreshToken Token is:";
print_r($refreshTokenValue);
?>

This helper should make the job easier for when your token expires. Your code needs to handle when the token expires, refresh (as above), and have your code try the request again.

Upvotes: 2

Related Questions