Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 28, 2018 09:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanaikech/23ddf599a4155b66f1029978bba8153b to your computer and use it in GitHub Desktop.
Save tanaikech/23ddf599a4155b66f1029978bba8153b to your computer and use it in GitHub Desktop.
Taking Advantage of Manifests by GAS Library

Taking Advantage of Manifests by GAS Library

Introduction

By recent Google update (Google update at October 24, 2017), various new winds to GAS developers were blown. There is "Manifests" as one of the new winds. "Manifests" makes us manage the project using JSON. Especially, the special scopes which have to use OAuth2 process can be used by only setting them to the Manifests. I think that this is the largest modification. However, when scopes are added to a project using Manifests, users who use the project can use only added scopes. This means that when users create scripts in the project, if there are some scopes which is required to be added, such scopes cannot be automatically added. So the error of "Insufficient Permission" occurs.

In this report, I would like to introduce the workaround for avoiding this problem.

Workaround

I found the workaround for avoiding the problem by using a library as follows.

  • By installing the library which was given "oauthScopes" of Manifests, the new project can automatically have those scopes as the default scopes.
    • This can be seen at On script editor -> File -> Project properties -> Scopes.
  • When users develop scripts in the project which installed the library, the scopes depended on the scripts are automatically installed to the project as the additional scopes.
    • This means that the above problem is avoided.

By this workaround, users can use various scopes by only installing a library. Furthermore, also users can develop scripts in the project which installed the library, while they don't worry about the permission error by scopes.

I think that this workaround can be applied to various applications. I have already applied this to my applications (ProjectApp, ManifestsApp, RearrangeScripts).

Sample

Here, I prepared a sample library for demonstrating the install of scopes using a library. The library has no script. But there are the following scopes in the Manifests.

"oauthScopes": [
  "https://www.googleapis.com/auth/analytics.readonly",
  "https://www.googleapis.com/auth/blogger.readonly",
  "https://www.googleapis.com/auth/calendar.readonly",
  "https://www.googleapis.com/auth/drive.readonly",
  "https://www.googleapis.com/auth/gmail.readonly",
  "https://www.googleapis.com/auth/presentations.readonly",
  "https://www.googleapis.com/auth/spreadsheets.readonly",
  "https://www.googleapis.com/auth/youtube.readonly"
]

As a demonstration, you can experience the workaround of this report. Please try as follows.

  1. Create a new project on your Google Drive.
  2. Confirm that there are no scopes on the new project at On script editor -> File -> Project properties -> Scopes.
  3. Install a sample library.
    • library's project key is 1TYEitZm7SsAGPDI1PMfeZhIDqRrpLF8a5H9AlhGddCjJMOyHS4EBRH1c
  4. Confirm that there are 8 scopes on the new project at On script editor -> File -> Project properties -> Scopes. You can see like the below demonstration.
    • This means that you can use the access token which included these scopes using ScriptApp.getOAuthToken().

By this workaround, the other scopes depended on the developed scripts can be automatically added.

References

Acknowledgements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment