Problem/Motivation
Sometime we got token error, coz refresh fun on preprecces didn't run
Steps to reproduce
Create webform and attach b24 integration
For some reason token refresh on preprocess func not work correctly and we got error like this one:
Bitrix24 method «crm.lead.add» error: The access token provided has expired..
Proposed resolution
Is it better to check token before request is send?
In the current solution we have a service with the method 'get':
$request = $this->httpClient->get($uri);
$this->refreshAccessToken();
return Json::decode($request->getBody());
So we send a request and after that refresh token each time. My purpose: update token before request is send or check token expired time inside service get method:
$this->refreshAccessToken();
$request = $this->httpClient->get($uri);
return Json::decode($request->getBody());
$expires = \Drupal::state()->get('b24_token_expires');
$current_time = \Drupal::service('datetime.time')->getCurrentTime();
if (($expires - $current_time) < 1800) {
$this->refreshAccessToken();
}
$request = $this->httpClient->get($uri);
return Json::decode($request->getBody());
It makes sense coz probably we don't need refreshed tokens after request
Issue fork b24-3221679
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 8.x-1.x
changes, plain diff MR !2
- 3221679-access-token-refresh
changes, plain diff MR !1
Comments
Comment #5
artem_kondra commentedComment #8
alezu commentedHi @pi0ne3r,
Thanks for the fix, I've merged it in a new release.