【问题标题】:Authorization failure in onOpen()onOpen() 中的授权失败
【发布时间】:2015-07-01 16:38:08
【问题描述】:

目标:

使用 Google 脚本插件,我想以最少的用户交互将一些联系人信息从 Google 电子表格推送到 Google 联系人。

问题:

无论我做什么,我总是会遇到授权失败。

  1. 当我在“已安装”模式下测试以下脚本时,我收到一条错误消息:脚本执行失败:您没有执行此操作的适当授权。 (第 6 行)
  2. 当我在“启用”或“已安装并启用”模式下测试以下脚本时,我收到一条错误消息:脚本执行失败:您无权调用 getContactGroup。 (第 11 行)

(注意:我将错误消息从我的语言法语翻译过来,所以原来的英文消息可能略有不同。我还重新编号并漂亮地打印了代码)

 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 }

我可以解释我遇到的第一个错误,因为脚本无法访问它所链接的电子表格。但是一旦脚本启用(甚至启用并安装),它不应该正确访问用户联系人吗?

Google documentation about authorizations 说 onOpen() 触发器在 AuthMode.LIMITED 中运行,这可以防止对用户数据的不必要访问。但我想尽可能安静地运行脚本(例如只请求一次授权)。

有什么建议吗?

【问题讨论】:

标签: google-apps-script google-sheets triggers google-contacts-api


【解决方案1】:

对于第二个错误,即使启用了插件,onOpen() 也无权访问user's data,因此您无法调用ContactsApp.getContactGroup(contactsGroupName)

您不必多次请求授权,只需在另一个函数中调用ContactsApp.getContactGroup(contactsGroupName)(可能由add-on menu中的项目或sidebardialog中的按钮触发)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多