How do I get invoice information using REST in Google Ads API?

Viewed 189

I am now looking to get invoice info using Google Ads API I tried to code the following and run it in GoogleAppScript, but all I got was an error message "Request contains an invalid argument.

const access_token = "XXXXXXXXXXXXXXXXXX";
const developer_token = "XXXXXXXXXXXXXXXXXX";
const mcc_account = "XXXXXXXXXX";
const customerId = "XXXXXXXXXXXXX";
const billing_Setup = "XXXXXXX";
const issue_Year = "2022";

function download_invoices(access_token, developer_token, mcc_account, customerId, billing_Setup, issue_Year) {
  const billingSetup = encodeURIComponent(`customers/${customerId}/billingSetups/${billing_Setup}`);
  const url = `https://googleads.googleapis.com/v10/customers/${customerId}/invoices?billingSetup=${billingSetup}&issueYear=${issue_Year}&issueMonth=MARCH`;
  const headers = {
    "Authorization": "Bearer " + access_token,
    "developer-token": "Bearer " + developer_token,
    "login-customer-id": "Bearer " + mcc_account
  };
  const options = {
    "method": "get",
    "headers": headers,
  };
  const httpResponse = UrlFetchApp.fetch(url, options);
  const status = httpResponse.getResponseCode();
  if (status !== 200) {
    throw "HttpRequestError";
  }
  const response = JSON.parse(httpResponse.getContentText());
  return response;
}

By the way, I am able to get the access token without any problem.

const client_Id = "XXXXXX";
const client_Secret = "XXXXXXXXXXXXXXXXXX";
const refresh_Token = "XXXXXXXXXXXXXXXXXX";

function get_accesstoken(client_Id, client_Secret, refresh_Token) {
  const url = "https://accounts.google.com/o/oauth2/token";
  const opt = {
        "method" : "POST",
        "payload" : {
            "client_id" : client_Id,
            "client_secret" : client_Secret,
            "refresh_token" : refresh_Token,
            "grant_type" : "refresh_token"
        },
        "muteHttpExceptions" : true
  };
  const httpResponse = UrlFetchApp.fetch(url, opt);
  const status = httpResponse.getResponseCode();
  if (status !== 200) {
    throw 'HttpRequestError';
  }
  const response = JSON.parse(httpResponse.getContentText());
  return response;
}

Is there something missing in this code?

All codes were made with the following reference.

https://developers.google.com/google-ads/api/rest/reference/rest/v10/customers.invoices/list#google.ads.googleads.v10.services.InvoiceService.ListInvoices

Result : I thought I could get the invoice information tied to my account, but all I got was an error "Request contains an invalid argument.

Exception: Request failed for https://googleads.googleapis.com returned code 400. Truncated server response: {
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
... (use muteHttpExceptions option to examine full response)
1 Answers

When I saw the official document of Method: customers.invoices.list, it seems that billingSetup, issueYear, issueMonth are required to be used as the query parameter. I thought that your error of Request contains an invalid argument. might be due to this. In this case, how about the following modification?

From:

const url = "https://googleads.googleapis.com/v10/customers/" + customerId + "/invoices";
const headers = {
  "Authorization": "Bearer " + access_token,
  "developer-token": "Bearer " + developer_token,
  "login-customer-id": "Bearer " + mcc_account
};
const options = {
  "method": "get",
  "headers": headers,
  "billingSetup": "XXXXXXX",
  "issueYear": "2022",
  "issueMonth": MonthOfYear.MARCH
};

To:

const billingSetup = encodeURIComponent(`customers/${customerId}/billingSetups/${billing_Setup}`); // Added
const url = `https://googleads.googleapis.com/v10/customers/${customerId}/invoices?billingSetup=${billingSetup}&issueYear=${issue_Year}&issueMonth=MARCH`; // Updated
const headers = {
  "Authorization": "Bearer " + access_token,
  "developer-token": "Bearer " + developer_token,
  "login-customer-id": "Bearer " + mcc_account
};
const options = {
  "method": "get",
  "headers": headers,
};

Note:

  • This modification suposses that your tokens in the request header and each value are valid values. Please be careful this.

Reference:

Related