【问题标题】:Oauth Causing More permissions problemsOauth 导致更多权限问题
【发布时间】:2018-08-07 13:42:18
【问题描述】:

几个月来我一直在努力解决这个问题,但情况越来越糟。

我在 Google Apps 脚本中有一个简单的程序,它从工作表中获取电子邮件地址并将它们放入适当的 Google 通讯录组中。直到一个月前它停止工作时,它都可以正常工作。

请帮帮我:

1) 了解我是否真的需要使用 API 来完成我正在做的事情。

2) 在下面检查我的工作是否有可能遗漏的内容。

原来的问题:

当我运行此代码时,我收到错误消息:“您无权执行该操作。”

function testAddEmail(){

ContactsApp.createContact(null, null, "tomfoolery@validemailaddress.com");

};

为了解决这个问题,我已经: - 确保在我的 Cloud Console 中启用了联系人 API。 - 多次禁用并重新启用它。 - 使用程序创建了一个新文件并启用了新的联系人 API。 -为新程序创建凭据(在此之后它工作了一天)。 - 联系 Google Cloud 支持和 G Suite 支持以确保设置正确(他们不支持脚本或联系人。)

问题 2:

我尝试添加 Oauth 2 代码,但现在它给了我错误“您无权调用 getActiveSpreadsheet。”

他们的问题在我下面的 Oauth 2 代码中吗? (我删除了我的个人信息。)

function getContactsService() {
  // Create a new service with the given name. The name will be used when
  // persisting the authorized token, so ensure it is unique within the
  // scope of the property store.
  return OAuth2.createService('contacts')

      // Set the endpoint URLs, which are the same for all Google services.
      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')

      // Set the client ID and secret, from the Google Developers Console.
      .setClientId('blablabla.apps.googleusercontent.com')
      .setClientSecret('blablabla')

      // Set the name of the callback function in the script referenced
      // above that should be invoked to complete the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())

      // Set the scopes to request (space-separated for Google services).
      .setScope('https://www.google.com/m8/feeds')
      .setScope('https://www.googleapis.com/auth/spreadsheets')

      // Below are Google-specific OAuth2 parameters.

      // Sets the login hint, which will prevent the account chooser screen
      // from being shown to users logged in with multiple accounts.
      .setParam('login_hint', Session.getActiveUser().getEmail())

      // Requests offline access.
      .setParam('access_type', 'offline')

      // Forces the approval prompt every time. This is useful for testing,
      // but not desirable in a production application.
      .setParam('approval_prompt', 'force');
}

【问题讨论】:

标签: google-apps-script google-oauth google-contacts-api


【解决方案1】:
  1. 您是否登录了多个 Google 帐户?这通常会导致授权问题。尝试注销所有内容并重新登录到一个帐户。

  2. 这是在触发器上运行还是您手动运行公式?

  3. 是否是电子表格的所有者并以所有者身份登录?您是否在电子表格的容器绑定脚本中运行此测试?

  4. 如果您是电子表格的所有者,并且您只是将自己电子表格中的电子邮件地址添加到自己的 Gmail 帐户,那么您没有理由需要使用 API 或 OAuth 协议。

  5. 正如另一位评论者所说,您要求的范围是什么?要从您的电子表格中设置一个简单的脚本来添加联系人,您可能只需要您列出的两个。您可以通过选择查看--> 显示清单文件来手动设置它们。例如:

    {
       "timeZone": "America/New_York",
       "dependencies": {
         },
       "oauthScopes": [
         "https://www.googleapis.com/auth/script.container.ui",
         "https://www.googleapis.com/auth/spreadsheets"
         ]  
    }
    

【讨论】:

  • 非常感谢您的帮助!!! 1)我已经注销了所有内容,并且只连接了一个帐户进行测试。
  • 2) 还没有触发器。只是尝试从菜单下拉菜单或脚本编辑器中运行。 3)我是业主。这是在我自己的域上运行的。该脚本不与任何人共享。这是容器绑定的。 4) 我是我的域的所有者,使用我自己的域帐户。 5) 如果我不需要 API,那么我不需要担心添加范围。还有其他建议吗?我应该将 G Suite 帐户设置重置为默认设置吗?
  • 我要重新开始:通过转到 myaccount.google.com/permissions 和“删除访问权限”来删除此应用程序的权限。然后注释掉代码中的所有内容(更好的是,删除所有内容)但测试函数,如上所述在清单文件中显式设置范围,然后尝试从脚本编辑器运行该函数。然后会发生什么...?
  • 所以我从我拥有的每个应用程序中删除了所有权限。还打开了一个新的工作表文件并粘贴在上面的测试功能中。得到了同样的错误。然后我在上面添加了电子表格和 UI 范围。该错误特别是“您无权添加联系人”。所以我添加了联系人范围。一切都重新授权了。并且仍然得到相同的“您无权执行该操作”。我什至需要范围吗?似乎在您上面的 cmets 中,因为它是我的联系人和我的脚本,它们不应该在同一个电子邮件地址下。
  • 对于这个新文件,我也进入并授权了联系人 API
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 2011-11-18
相关资源
最近更新 更多