Home > Mobile >  Error when using Google Ads API REST interface to change campaign budget through Google Apps Script
Error when using Google Ads API REST interface to change campaign budget through Google Apps Script

Time:07-27

I'm simply trying to construct a test script that can change/mutate a specific campaign budget using the REST interface of the Google Ads API on Google Apps Scripts but I keep on running into the following error:

Exception: Request failed for https://googleads.googleapis.com returned code 400. Truncated server response: { "error": { "code": 400, "message": "Invalid JSON payload received. Unexpected token.\nvalidateOnly=true&pa\n^", "status": "INVALID_... (use muteHttpExceptions option to examine full response)

The relevant function code is as follows:

//API specific variables
const developer_token = {DEVELOPER TOKEN};
const parent_mcc_id = "xxxxxxxxxx";

//Temporary placeholder values
var child_customer_id = "xxxxxxxxxx";
var budget_id = "xxxxxxxxxx";

  let headers = {
     Authorization: "Bearer "   ScriptApp.getOAuthToken(),
     "developer-token": developer_token,
     "login-customer-id": parent_mcc_id
  };

  //Make API call to retrieve each Google Ads account
  try{
    let requestParams = {
     method: "POST",
     contentType: "application/json",
     headers: headers,
     payload: {
       operations:
       [
        {
          updateMask: "amount_micros",
          update:{
            resourceName: "customers/"   child_customer_id   "/campaignBudgets/"   budget_id,
            amountMicros: "60000000"
          }
        }
       ],
      "partialFailure": true,
      "validateOnly": true,
      "responseContentType": "RESOURCE_NAME_ONLY"
      }
    }
    

    var url = ("https://googleads.googleapis.com/v11/customers/{CHILD ACCOUNT ID}/campaignBudgets:mutate");

    Logger.log(requestParams.payload);

    var postChange = UrlFetchApp.fetch(url, requestParams);

  }
  catch(e) {
   Logger.log(e);
  }

I have used similar functions with queries in the payload portion to get data through the Google Ads API, place the data in an array and dump it into a spreadsheet, so I know that my developer token, mcc ids, and child account ids are correct. Any help would be greatly appreciated!

CodePudding user response:

To me it looks as if the payload is sent in x-www-form-urlencoded format based on the "validateOnly=true&pa" snippet in the error text.

You'll need to send an actual JSON object instead (see the examples for reference). I'm not sure how this is best done using UrlFetchApp, but this response seems to indicate that you'll have to use JSON.stringify in order to coerce the object into a string before sending it as payload.

  • Related