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);
}
}
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.
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.
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.
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:
Revoking access in your Google Account's Security settings to both www.google.com AND the source of Apps Script (spreadsheet etc).
Change the oAuthServiceName parameter to something else.
Re-running the script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With