使用 Google Apps 脚本中的高级服务指定 API 版本?

Specify the API version using an Advanced Service within Google Apps Scripts?

我需要在我的脚本中使用内容 API 的 2.1 版,但是,我不确定如何传递版本号。

这是代码的相关部分:

var products = ShoppingContent.Products.list(merchantId, {
    pageToken: pageToken,
    maxResults: maxResults,
    includeInvalidInsertedItems: true
});

我试过 version: 2.1 但没有雪茄。

谢谢

仅当您 enabling a particular advanced service. Not all versions are supported by all client libraries e.g. the Drive advanced service 不支持 v3 端点时才指定特定客户端库的版本。

对于 ShoppingContent 客户端库,Apps 脚本仅提供与版本 2 的绑定:

因此,要使用 v2.1,您需要将购物内容 API 视为 external API, and access it using UrlFetchApp. You will need to authorize the requests as appropriate, constructing your own OAuth2 authorization header with the ScriptApp.getOAuthToken() 方法,例如:

function addAuthHeader(headers) {
  var token = ScriptApp.getOAuthToken();
  headers['Authorization'] = 'Bearer ' + token;
}
function getBaseURI(version) {
  return 'https://www.googleapis.com/content/' + version + '/';
}

function listProducts(merchantId, pageToken) {
  const base = getBaseURI('v2.1');
  const path = merchantId + '/products';
  if (pageToken)
    path + '?pageToken=' + pageToken;

  const headers = {
    /* whatever you need here */
  };
  addAuthHeader(headers);

  const fetchOptions = {
    method: 'GET',
    /* whatever else you need here
      https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params
     */
    headers: headers
  };
  var pageResponse = UrlFetchApp.fetch(base + path, fetchOptions);
  var onePageOfResults = JSON.parse(pageResponse.getContentText());
  /* whatever else */
}