Simple Method for using ggsrun

Gists

This is a simple method for using ggsrun. ggsrun is a CLI tool to execute Google Apps Script (GAS) on a terminal. Also this CLI tool can be used for managing files in Google Drive for OAuth2 and Service Account.

When you use ggsrun, it is required to retrieve the client ID and client secret and/or the service account at Cloud Platform Project. But, there is the case that you want to simply test or you want to just upload and download the files for Google Drive. In this post, I would like to introduce how to simply use ggsrun.

I thought that when ggsrun can be used simply, it might be useful for simply managing Google Drive, for example, upload and download files and search files, and so on.

Usage

1. Get ggsrun.

Please get ggsrun. Ref

$ go get -u github.com/tanaikech/ggsrun

Or, you can also get it at the release page.

2. Create Google Apps Script project.

In this method, Google Apps Script is used. Please create a project of Google Apps Script.

If you want to directly create it, please access https://script.new/. In this case, if you are not logged in to Google, the log-in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.

3. Set scopes and APIs.

In order to use ggsrun, as the next step, please set the scopes and APIs.

You can set the scopes and APIs at the manifest file (appsscript.json). Please open appsscript.json. Ref And, please set appsscript.json as follows.

{
  "timeZone": "Asia/Tokyo",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Drive",
        "version": "v2",
        "serviceId": "drive"
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/drive.scripts"
  ]
}
  • In this sample script, the timeZone of the script is not used. But, if you want to set your timeZone, please set your timeZone in the above JSON object.

  • In this sample, Drive API is used. But, if you want to use other scopes and APIs, please add them.

4. Sample script.

Please copy and paste the following script to the script editor of the created Google Apps Script project. This script creates ggsrun.cfg for using ggsrun.

function createConfigOfggsrun() {
  const obj = {
    script_id: "###",
    client_id: "###",
    client_secret: "###",
    refresh_token: "###",
    access_token: ScriptApp.getOAuthToken(),
    expires_in: Math.floor((new Date().getTime() + 60 * 60 * 1000) / 1000),
    scopes: [],
  };
  DriveApp.createFile(
    "ggsrun.cfg",
    JSON.stringify(obj, null, "\t"),
    MimeType.PLAIN_TEXT
  );
}

5. Testing.

Please run the function createConfigOfggsrun at the script editor. By this, the authorization screen is opened. Please authorize the scopes. By this, a file of ggsrun.cfg is created on the root folder of Google Drive as shown in the following data. You can use this file for using ggsrun. Please download this file to your local PC in the same directory of ggsrun. By this, you can use ggsrun.

{
  "script_id": "###",
  "client_id": "###",
  "client_secret": "###",
  "refresh_token": "###",
  "access_token": "your access token",
  "expires_in": 123456789,
  "scopes": []
}

IMPORTANT.

This method is for simply using ggsrun. So, in this method, the expiration time of the access token is 1 hour. And, in this method, the refresh token cannot be used. So when the expiration of access token is over, an error occurs. At that time, it is required to replace ggsrun.cfg with new one by the above script. Please be careful this.

When you want to use continuously, please use OAuth2 and/or service account. Ref

But, even when this method is used, ggsrun can use in 1 hour can. So for example, when you want to manage Google Drive using a CLI tool, I thought that you can use this method.

References

 Share!