0

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.

Scope

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:

Error 403

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);
  }
}
s2000coder
  • 913
  • 2
  • 15
  • 23
  • An obvious test would be: can you read and write other things with the Email Settings API? If yes, then availability is not a problem, your authentication is. – tehhowch Apr 17 '18 at 23:19
  • @tehhowch following your advice, I tried disabling the forwarding settings using the Email Setting API, but without success. I received the same message, "You are not authorized to access this API Error 403". would availability be a problem – s2000coder Apr 17 '18 at 23:35
  • @tehhowch Does Google usually deny access to deprecated APIs even if the shut down date has been postponed? The shut down date for Email Settings API is November 17, 2018. Current apps using this API would still work. Is it safe to assume that newly created app using this API would work as well? – s2000coder Apr 17 '18 at 23:42
  • Please try using service account https://ctrlq.org/code/20375-service-accounts-google-apps-script – Shivaji Apr 27 '18 at 16:20
  • @shiva In first paragraph, I mentioned that I did use a service account. – s2000coder May 02 '18 at 17:21
  • @s2000coder got it could you please Use below link code. https://github.com/gsuitedevs/apps-script-oauth2/blob/master/samples/GoogleServiceAccount.gs – Shivaji May 03 '18 at 17:50

0 Answers0