> For the complete documentation index, see [llms.txt](https://edrus.gitbook.io/appgregator-api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://edrus.gitbook.io/appgregator-api-docs/appgregators/marketing/google-ads/before-starting/2.-exchange-authorization-code-for-refresh-and-access-tokens.md).

# 2. Exchange authorization code for refresh and access tokens

To exchange an authorization code for an access token, call the <https://oauth2.googleapis.com/token> endpoint and set the following parameters:

| Fields         | Value                                                                     | Required |
| -------------- | ------------------------------------------------------------------------- | -------- |
| client\_id     | 329011709740-sq6l548671df39l5bingat6kpd0rmuth.apps.googleusercontent.com  | Yes      |
| client\_secret | xxx                                                                       | Yes      |
| code           | 4/0AWgavdeTEPxfXAPtbSUqv2OrbfRPywz7JUhCeZbDV4smVxBOgztU4TzFqNOwC0diUCCPog | Yes      |
| grant\_type    | authorization\_code                                                       | Yes      |
| redirect\_uri  | <http://localhost:5001/callback>                                          | Yes      |

<mark style="color:green;">`POST`</mark> `https://oauth2.googleapis.com/token/`

#### Query Parameters

| Name                                             | Type   | Description                                                               |
| ------------------------------------------------ | ------ | ------------------------------------------------------------------------- |
| client\_id<mark style="color:red;">\*</mark>     | String | 329011709740-sq6l548671df39l5bingat6kpd0rmuth.apps.googleusercontent.com  |
| client\_secret<mark style="color:red;">\*</mark> | String | xxx                                                                       |
| code<mark style="color:red;">\*</mark>           | String | 4/0AWgavdeTEPxfXAPtbSUqv2OrbfRPywz7JUhCeZbDV4smVxBOgztU4TzFqNOwC0diUCCPog |
| grant\_type<mark style="color:red;">\*</mark>    | String | authorization\_code                                                       |
| redirect\_uri<mark style="color:red;">\*</mark>  | String | <http://localhost:5001/callback>                                          |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
  "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
  "expires_in": 3920,
  "token_type": "Bearer",
  "scope": "https://www.googleapis.com/auth/drive.metadata.readonly",
  "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="NodeJs - Axios" %}

```javascript
var axios = require('axios');

var config = {
  method: 'post',
  url: 'https://oauth2.googleapis.com/token?redirect_uri=http://localhost:5001/appgregator/us-central1/googleads/callback&client_id=329011709740-sq6l548671df39l5bingat6kpd0rmuth.apps.googleusercontent.com&client_secret=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&grant_type=authorization_code&code=4/0AWgavdeTEPxfXAPtbSUqv2OrbfRPywz7JUhCeZbDV4smVxBOgztU4TzFqNOwC0diUCCPog',
  headers: { }
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

```

{% endtab %}

{% tab title="cURL" %}

```shell
curl --location --request POST 'https://oauth2.googleapis.com/token?redirect_uri=http://localhost:5001/appgregator/us-central1/googleads/callback&client_id=329011709740-sq6l548671df39l5bingat6kpd0rmuth.apps.googleusercontent.com&client_secret=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&grant_type=authorization_code&code=4/0AWgavdeTEPxfXAPtbSUqv2OrbfRPywz7JUhCeZbDV4smVxBOgztU4TzFqNOwC0diUCCPog'
```

{% endtab %}

{% tab title="PHP - cURL" %}

```php
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://oauth2.googleapis.com/token?redirect_uri=http://localhost:5001/appgregator/us-central1/googleads/callback&client_id=329011709740-sq6l548671df39l5bingat6kpd0rmuth.apps.googleusercontent.com&client_secret=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&grant_type=authorization_code&code=4/0AWgavdeTEPxfXAPtbSUqv2OrbfRPywz7JUhCeZbDV4smVxBOgztU4TzFqNOwC0diUCCPog',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

```

{% endtab %}
{% endtabs %}
