1
votes

I have a Google Apps Script bound to a spreadsheet that needs to read AdWords data. I'm avoiding the user permission OAuth2 procedures because it's an entirely server-to-server script that automatically updates daily data to the sheet.

After much swimming through documentation, I've created a service account and given it the necessary permissions as per this guide: https://developers.google.com/adwords/api/docs/guides/authentication#oauth2_service_accounts

I haven't fully understood the next step, which is how to actually make the request, specifically in Apps Script. What would be the code for this?

Also accepting better suggestions on the most elegant way to request the AdWords API on Apps Script without client permissions. I'd managed to get a refresh token which seemed to be the right track, but couldn't generate an OAuth2 token with it.

1

1 Answers

1
votes

I've found that a service account is not required, and the best way is to follow the installed app flow.

Here's the simplest way to request the AdWords API on Google Apps Script:

  1. Create a client ID and client secret as described here.
  2. Follow the OAuth2 Playground process to get a refresh token as described here.
  3. Get your developer token by following this process. Note: You'll have to create/use a separate manager account, apply for a token, and wait for its approval. You could alternatively use a test account.
  4. Find your client customer ID here - after logging in it'll be on the top right in the form XXX-XXX-XXXX.
  5. Create your AWQL query (not sure if this guide works the same for XML).

function getGoogleAccessToken() {

    // This is also a general method to fetch an access token using a refresh token in GAS.

    var params = {
        client_id: YOUR_CLIENT_ID,
        client_secret: YOUR_CLIENT_SECRET,
        refresh_token: YOUR_REFRESH_TOKEN,
        grant_type: 'refresh_token'
    }

    const options = {
        contentType: 'application/x-www-form-urlencoded',
        method: 'post',
        muteHttpExceptions: true, // To see the whole response in case of errors
        payload: params
    }

    var url = "https://www.googleapis.com/oauth2/v4/token";

    var data = UrlFetchApp.fetch(url, options);
    return (JSON.parse(data).access_token);
}

function getAdwordsData() {

    var adwordsParams = {
        "__fmt": "CSV",
        "__rdquery": YOUR_VALID_AWQL_QUERY
    }

    var adwordsOptions = {
        contentType: "application/x-www-form-urlencoded",
        method: 'post',
        muteHttpExceptions: true,
        payload: adwordsParams
    };

    adwordsOptions.headers = {
        "Authorization": "Bearer " + getGoogleAccessToken(),
        "developerToken": YOUR_DEVELOPER_TOKEN,
        "clientCustomerId": YOUR_CLIENT_CUSTOMER_ID",
        "includeZeroImpressions": true,
        "Expect": "100-continue",
        "User-Agent": "curl, gzip",
        "Accept": "*/*",
        "Accept-Encoding": "gzip"
    };

    var url = 'https://adwords.google.com/api/adwords/reportdownload/v201710';
    var res = UrlFetchApp.fetch(url, options);
    Logger.log(res);
}