We have recently updated the Magento platform from version 2.2.3 to 2.2.5 to one of our clients and faced a painstaking issue: every API call return us an error message: Consumer is not authorized to access %resources
.
The full stack trace of error is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "message": "Consumer is not authorized to access %resources", "parameters": { "resources": "Magento_Sales::sales" }, "trace": "#0 /var/www/html/vendor/magento/module-webapi/Controller/Rest/RequestValidator.php(68): Magento\\Webapi\\Controller\\Rest\\RequestValidator->checkPermissions()\n#1 /var/www/html/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php(86): Magento\\Webapi\\Controller\\Rest\\RequestValidator->validate()\n#2 /var/www/html/vendor/magento/module-webapi/Controller/Rest.php(322): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver->resolve()\n#3 /var/www/html/vendor/magento/module-webapi/Controller/Rest.php(239): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#4 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#5 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#6 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#7 /var/www/html/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(39): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callPlugins('dispatch', Array, Array)\n#8 /var/www/html/vendor/magento/framework/App/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#9 /var/www/html/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\\Framework\\App\\Http->launch()\n#10 /var/www/html/vendor/magento/framework/App/Bootstrap.php(256): Magento\\Framework\\App\\Http\\Interceptor->launch()\n#11 /var/www/html/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#12 {main}" } |
We looked for the solution to this issue on GitHub, but couldn’t find it there, so we began the debugging process. What caused this issue was the method \Magento\Webapi\Model\Authorization\TokenUserContext::isTokenExpired.
Migration to Magento 2
Take your online store to the next level with BelVG Magento migration
Visit the pageAs soon as we found the cause of the issue we checked this file on a 2.2-develop
branch on GitHub and found a patch. After that we found an issue on GitHub:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private function isTokenExpired(Token $token): bool { if ($token->getUserType() == UserContextInterface::USER_TYPE_ADMIN) { $tokenTtl = $this->oauthHelper->getAdminTokenLifetime(); } elseif ($token->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) { $tokenTtl = $this->oauthHelper->getCustomerTokenLifetime(); } else { // other user-type tokens are considered always valid return false; } if (empty($tokenTtl)) { //this part should be added return false; //to avoid the token issue } if ($this->dateTime->strToTime($token->getCreatedAt()) < ($this->date->gmtTimestamp() - $tokenTtl * 3600)) { return true; } return false; } |
Since this issue is relevant to the currently available version of Magento (the current version now is 2.2.5), I hope you will find this article helpful.
Magento Development
Take your online store to the next level with BelVG Magento development
Visit the pageLooking for a reliable partner to migrate your Magento store to the newest version? Turn to BelVG!
Hi, Rajesh. Thanks for your question.
The decision is stated in the article
– You can create your own module that will override the method and add new lines
– You can make a composer patch from the git link we gave in the article
Yes, I get This Error with Shiprocket, But What is the Solution For This Error ? Please Help