This PHP script allows you to generate an OAuth2 refresh token for the Google Ads API using the Desktop application flow. The refresh token can be used to obtain access tokens for authenticating requests against the Google Ads API.
Tested up to V15
- PHP installed on your system. (PHP 8.1 Required)
- Google Ads API PHP client library installed.
git clone https://github.com/googleads/google-ads-php.git
- You can install it using Composer:
composer install
-
Clone or download the Google Ads PHP API client library from the official repository: google-ads-php.
-
Navigate to the directory where the script is located:
cd path/to/google-ads-php/examples/Authentication
- Copy & open the auth.php file and set the REDIRECT_URI variable to your desired redirect URI.
private const REDIRECT_URI = 'INSERT_REDIRECT_URL_HERE';
- Run the script:
php path/to/google-ads-php/examples/Authentication/auth.php
or if you have copied the file into the google-ads-php root directory;
php path/to/google-ads-php/auth.php
-
Follow the prompts to enter the OAuth2 client ID, client secret, and any additional scopes (if needed).
-
Visit the generated authorization URL in your web browser.
-
Log into the Google account associated with your Google Ads.
-
After approval, enter the state variable when prompted by the script.
-
Next, enter the authorization code provided by the Google OAuth2 server.
-
The script will output your refresh token. Copy this token.
-
Update your google_ads_php.ini file with the following information:
[GOOGLE_ADS]
developerToken = "INSERT_DEVELOPER_TOKEN_HERE"
; Optional: Specify the login customer ID used to authenticate API calls.
; loginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
[OAUTH2]
clientId = "YOUR_OAUTH2_CLIENT_ID"
clientSecret = "YOUR_OAUTH2_CLIENT_SECRET"
refreshToken = "YOUR_REFRESH_TOKEN"
Replace "INSERT_DEVELOPER_TOKEN_HERE", "INSERT_LOGIN_CUSTOMER_ID_HERE", "YOUR_OAUTH2_CLIENT_ID", "YOUR_OAUTH2_CLIENT_SECRET", and "YOUR_REFRESH_TOKEN" with your actual values.
Contributions are always welcome!