# Error on refreshing access token

**URL:** <https://integration.keap.com/t/error-on-refreshing-access-token/38690>\
**Category:** API Q&A\
**Created:** [April 30, 2018, 6:07am UTC](https://integration.keap.com/t/error-on-refreshing-access-token/38690 "2018-04-30T06:07:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunny\_Gajjar](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Sunny\_Gajjar](https://integration.keap.com/u/Sunny_Gajjar)\
**Post date:** [April 30, 2018, 6:07am UTC](https://integration.keap.com/t/error-on-refreshing-access-token/38690/1 "2018-04-30T06:07:51Z")

</div>

Hello Team,

I am stored my token objects in DB.When refresh the token that time i used below code:

require\_once ‘New\_IS/vendor/autoload.php’;  
$infusionsoft = new \Infusionsoft\Infusionsoft(array(  
‘clientId’ =\> ‘XXXXXXXXXXXXXXXXXXX’,  
‘clientSecret’ =\> ‘XXXXXXX’,  
‘redirectUri’ =\> ‘[http://abc.com/abc.php](http://abc.com/abc.php)’,  
));  
$conn=mysqli\_connect($host,$username,$password,$db);  
$qrySel=“SELECT \* FROM `ZZZ` WHERE id=1”;  
$rsltOp=mysqli\_query($conn,$qrySel);  
$rowGetInfo=mysqli\_fetch\_assoc($rsltOp);  
$infusionsoft-\>setToken(unserialize($rowGetInfo[“token”]));  
$infusionsoft-\>refreshAccessToken();

**for some time my code work fine, and then suddenly i am getting error like,**

Fatal error: Uncaught exception ‘GuzzleHttp\Exception\ClientException’ with message 'Client error: `POST https://api.infusionsoft.com/token` resulted in a `400 Bad Request` response:  
{“error”:“invalid\_grant”,“error\_description”:“Invalid refresh token”}  
’ in /home4/steamline/public\_html/BWS/New\_IS/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113  
Stack trace:  
#0 /home4/steamline/public\_html/BWS/New\_IS/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))  
#1 /home4/steamline/public\_html/BWS/New\_IS/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))  
#2 /home4/steamline/public\_html/BWS/New\_IS/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)  
#3 /home4/steamline/public\_html/BWS/New\_IS/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\Promise in /home4/steamline/public\_html/BWS/New\_IS/vendor/infusionsoft/php-sdk/src/Infusionsoft/Http/GuzzleHttpClient.php on line 74

---

<div class="post-metadata">

**Author:** ![bradb](https://sea1.discourse-cdn.com/flex019/user_avatar/integration.keap.com/bradb/32/13_2.png) [@bradb](https://integration.keap.com/u/bradb)\
**Post date:** [April 30, 2018, 5:23pm UTC](https://integration.keap.com/t/error-on-refreshing-access-token/38690/2 "2018-04-30T17:23:35Z")

</div>

This is almost always caused by a bad refresh token. Typically this is due to unsafe threading when updating the token. A refresh token can only be used once to get a new access token. If two requests come into to update the token at the same time, then one will fail without proper thread synchronization.

---

<div class="post-metadata">

**Author:** ![Sunny\_Gajjar](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Sunny\_Gajjar](https://integration.keap.com/u/Sunny_Gajjar)\
**Post date:** [May 1, 2018, 7:07am UTC](https://integration.keap.com/t/error-on-refreshing-access-token/38690/3 "2018-05-01T07:07:35Z")

</div>

Thanks @bradb

Can you give me a sample code for how to refresh the access token?

Thanks
