Home > other >  Get data from Google Ads API using App Script
Get data from Google Ads API using App Script

Time:01-16

I wanna get out campaigns reports using Google Rest API and it does'nt work in Google ads Apps script.

My code:

function main() {
  
const API_VERSION = "12";
const CUSTOMER_ID = "***"; //contais real custommer ID
const DEVELOPER_TOKEN = "***"; //contais real developper ID
const MANAGER_CUSTOMER_ID = "***"; //contais real manager ID
const OAUTH2_ACCESS_TOKEN = ""; //contais real ACCES TOKEN

const data = {
  "pageSize": 10000,
  "query": "SELECT ad_group_criterion.keyword.text, ad_group_criterion.status FROM ad_group_criterion WHERE ad_group_criterion.type = 'KEYWORD' AND ad_group_criterion.status = 'ENABLED'"
};

const url = `https://googleads.googleapis.com/v${API_VERSION}/customers/${CUSTOMER_ID}/googleAds:search`;
const options = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "developer-token": DEVELOPER_TOKEN,
    "login-customer-id": MANAGER_CUSTOMER_ID,
    "Authorization": `Bearer ${OAUTH2_ACCESS_TOKEN}`
  },
  body:  JSON.stringify(data), 
  "muteHttpExceptions": true
};

Logger.log(UrlFetchApp.fetch(url, options));
}

Result error: { "error": { "code": 400, "message": "Request contains an invalid argument.", "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.ads.googleads.v12.errors.GoogleAdsFailure", "errors": [ { "errorCode": { "queryError": "UNEXPECTED_END_OF_QUERY" }, "message": "Error in query: unexpected end of query." } ], "requestId": "zKBR9-dJoG9NWAx3iJea2g" } ] } }

But query is valid https://developers.google.com/google-ads/api/fields/v11/query_validator enter image description here

Could you plese help?

Thanks

I wanna get out campaigns reports using Google Rest API and it does'nt work. My code and result is above.

CodePudding user response:

Based on the documentation of UrlFetchApp, 'body' is not the correct option for passing in the query. You want 'payload'.

const options = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "developer-token": DEVELOPER_TOKEN,
    "login-customer-id": MANAGER_CUSTOMER_ID,
    "Authorization": `Bearer ${OAUTH2_ACCESS_TOKEN}`
  },
  payload:  JSON.stringify(data), 
  "muteHttpExceptions": true
};
  • Related