Simple Web App using Google Ads API

778 views
Skip to first unread message

G

unread,
May 13, 2022, 7:35:26 PM5/13/22
to Google Ads API and AdWords API Forum
Hello,

I'm developing a simple web app using Google Ads API. This web app will be used by the advertisers, and what it does is (1) displaying the list of Customer IDs associated with them (instead of me (developer)), and (2) letting them select one of the customer IDs.

What I'm stuck at is the fact that the 'user' should be dynamic. Most of the documentation.. especially using OAuth 2.0 Playground seems to say that it's using my (developer's) own credentials and set up the GCP project and the OAuth Client to get the client ID and client secret, and generate refresh token using OAuth 2.0 Playground. So, with those, I can only get the list of customer IDs that's associated to my google account, by using CustomerService.listAccessibleCustomers. (Btw, FYI, I'm a JS person so I was using https://github.com/Opteo/google-ads-api as the Google Ads API client, even though it's not an official one.)

What I'm trying to achieve is this:
(1) Let's say my users (who will access my web app) are Advertiser A and Advertiser B.
(2) When Advertiser A access my web app, it should display the list of customer IDs that are associated with Advertiser A, so they can select one of them.
(3) When Advertiser B access my web app, it should display the list of customer IDs that are associated with Advertiser B, so they can select one of them.

How can I achieve this (handling dynamic user to use 'listAccessibleCustomers')?

G

unread,
May 14, 2022, 9:58:46 AM5/14/22
to Google Ads API and AdWords API Forum
I'm still working on it, but maybe I should get refresh token per user by using the authentication to Google APIs (before using Google Ads API). This page is one of the main references I'm looking at: https://github.com/googleapis/google-api-nodejs-client

I'll report back if I figure it out. In the meantime, if you have same or similar experience, please share it. Thanks–

Google Ads API Forum Advisor

unread,
May 19, 2022, 2:19:26 AM5/19/22
to ange...@gmail.com, adwor...@googlegroups.com

Hi,
 

Thanks for reaching out to the Google Ads API Support team.
 

To answer your concerns and questions in general, allow me to reiterate your message "I'm still working on it, but maybe I should get refresh token per user by using the authentication to Google APIs (before using Google Ads API)." In this, you are correct because if you want to separate access in your advertiser, your customers would need to create their own separate OAuth credentials such as their own client ID and client secret, and refresh token. This is so that, their respective requests can only return the list of customer IDs that their user / email address (and associated credentials) have access to.


Also, " I'll report back if I figure it out. In the meantime, if you have same or similar experience, please share it." Our team waits for your report on what you have done.


Regards,

Google Logo
Darwin
Google Ads API Team
 


ref:_00D1U1174p._5004Q2asplo:ref

G

unread,
May 19, 2022, 3:56:58 AM5/19/22
to ads...@forumsupport.google, adwor...@googlegroups.com
Thank you, Darwin!

In my case, I was the person who created my OAuth credentials using the GCP console. My customers (advertisers) only can have their Google account and Google Ads account.

I kinda figured it out and it seems to work for my case. Here's the code snippet (see below). I'm using Node.js (Express.js), btw. The idea is that when the users hit a specific URL, it's supposed to call googleoauth function defined in the handler object.

const { google } = require('googleapis');
const { GoogleAdsApi } = require('google-ads-api');

let oauth2Client;

const handler = {
    googleoauth: async (req, res) => {
        // if no code parameter, then this is an initial login, so redirect to Google OAuth
        if (!req.query.code) {
            oauth2Client = new google.auth.OAuth2(
                '<CLIENT-ID>',
                '<CLIENT-SECRET>',
                '<REDIRECT-URI>'
            );

            const url = oauth2Client.generateAuthUrl({
                access_type: 'offline',
                prompt: 'consent',
                scope: 'https://www.googleapis.com/auth/adwords'
            });
            res.redirect(url);
        }
        //redirect with access code from Google OAuth
        else {
            const { tokens } = await oauth2Client.getToken(req.query.code);
            oauth2Client.credentials = tokens;

            const client = new GoogleAdsApi({
                client_id: '<CLIENT-ID>',
                client_secret: '<CLIENT-SECRET>',
                developer_token: '<DEVELOPER-TOKEN>'
            });

            const customers = await client.listAccessibleCustomers(tokens.refresh_token);

            return res.json(customers);
        }
    }
};

exports.handler = handler;


Google Ads API Forum Advisor

unread,
May 20, 2022, 3:27:03 AM5/20/22
to ange...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for getting back to us.

Moving forward, we are glad to hear that you figured it out and it seems to work on your case. If you have further questions / concern, please feel free to get back to us and provide more details to it so our team can give support to you.

Regards,
Google Logo
Carmela
Google Ads API Team
 


ref:_00D1U1174p._5004Q2asplo:ref
Reply all
Reply to author
Forward
0 new messages