How to fix Invalid JSON payload error from the Google Ads API
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.