# Updating access token gives 400 error invalid grant

**URL:** <https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611>\
**Category:** API and Webhooks\
**Created:** [July 4, 2022, 11:20am UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611 "2022-07-04T11:20:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagguy999](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/jagguy999/32/33867_2.png) [@jagguy999](https://devforum.zoom.us/u/jagguy999)\
**Post date:** [July 4, 2022, 11:20am UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611/1 "2022-07-04T11:20:08Z")

</div>

Hi, I am following the php tutorial on zoom api php

> **[How to Create a Meeting on Zoom using Zoom API and PHP - Artisans Web](https://artisansweb.net/how-to-create-a-meeting-on-zoom-using-zoom-api-and-php/)**
>
> Are you looking to create a meeting on Zoom using Zoom API and PHP? In this article, I will explain how to interact with Zoom API and generate

I can create the tokens with a callback and store in db. My issue is simply updating the access token gives a 400 error on the refresh token line to be exact. I cant see an issue with my access/refresh token i just created and update. I am using cakephp4 and zoom api.

//my update access token is

```
$zoomOauth=$this->ZoomOauths->find()->first();

$refresh_token = $zoomOauth->refresh_token;
$client = new Client(['base_uri' => 'https://zoom.us']);
$response = $client->request('POST', '/oauth/token', [
   "headers" => [
        "Authorization" => "Basic ". base64_encode($zoom->zoom_client_key.':'.$zoom->zoom_secret_key)
        ],
        'form_params' => [
            "grant_type" => "refresh_token",
            "refresh_token" => $refresh_token //error here? refresh token is correct?
        ],
    ]);
     ;
Client error: POST https://zoom.us/oauth/token resulted in a 400 Bad Request response:

 $newToken=$response->getBody();

```

how do i fix this error as it makes no sense to me

current data from Oauth i have store  
access\_token =\> ‘[REDACTED]’

token\_type =\> ‘bearer’

refresh\_token =\> ‘[REDACTED]’

expires\_in =\> (int) 3599  
scope =\> ‘meeting:write:admin’

the past posts on this topic are too general and i need a code solution

---

<div class="post-metadata">

**Author:** ![jan](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/jan/32/38608_2.png) [@jan](https://devforum.zoom.us/u/jan)\
**Post date:** [July 5, 2022, 1:30pm UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611/2 "2022-07-05T13:30:47Z")

</div>

Did you try:

```auto
$token = json_decode($response->getBody()->getContents());

```

---

<div class="post-metadata">

**Author:** ![jagguy999](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/jagguy999/32/33867_2.png) [@jagguy999](https://devforum.zoom.us/u/jagguy999)\
**Post date:** [July 5, 2022, 1:41pm UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611/3 "2022-07-05T13:41:37Z")

</div>

yes i tried $token = json\_decode($response-\>getBody()-\>getContents());

what happens now is that the code will work if i rerun the callback and get new tokens. If do a debug which is printing the variable contents to screen this will cause an invalid grant and going back and re running the function wont fix the invalid grant error.

```auto

```

---

<div class="post-metadata">

**Author:** ![jan](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/jan/32/38608_2.png) [@jan](https://devforum.zoom.us/u/jan)\
**Post date:** [July 5, 2022, 2:08pm UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611/4 "2022-07-05T14:08:29Z")

</div>

Okay, other option:

I manually check if the token has expired, by checking a “date last modified” field in my database table where I store the access/refresh tokens:

```auto
        $sql = "
            SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(dlm) > expires_in
            FROM zoom_tokens
            WHERE uuid = ?
        ";

```

My dlm column is type datetime, Default CURRENT\_TIMESTAMP and Extra on update CURRENT\_TIMESTAMP.

So I call my refresh token method after this method returns true, _not_ after a failed oAuth call to the API.

This is my refresh method, very similar to yours:

```auto
    public function refreshAccessToken()
    {
        try {
            $client = IoC::make('GuzzleClient', ['base_uri' => 'https://zoom.us']);
            $response = $client->request('POST', '/oauth/token', [
                'headers' => IoC::make('ZoomAuthorization')->getBasicAuthorization(),
                'form_params' => IoC::make('ZoomToken')->getRefreshTokenFormParams(),
            ]);
            $token = json_decode($response->getBody()->getContents());

            IoC::make('ZoomToken')->store($token);
        } catch (ClientException $e) {
            bdump($e, $e->getMessage());
        } catch (Exception $e) {
            bdump($e, $e->getMessage());
        }
    }

```

p.s. that bdump function belongs to the Tracy debugger. You should of course use your own method 😉

---

<div class="post-metadata">

**Author:** ![jagguy999](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/jagguy999/32/33867_2.png) [@jagguy999](https://devforum.zoom.us/u/jagguy999)\
**Post date:** [July 7, 2022, 8:34am UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611/5 "2022-07-07T08:34:19Z")

</div>

Yes i already check manually and it works fine. As I said once an invalid grant error appears through simply debugging(printing) a variable then i cant get the tokens to work again.

```
 $zoom=$this->Admins->find()->first();
  $zoomOauth=$this->ZoomOauths->find()->first();

//check expiry
  $expirytime=$zoomOauth->created+$zoomOauth->expires_in -30;
  $expiry=date('d-m-Y h:i:s',$expirytime);
  if ($expirytime > time() ){ //not expired
	 $this->Flash->success('Access Token expires at '. $expiry .' and doesnt need Updating ');
	return 0;
}

$refresh_token = $zoomOauth->refresh_token;

        

$client = new Client(['base_uri' => 'https://zoom.us']);
$response = $client->request('POST', '/oauth/token', [
   "headers" => [
        "Authorization" => "Basic ". base64_encode($zoom->zoom_client_key.':'.$zoom->zoom_secret_key)
        ],
        'form_params' => [
            "grant_type" => "refresh_token",
            "refresh_token" => $refresh_token
        ],
    ]);

 $time=time();
$newToken = json_decode($response->getBody()->getContents(), true); //if print this then invalid grant is thrown

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/zoomdeveloper/original/3X/6/1/614bdd549b610bbaa46ff934617683a02bdaa03c.png) [@system](https://devforum.zoom.us/u/system)\
**Post date:** [August 6, 2022, 6:34pm UTC](https://devforum.zoom.us/t/updating-access-token-gives-400-error-invalid-grant/71611/6 "2022-08-06T18:34:27Z")

</div>

Closed on August 6, 2022, 6:34pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
