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;

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/CAFLJBYBs3kpZBf3rgDHGvcVYQqGgSSW485kvTx4QYOJUD5jGSQ%40mail.gmail.com.

Reply via email to