【问题标题】:Deploy as Web App programmatically from Apps Script browser Editor从 Apps Script browser Editor 以编程方式部署为 Web App
【发布时间】:2020-02-23 04:44:17
【问题描述】:

我了解在clasp 的帮助下,我们可以以编程方式部署脚本(即使不登录 Apps 脚本编辑器);但是,我想知道是否有类似的函数允许我们直接从 Apps 脚本编辑器调用这些 API。

我发现以下资源有助于通过 HTTP GET 和 POST 请求进行部署 -

不幸的是,除了这个示例 sn-p 之外,我找不到任何可以帮助以类似于我们执行其他脚本服务的方式/方式来调用这些的参考,例如:SpreadsheetAppScriptApp 或 @987654327 @等

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for script.projects.versions.create
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/guides/code_samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/script.projects"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    gapi.client.setApiKey("YOUR_API_KEY");
    return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/script/v1/rest")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.script.projects.versions.create({
      "resource": {}
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

我了解以上内容是 JavaScript,对 Apps 脚本不是特别有用,但是...

尝试查看是否有 gapi.client.script.projects.versions.create 的 Apps 脚本(浏览器编辑器)版本

【问题讨论】:

标签: google-apps-script


【解决方案1】:

没有可用的内置服务,但您可以使用URLFetchApp 直接调用Apps Script REST API

要处理授权,您必须将您的 Apps 脚本项目绑定到 GCP 项目并启用 Apps Script API(在 GCP 项目上)。

然后,您可以在应用脚本项目的清单文件 (appscript.json) 中设置所需的 OAuth 范围。这将允许您调用 ScriptApp.getOAuthToken() 并将结果值用作不记名令牌,当您进行 UrlFetchApp.fetch 调用时将其作为身份验证标头传递。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2021-04-09
    • 2019-07-21
    • 2016-04-27
    • 1970-01-01
    • 2012-07-14
    • 2016-02-26
    • 2021-11-22
    • 2016-08-10
    相关资源
    最近更新 更多