Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

muteHttpExceptions = true throws authentication failure

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="[email protected]" 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 = '[email protected]'
      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);
      }
    }
like image 572
Jasper Duizendstra Avatar asked Nov 27 '13 09:11

Jasper Duizendstra


People also ask

What is UrlFetchApp fetch()?

UrlFetchApp. Fetch resources and communicate with other hosts over the Internet. This service allows scripts to communicate with other applications or access other resources on the web by fetching URLs. A script can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses.

How do you send a POST request in App script?

The POST API returns a parameter from the query string of the URL and the name from the request body. const doPost = (request = {}) => { const { parameter, postData: { contents, type } = {} } = request; const data = JSON. parse(contents); return ContentService. createTextOutput(parameter.

What is a URL fetch call?

URL Fetch. This service allows scripts to access other resources on the web by fetching URLs. A script can use the UrlFetch service to issue HTTP and HTTPS requests and receive responses. The UrlFetch service uses Google's network infrastructure for efficiency and scaling purposes.


1 Answers

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

like image 142
mhawksey Avatar answered Sep 29 '22 18:09

mhawksey