This is the javascript code to get the user consent for the scope and access token
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Ads API Authentication</title>
<!-- Include the Google Sign-In and Google API client libraries -->
<script src="
https://apis.google.com/js/platform.js" async defer></script>
<script src="
https://apis.google.com/js/api.js"></script>
</head>
<body>
<h1>Google Ads API Authentication</h1>
<div id="google-signin-button"></div>
<script>
const API_KEY = 'GOCSPX-**************';
const CLIENT_ID = '***********.
apps.googleusercontent.com';
function handleClientLoad() {
gapi.load('auth2', initGoogleAuth);
}
function initGoogleAuth() {
gapi.auth2.init({
client_id: CLIENT_ID,
apiKey: API_KEY,
scope: '
https://www.googleapis.com/auth/adwords'
}).then(() => {
renderGoogleSignInButton();
});
}
function renderGoogleSignInButton() {
gapi.signin2.render('google-signin-button', {
scope: '
https://www.googleapis.com/auth/adwords',
width: 200,
height: 40,
longtitle: true,
theme: 'dark',
onsuccess: onGoogleSignInSuccess,
onfailure: onGoogleSignInFailure
});
}
function onGoogleSignInSuccess(googleUser) {
const authResponse = googleUser.getAuthResponse();
const accessToken = authResponse.access_token;
fetchGoogleAdsAccounts(accessToken)
}
function onGoogleSignInFailure(error) {
console.error('Google Sign-In failed:', error);
}
function fetchGoogleAdsAccounts(accessToken) {
/* Rest API or function to get the Google ad customer ID */
}
</script>
<!-- Load the Google API client library -->
<script src="
https://apis.google.com/js/api.js?onload=handleClientLoad"></script>
</body>
</html>
In the function `fetchGoogleAdsAccounts` I consume the rest API built in PHP as mentioned in the earlier email.