2

Goal:

With a Google script add-on, I want to push some contact info from a Google spreadsheet to Google Contacts with minimal user interaction.

Problem:

Whatever I do, I always get an authorization failure.

  1. When I test the script below in "installed" mode I get an error message: Script execution failure: you don't have the appropriate authorization to do this action. (line 6)
  2. When I test the script below in "enabled" or in "installed and enabled" mode I get an error message: Script execution failure: you're not authorized to call getContactGroup. (line 11)

(Note: I translated the error messages from french which is my language, so the original english messages may differ slightly. I also renumbered and pretty printed the code)

 1 function onInstall(e) {
 2   onOpen(e);
 3 }
 4
 5 function onOpen(e) {
 6  var spreadsheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
 7  createContactsGroupIfNotExists(spreadsheetName);
 8 }
 9
10 function createContactsGroupIfNotExists(contactsGroupName) {
11   var group = ContactsApp.getContactGroup(contactsGroupName)
12   
13   if(! group)
14     contactsGroup = ContactsApp.createContactGroup(contactsGroupName);
15   
16   return group;
17 }

I can explain the first error I get as the script can't access yet the spreadsheet it is linked to. But once the script is enabled (or even enabled and installed), shouldn't it would access the user contacts properly.

Google documentation about authorizations say the onOpen() trigger is run in AuthMode.LIMITED which prevent unwanted access to the user data. But I want to run the script as silently as possible (e.g. asking for authorization only once).

Any advice?

Rubén
  • 34,714
  • 9
  • 70
  • 166
jlp6k
  • 21
  • 2
  • btw, I'm not looking for a way around Google security stuff but the right way to proceed. – jlp6k Jul 02 '15 at 08:00
  • Suspect the problem is getActiveSpreadsheet() You can get the spreadsheet from e in onInstall(e) see https://developers.google.com/apps-script/guides/triggers/events and https://developers.google.com/apps-script/guides/triggers/installable – eddyparkinson Jul 06 '15 at 05:08
  • Possible duplicate of [Google Apps Script Not Calling the UrlFetchApp() in the onOpen() function](https://stackoverflow.com/questions/21794970/google-apps-script-not-calling-the-urlfetchapp-in-the-onopen-function) – Rubén Feb 24 '19 at 22:20

1 Answers1

0

For the second error, even when the add-on is enabled, onOpen() doesn't have access to the user's data so you can't call ContactsApp.getContactGroup(contactsGroupName).

You don't have to ask for authorization multiple times, you just have to call ContactsApp.getContactGroup(contactsGroupName) in another function (perhaps triggered by item in add-on menu or button in sidebar or dialog)

Tovly Deutsch
  • 663
  • 9
  • 30