muteHttpExceptions = true throws authentication fa

2019-03-29 09:48发布

Using the Google Apps Email Settings API a delegate is deleted with the script below. If an error occurs, for example trying to delete delegates that do not exist, the following message is returned:

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)

However when using muteHttpExceptions = true the authentication fails:

Exception: Failed to authenticate for service: google

This forces me to use a try / catch structure instead of examining the HTTPResponse object. I would like to know why this is happening and how to solve it.

The test function:

   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);
      }
    }

1条回答
Luminary・发光体
2楼-- · 2019-03-29 10:31

This question has been posted to the Google Apps Script issue tracker as ticket 3478 and acknowledged as a bug. The ticket remains open but 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

查看更多
登录 后发表回答