Get google ads account id using access token

221 views
Skip to first unread message

Vikram singh

unread,
Dec 13, 2023, 7:52:59 AM12/13/23
to Google Ads API and AdWords API Forum
I am working on an application where my clients can connect their google account and see the google ad reports and listing. For this I have create credentials with required API and configured the auth page too. From the page clients is able to authorize the app to read/write the ads however this is only provide me the access token but not the ads account list.

To get the ads account listing I am trying to integration google ads php api where I am passing the access token to fetch the ad account id of connected user. 

following is sample of code I am writing but not sure how to proceed to get the ads account id.

require 'google-ads-php/vendor/autoload.php'; // Assuming you have the necessary dependencies installed
// Set up your credentials
$client = new Google_Client();
$client->setAuthConfig('google-credential.json'); // Replace with the path to your credentials JSON file
$client->setAccessType('offline');
use Google\Ads\GoogleAds\GoogleAdsClient;
// Set the access token
$accessToken = 'Token Here';

// Initialize the Google Ads client.
$googleAdsClient = GoogleAdsClient::loadFromStorage();

// Set the access token.
$googleAdsClient->setDeveloperToken($accessToken);

// Make a request to get details about the authenticated customer.
$customerDetails = $googleAdsClient->getCustomer();

// Print or use the client ID as needed.
$clientID = $customerDetails->getResourceName();
echo "Client ID: $clientID";


It is not working and returns following error "Uncaught Error: Class "Google\\Ads\\GoogleAds\\GoogleAdsClient" not found"

Please help.

Google Ads API Forum Advisor

unread,
Dec 13, 2023, 8:56:39 AM12/13/23
to vikram...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

After reviewing your concern, I understand that you are encountering an error while trying to list the accounts via the Google Ads API.

I would recommend you to check this guide on List Accessible Accounts for more information regarding how to fetch the list of customers accessible to you.

In order to assist you further, kindly provide us with the complete API logs (request and response with request-id and request header) generated at your end.

Since you are using the PHP client library, if you haven't enabled the logging yet, I would request you to enable it by referring to this guide PHP.

You can send the details via Reply privately to the author option, or direct private reply to this email.

 
This message is in relation to case "ref:!00D1U01174p.!5004Q02r1Y38:ref"

Thanks,
 
Google Logo Google Ads API Team


Vikram singh

unread,
Dec 13, 2023, 10:18:11 PM12/13/23
to Google Ads API and AdWords API Forum
I am not trying to list all the accounts but trying to get the google ad account id for the customer using the access token. Following is the flow of the sequence.

1. Customer comes to the report page where s/he sees a connect button.
2. On this button, the Google auth popup will open with all permission list like reading Google ads, getting email and profile, and google analytic (declare with scope and add in the project too)
3. On authorization I get an access token as it does not return the account id for the connected user.
4. Using the access token I want to get the Google Ad account id (it shows at top right of the Google Ads dashboard)
5. I show the Google ad reports to my customers with this account id.

I have read the documentation but was not able to figure out how to do it with PHP API or with javascript.

Google Ads API Forum Advisor

unread,
Dec 14, 2023, 3:08:19 AM12/14/23
to vikram...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for getting back to us.

By reviewing your query, I understand that you are trying to get the Google Ad account Id for the customer using the access token. In order to investigate further, kindly provide us with the uncropped UI screenshot including the customer ID to assist you better on this request.

Vikram singh

unread,
Dec 14, 2023, 10:09:23 PM12/14/23
to Google Ads API and AdWords API Forum
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.

Can you please let me know which customer ID you are asking for?  

Google Ads API Forum Advisor

unread,
Dec 15, 2023, 3:37:55 AM12/15/23
to vikram...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for getting back to us.

To retrieve the customer ID, you can use the customer report. You can refer to this sample query to retrieve the customer ID:
SELECT customer.id, customer.descriptive_name, customer.resource_name FROM customer
Also, to see the results, you can try making the request to the search and searchStream endpoints. 

In order to investigate your issue, kindly provide us with the uncropped UI screenshot of the Google Ads account with visible customer ID and updated complete API logs (request and response with request-id and request header) generated at your end.

If you are using a client library and haven't enabled the logging yet, I would request you to enable logging for the specific client library that you are using. You can refer to the guide PHP to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.
Reply all
Reply to author
Forward
0 new messages