Pergunta

I am looking for a method to find the unread message count of delegated mailboxes from any of the Google API's.

I am not sure IF it is possible, but it would help me develop a helping tool for a company using this for 1000+ users. A lot of delegation is going on, and I am eager to find a way to accomplish this.

But I might need some help, maybe from people closer with knowledge of the possibilities of the Admin SDK from Google.

I want to use Google Apps Script to collect the unread message count.

Foi útil?

Solução 2

I got my answer from the GAS community on Google Plus, so credits to the posters there.

https://plus.google.com/106333172328928589411/posts/7g3Vu7iFZfb

Sergii: Check out this gist that shows how to do 2-legged OAuth authentication in GAS https://gist.github.com/rcknr/c5be4eb80d821158c8ef.

Using 2 Legged Oauth you can get access to the ATOM feed of other users:

A piece of working code for it:

function gmail2lo(user) {
 var OAUTH_CONSUMER_SECRET = 'secret';
  var domain = 'domain'; //use the domain as key in apps panel
  var username = 'user';
  var xuser = username+'@'+domain;
  var method = "GET";
  var baseUrl = "https://mail.google.com/mail/feed/atom";

  var timestamp = Math.round(new Date().getTime() / 1000);  

  var paramsJson;
  var paramsOauth = {
    oauth_consumer_key : domain,
    oauth_nonce : timestamp,
    oauth_signature_method : "HMAC-SHA1",
    oauth_timestamp : timestamp,
    oauth_version : "1.0",
    'xoauth_requestor_id' : xuser
  };

  var paramsStringArray =  [];
  for (var k in paramsJson)
    paramsStringArray.push(k + '=' + encodeURIComponent(paramsJson[k]));

  var oauthStringArray = [];  
  for (var k in paramsOauth)
    oauthStringArray.push(k + '=' + encodeURIComponent(paramsOauth[k]));

  var paramsString = paramsStringArray.concat(oauthStringArray).sort().join('&');
  var signatureBaseString = method +"&"+ encodeURIComponent(baseUrl) +"&"+ encodeURIComponent(paramsString);
  var signatureBytes = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1, signatureBaseString, OAUTH_CONSUMER_SECRET+'&');
  var signature = encodeURIComponent(Utilities.base64Encode(signatureBytes));

  var xoauthString = 'OAuth ' + oauthStringArray.sort().slice(0,oauthStringArray.length-1).join(", ") + ', oauth_signature=' + signature;

  var ooptions = {
    headers : {authorization: xoauthString}
  }

  url  = baseUrl;
  url += "?" + paramsStringArray.join("&") + '&xoauth_requestor_id=' + encodeURIComponent(xuser);

  var response = UrlFetchApp.fetch(url, ooptions).getContentText();

}

Outras dicas

The Email Settings API allows you to see which delegations are in place.

It is not possible for a user to access the mailbox of another user who has delegated them access via IMAP, thus you can't authenticate as a user and check the delegated mailbox.

You should use OAuth 2.0 Service Accounts to authenticate to the mailboxes via IMAP.

Once authenticated you can select the Gmail "All Mail" folder (or Inbox if you only want count for Inbox). and do a Gmail search of "is:unread" to determine how many unread messages the user has.

FYI, my open-source app, GYB can do just this. There is a getting started guide for GYB. You'll also need to setup the service account. The command to get unread message count for all mail would be something like:

gyb --email delegated-mailbox@yourcompany.com --service-account your-service@account.com --action count --search "is:unread"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top