I'm trying to pull a report from the Google Ads API into Google sheets and I can't get the API to recognize my query as a query

Here's the code and error I'm getting:

    function basicReport() {   var query = {     "query" : "SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id"   };      var body = JSON.stringify(query);      var head = {     'Developer-token' : "<Dev token>",     'login-customer-id' : <Manager ID>,     'Authorization' : "Bearer <Auth token>",   }; var options = {   'method' : 'POST',   'content-type': 'application/json',   'headers' : head,   'payload' : body,   'muteHttpExceptions' : true };    var response = UrlFetchApp.fetch('https://googleads.googleapis.com/v4/customers/<Customer ID>/googleAds:searchStream', options);   var json = response.getContentText();   var data = JSON.parse(json); 

But I constantly get the error:

"error": {     "code": 400,     "message": "Invalid JSON payload received. Unknown name \"{\"query\":\"SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id\"}\": Cannot bind query parameter. Field '{\"query\":\"SELECT campaign' could not be found in request message.",     "status": "INVALID_ARGUMENT",     "details": [       {         "@type": "type.googleapis.com/google.rpc.BadRequest",         "fieldViolations": [           {             "description": "Invalid JSON payload received. Unknown name \"{\"query\":\"SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id\"}\": Cannot bind query parameter. Field '{\"query\":\"SELECT campaign' could not be found in request message." 

I've run the query in OAuth playground (https://developers.google.com/oauthplayground) and it worked there, so I know the query is ok.

I've tried passing the body as an object not a string, but then I get a 500 error.

Tag:google-ads-api, google-api, google-apps-script

Only one comment.

  1. John Musson

    In case anyone else is looking for this - I did solve it.

    When set in the header, Content-Type has a dash, when set in the options, contentType does not.

    function basicReport() { var query = { "query" : "SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id" }; var body = JSON.stringify(query); var head = { 'developer-token' : "<Dev token>", 'login-customer-id' : "<Manager ID>", 'authorization' : "Bearer <Auth token>", 'accept' : 'application/json' 'Content-Type': 'application/json', }; var options = { 'method' : 'POST', 'headers' : head, 'payload' : body, 'muteHttpExceptions' : true }; var response = UrlFetchApp.fetch('https://googleads.googleapis.com/v4/customers/<Customer ID>/googleAds:searchStream', options); var json = response.getContentText(); var data = JSON.parse(json); Logger.log(data);

Add a new comment.