google-apps-script - muteHttpExceptions = true 抛出身份验证失败

标签 google-apps-script google-api oauth-2.0

使用Google Apps Email Settings API使用以下脚本删除委托(delegate)。如果发生错误,例如尝试删除不存在的委托(delegate),则会返回以下消息:

Exception: Request failed for returned code 400. Truncated server response: <xml version="1.0" encoding="UTF-8"?> <AppsForYourDomainErrors> <error errorCode="1303" invalidInput="XXXX@XXXX.com" reason="E... (use muteHttpExceptions option to examine full response)

但是,当使用 muteHttpExceptions = true 时,身份验证失败:

Exception: Failed to authenticate for service: google

这迫使我使用 try/catch 结构而不是检查 HTTPResponse 对象。我想知道为什么会发生这种情况以及如何解决。

测试函数:

   function test() {
      var consumerKey = 'XXXX';
      var consumerSecret = 'XXXX';
      var domain = 'XXXX.com';
      var userName = 'XXXX'
      var delegateName = 'XXXX@XXXX.com'
      var serviceName = 'google';
      var scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/';


      var oAuthConfig = UrlFetchApp.addOAuthService(serviceName);
      oAuthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope=' + scope);
      oAuthConfig.setAuthorizationUrl('https://www.google.com/accounts/OAuthAuthorizeToken');
      oAuthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
      oAuthConfig.setConsumerKey(consumerKey);
      oAuthConfig.setConsumerSecret(consumerSecret);

      var fetchParameters = {};
      fetchParameters.oAuthServiceName = serviceName;
      fetchParameters.oAuthUseToken = 'always';
      fetchParameters.method = 'DELETE';
      fetchParameters.muteHttpExceptions = false;

      try {
        var url = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/'+ domain + '/' + userName + '/delegation/' + delegateName;
        var result = UrlFetchApp.fetch(url, fetchParameters);
      } catch (e) {
        Logger.log(e);
      }
    }

最佳答案

此问题已发布到 Google Apps 脚本问题跟踪器,编号为 ticket 3478acknowledged as a bug 。票证仍然开放,但 the following workaround has been proposed :

  1. Revoking access in your Google Account's Security settings to both www.google.com AND the source of Apps Script (spreadsheet etc).

  2. Change the oAuthServiceName parameter to something else.

  3. Re-running the script

关于google-apps-script - muteHttpExceptions = true 抛出身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20238787/

相关文章:

Javascript:在插入 Google 表格之前按月而不是按天对 csv 数据进行分组

PHP Oauth signature_invalid

javascript - 在nodejs中通过google api发送邮件失败

java - 使用第 3 方 OAuth2 服务器登录 Spring Security 应用程序

java - 在 Web 应用程序中使用 QR 进行移动身份验证

c# - 使用 Onedrive SDK/API(或任何 API)保持身份验证

javascript - 使用应用程序脚本将谷歌电子表格数据导入谷歌表单

google-apps-script - 锁定服务 getScriptLock() 在库中是否有效

javascript - 如果另一个单元格不为空,如何为单元格设置代码 Google SpreadSheet

android - Android-Google-Service插件冲突