简体   繁体   中英

Deploy as Web App programmatically from Apps Script browser Editor

I understand with the help of clasp , we can deploy a script programmatically (even without logging in to the Apps Script editor); however, I was wondering if there are similar functions that allow for us to invoke those APIs directly from the Apps Script editor.

I found the following resources that help with deploying via HTTP GET and POST requests -

Unfortunately, I couldn't find any reference apart from this example snippet that could help to invoke these in a way similar to what/how we do other script services ex: SpreadsheetApp or ScriptApp or PropertiesService etc.

<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>

I understand the above is JavaScript and not specifically useful for Apps Script but...

Trying to see if there's an Apps Script (browser editor) version of gapi.client.script.projects.versions.create

There is no built-in service available, but you can call the Apps Script REST API directly using URLFetchApp .

To handle authorization you'll have to bind your Apps Script project to a GCP project and enable the Apps Script API (on the GCP project).

Then you can set the required OAuth scopes in your apps script project's manifest file (appscript.json). That will allow you to call ScriptApp.getOAuthToken() and use the resulting value as a bearer token that you pass as an Authentication header when you make your UrlFetchApp.fetch calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM