google-apps-scriptgoogle-docsgmail-addons

Accessing Google Docs from GMail Add-On


I am having problems accessing Google Docs from a GMail Add-On.

I want to copy particular Google Docs file from my GMail Add-On, and then edit the new doc. I am able to create a new doc from my template doc, using:

  var docTemplate="<DOCUMENT ID>";
  var docName = "Generated Document 123";
  var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName).getId();

However, when I try to open the file using this code:

  var copyDoc = DocumentApp.openById(copyId);

I get an error in the GMail Add-On pane saying "You do not have permission to call openById [line: 36, function: getContextualAddOn, file: Code]"

I've used this same code successfully in a Apps Script in Google Sheets, so I assume I haven't set up my permissions correctly.

In my manifest file, I set the oauthScopes values as:

  "oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/script.external_request"
  ]

The /auth/drive permission allows me to call DriveApp.getFileById(), but what different permission do I need to call DocumentApp.openById()? Or is there some other issue?

Any pointers / guidance here would be greatly appreciated!


Solution

  • Add this to your scopes: "https://www.googleapis.com/auth/documents"