Timeout error , when try to download mailbox using Email Audit Api via google script

346 Views Asked by At

I am using Email Audi API to download user's mailbox in encrypted form..When i write code into google script and tried to run then it gives error 504 : Timeout error.. When i did this using OAuth playground then i succeed to download mailbox..So please give me some suggestion to resolve this problem.

Code :
function downloadMailBox(user){
var user='[email protected]'
var base='https://apps-apis.google.com/a/feeds/compliance/audit/'
var fetchArgs=googleOAuth_('google',base)
var userID=user.split('@')[0]
//Logger.log(userID)
var rawXml='<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006">'+
    '<apps:property name="packageContent" value="FULL_MESSAGE"/></atom:entry>'
    fetchArgs.payload=rawXml
var uriForMailbox=base+'mail/export/mydomain.com/'+userID
UrlFetchApp.fetch(uriForMailbox,fetchArgs)
}


function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  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("mydomain.com");
  oAuthConfig.setConsumerSecret(consumersecret);
  return {oAuthServiceName:name, 
  oAuthUseToken:"always",
  contentType:'application/atom+xml',
  method:'POST'
  }; 

}
2

There are 2 best solutions below

0
On BEST ANSWER

I found one solution for this...I just wrote above code into try-catch block..As timeout error comes, it goes to catch block where i am calling above function downloadmailBox() again.(sometimes script executes properly and sometimes gives error).It will call this function again and again till get success. probably it gets success within 2 or 3 attempts...So by doing this way, timeout problem can be resolve.

2
On

After how much time, you are getting the error? Google has script execution time limit which is around 5 minutes. Any script, which will take time longer than this will time out. There are other limits as well. You can check the details on Apps Script Dashboard.

You need to optimize the script or break your operation in small batches. Here are some tips to optimize google script. Link#1 Linkk#2