0

I am working with Admin SDK .when I update single user it's working fine but when I am trying to update bunch(1000) of users I got User Rate Limit Exceeded Exception. Please check below my code and tell me what am i missing ? or tell any suggestion ?

 private Directory getDirectoryService(String adminEmailAddress){
 Directory directoryService = null;
 try {
      Collection<String> SCOPES = new ArrayList<String>();
      SCOPES.add("https://www.googleapis.com/auth/admin.directory.user");
      SCOPES.add("https://www.googleapis.com/auth/admin.directory.user.readonly");
      SCOPES.add("https://www.googleapis.com/auth/admin.directory.group");
      SCOPES.add("https://www.googleapis.com/auth/admin.directory.group.readonly");
      SCOPES.add("https://www.googleapis.com/auth/admin.directory.orgunit");
      SCOPES.add("https://www.googleapis.com/auth/admin.directory.orgunit.readonly");
      SCOPES.add("https://www.googleapis.com/auth/userinfo.profile");

      HttpTransport httpTransport = new NetHttpTransport();
      JacksonFactory jsonFactory = new JacksonFactory();
      GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
      .setServiceAccountScopes(SCOPES)
      .setServiceAccountUser(adminEmailAddress)
      .setServiceAccountPrivateKeyFromP12File(new                                                         java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)).build();

    directoryService = new Directory.Builder(httpTransport,jsonFactory, credential).setApplicationName("gdirectoryspring").build();

 } catch(Exception e){
   ErrorHandler.errorHandler(this.getClass().getName(), e);
 }
 return directoryService;
}

Below is Update User Code

Directory directoryService = getDirectoryService("adminEmailAddress");
User user = directoryService.users().get("userPrimaryEmail").execute();
       List<UserOrganization> organizaionList = user.getOrganizations();
       for (int j = 0; j < organizaionList.size(); j++)
       {
         UserOrganization singleOrg = organizaionList.get(j);
         if (singleOrg != null)
         {
          if ("work".equalsIgnoreCase(singleOrg.getCustomType()) ||singleOrg.getPrimary() != null)  
  {
  if (singleOrg.getTitle() != null)
  { 
    singleOrg.setTitle(jobTitle);
   }
  }
 }
  user.setOrganizations(organizaionList);
}

       Update update= directoryService.users().update(primaryEmail, user);
       User userUpdated = update.execute();

and in admin console I increased my limit like below

Admin SDK   10.0 requests/second/user

but till I am getting User Rate Limit Exceeded Excepiton. can any one help me?

RBP
  • 481
  • 1
  • 9
  • 29

1 Answers1

2

You are missing exponential backoff. See here for an example (its for drive but can be adapted) https://developers.google.com/drive/handle-errors#implementing_exponential_backoff in there you will see how it deals with errors like 'userRateLimitExceeded'

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • dont know why this was marked down. its the correct answer. Hes getting an expected User Rate Limit Exceeded Excepiton because hes calling the api 1000s of times in a loop when his limit is 10 requests per second. He simply needs to handle the error and implmenent exponential backoff to retry the current element in the loop. he could also place "sleep" in key areas but that is not as generic and will wait more than needed. – Zig Mandel Nov 29 '13 at 16:56
  • I implemented the exponential back up now i am getting below error.403. That’s an error.

    We're sorry, but you do not have access to this page. That’s all we know.

    – RBP Feb 25 '14 at 07:39
  • Take a look at http://stackoverflow.com/questions/22011490/token-response-exception – RBP Feb 25 '14 at 14:05
  • can u tell me any suggestion for this http://stackoverflow.com/questions/22009892/status-code-503-requested-service-is-not-available-yet – RBP Feb 25 '14 at 14:54
  • I added exp backoff , but getting same issues. Thread.sleep((1 << n) * 1000 + randomGenerator.nextInt(1001)); – RBP Feb 26 '14 at 06:24