【问题标题】:ScriptApp.getService().getUrl() points to dev URL. How can I get it to point to exec production URL?ScriptApp.getService().getUrl() 指向开发 URL。我怎样才能让它指向 exec 生产 URL?
【发布时间】:2020-02-14 19:38:54
【问题描述】:

ScriptApp.getService().getUrl() 正在生成一个开发 URL。如何让它生成 exec 生产 URL?

【问题讨论】:

  • 这是在网络应用程序中吗?如果它在网络应用程序中,您必须确保在当前的网络应用程序网址中访问它,而不是“为您的最新代码测试网络应用程序”链接

标签: google-apps-script


【解决方案1】:

这个答案怎么样?请认为这只是几个可能的答案之一。

问题和解决方法:

如果你是开启V8运行时,在当前阶段,似乎启用V8运行时,ScriptApp.getService().getUrl()返回类似https://script.google.com/macros/s/###/dev的dev URL,而当禁用V8运行时,它返回类似@的exec URL 987654326@。我认为这可能是 V8 的错误之一。

如果您想在启用 V8 的情况下直接检索 exec URL,作为当前的解决方法,使用the method of projects.deployments.list in Apps Script API 检索它怎么样?您可以在“试用此 API”中对其进行测试。当然,这可以与 Google Apps Script 一起使用。

参考:

如果我误解了您的情况并且这不是您想要的方向,我深表歉意。

【讨论】:

  • 好建议。为了使用该方法,必须授权以下两个范围之一:https://www.googleapis.com/auth/script.deploymentshttps://www.googleapis.com/auth/script.deployments.readonly
  • 您是否有机会解释如何在我的 gmail.com 帐户中使用 project.deployment.liststackoverflow.com/q/68543983/250422
【解决方案2】:

对于到达这里的那些可怜的灵魂,J. G.'s comment 的扩展:确认getService().getURL() 调用的结果取决于最终用户访问的 URL(/exec/dev)。

method documentation 中对此也有明确的说明(不确定之前是否存在),因此它似乎是设计使然:

如果您正在运行开发模式 Web 应用,则返回开发模式 URL。

请注意,要获得正确的 URL,您需要使用解决方法 suggested by Tanaike。使用它时,请记住,它需要standard GCP 来启用 Apps Script API(从技术上讲,您可以为此使用默认设置,但它仅适用于有权访问 system-gsuite/apps-script/ 资源的 G Suite (Google Workspace) 帐户)。

部署 getter 的实现是:

const getDeployments = (options = {}) => {
  const {
    version = 1,
    id = ScriptApp.getScriptId(),
    token = ScriptApp.getOAuthToken(),
    page = "",
    size = 50,
    type = "WEB_APP",
  } = options;

  const uri = `https://script.googleapis.com/v${version}/projects/${id}/deployments`;

  const fullURI = `${uri}?pageSize=${size}${page ? `&pageToken=${page}` : ""}`;

  const params = {
    contentType: "application/json",
    headers: {
      Authorization: `Bearer ${token}`,
    },
    muteHttpExceptions: true,
    method: "get",
  };

  const deps = [];

  const response = UrlFetchApp.fetch(fullURI, params);

  if (response.getResponseCode() !== 200) {
    console.log(response.getContentText());
    return deps;
  }

  const { deployments, nextPageToken } = JSON.parse(response.getContentText());

  const requested = deployments.filter(({ entryPoints }) =>
    entryPoints.some(({ entryPointType }) => entryPointType === type)
  );

  deps.push(...requested);

  if (nextPageToken) {
    deps.push(...getDeployments(options));
  }

  return deps;
};

响应成功后,查看entryPoints集合获取你需要的部署。每个入口点都会有一个 webApp 嵌套对象 - 您对它的 url 属性感兴趣。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多