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);
}