Quickbooks PHP SDK OAuth2,如何获取和存储访问/刷新令牌

问题描述:

我有一个Magento 2.3商店,我正在尝试将一些数据同步到Quickbooks Online. 我已经创建了一个QBO App,但这是我第一次使用oauth,我对如何存储和使用访问/刷新令牌有些困惑.

I have a Magento 2.3 store that I'm trying to sync some data to Quickbooks Online. I've created a QBO App but this is my first time using oauth and I'm a bit confused on how to store and use the access / refresh tokens.

根据Quickbooks文档,我需要存储最新的刷新令牌:

According to Quickbooks doc I need to store the latest refresh token:

每个访问令牌只能在创建后的一个小时内有效.如果您尝试在一个小时后使用相同的访问令牌进行API调用,则该请求将被QBO阻止.这就是刷新令牌的用途.它用于在访问令牌过期后请求新的访问令牌,因此一小时后您仍然可以访问QBO公司.请记住,每当您进行一次refreshToken API调用时,始终在会话或数据库中存储最新刷新令牌值.在QuickBooks Online OAuth 2协议中,它不是您应该存储的访问令牌,而是您需要存储的刷新令牌.

Each access token can only be valid for an hour after its creation. If you try to make an API call after an hour with the same access token, the request will be blocked by QBO. That is what refresh token used for. It is used to request a new access token after access token expired, so you can still access to the QBO company after an hour. Just remember, whenever you make a refreshToken API call, always STORE THE LATEST REFRESH TOKEN value in your session or database. In QuickBooks Online OAuth 2 protocol, it is not the access token you should store, it is the refresh token you need to store.

所以我的问题是,每当我的API调用同步数据时,如何正确存储和调用刷新令牌以生成新的访问令牌.

So my question is, how do I properly store and call upon my refresh token to generate a new access token each time my API makes a call to sync data.

当前,我通过将OAuth令牌硬编码到我的帮助文件中来直接使用它们:

Currently, I'm directly using my OAuth tokens by hard coding them into my helper file:

<?php
namespace Company\Module\Helper;

use QuickBooksOnline\API\DataService\DataService;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
  public function getConfigurationSetting()
  {
    $dataService = DataService::Configure(array(
      'auth_mode' => 'oauth2',
      'ClientID' => '<<my ClientID',
      'ClientSecret' => '<<my ClientSecret>>',
      'accessTokenKey' => 'xxxxxx',
      'refreshTokenKey' => 'xxxxxx',
      'QBORealmID' => "123xxxxxxx",
      'baseUrl' => 'Development'
    ));

    $OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();
    $refreshedAccessTokenObj = $OAuth2LoginHelper->refreshToken();
    $error = $OAuth2LoginHelper->getLastError();
    if ($error){
      $dataService->throwExceptionOnError(true);
    } else {
      $dataService->updateOAuth2Token($refreshedAccessTokenObj);
    }
    return $dataService;
  }
}

然后我从控制器中调用它:

And then I'm calling that from my controller:

<?php
namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use QuickBooksOnline\API\DataService\DataService;

class CreateQbInvoice implements ObserverInterface
{

  protected $helperData;

  public function __construct(
    \Company\Module\Helper\Data $helperData
  ){
    $this->helperData = $helperData;
  }

  public function execute()
  {
    // Prep Data Services
    $dataService = $this->helperData->getConfigurationSetting();
...

现在这可以解决,直到我的访问令牌到期并且我需要生成一个新的令牌为止,我只是不确定如何更新我的访问令牌并正确存储新的刷新令牌,以使对我的应用程序的访问始终保持刷新状态.> 一旦获得访问令牌,

Now this works until my access token expires and I need to generate a new one, I'm just not sure how to update my access token and store the new refresh token properly to keep access to my app always refreshed.

.使用它来获取令牌并刷新令牌. 您将获得令牌,刷新令牌,令牌到期,刷新令牌到期 用当前时间将所有数据保存在数据库中.

once you get access token. use that to get token and refresh token. you will get token, refresh token, expiry for token, expiry for refresh token save all data in database with current time.

QuickBook令牌的

将在几个小时后过期,但刷新令牌的有效期最长为一年. 因此,对于每个请求,您将首先检查令牌是否过期,并获取带有刷新令牌的新令牌.刷新令牌将返回令牌,而新的刷新令牌将替换上一个令牌

for QuickBook token will expire after few hours but refresh token will not expire up to 1 year. so for every request you will first check if token expire get new token with refresh token. refresh token will return token and new refresh token replace that will previous one