I understand that the Email Settings API is deprecated and will be turned down in Fall 2018. However, I need to create a script that delegates domain users (as Administrator) and the new Gmail API doesn't support the delegation feature yet.
I used OAuth2 library for Google Apps Script. I created a Service Account for the project. I delegated domain-wide delegation authority for the Service Account. I added the scope "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" in Admin Console.
The OAuth2.0 code works. I have used it to successfully set a domain user's signature.
However, when I try to add delegate with Email Settings API the following code, I get this message:
Are you no longer able to access the deprecated Email Settings API? Any pointer would be appreciated. Thanks!
function addDelegate() {
var email = "delegator@domain.com"
var domainName = "domain.com";
var username = email.split("@")[0];
var authorizationScope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/';
var service = getDomainWideDelegationService("Email Settings API:", authorizationScope, email);
if (!service.hasAccess()) {
Logger.log("failed to authenticate as user " + email);
Logger.log(service.getLastError());
} else {
Logger.log("successfully authenticated as user " + email);
}
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006">' +
'<apps:property name="address" value="delegate@domain.com" />' +
'</atom:entry>';
var base = "https://apps-apis.google.com/a/feeds/emailsettings/2.0/";
var fetchArgs = {};
fetchArgs.headers = {"Authorization": "Bearer " + service.getAccessToken()};
fetchArgs.method = "POST";
fetchArgs.contentType = "application/atom+xml";
fetchArgs.payload = xml;
fetchArgs.muteHttpExceptions = true;
var url = base + domainName + "/" + username + "/delegation";
try {
var response = UrlFetchApp.fetch(url, fetchArgs); // Add delegate
Logger.log("response = %s ", response.getContentText());
} catch (e) {
Logger.log("Add delegate failed: " + e);
}
}