Reputation: 485
I am retrieving user's photos using admin sdk in java.I have implemented exponential backoff also.
But after few requests, I am getting 403 error code with rate limited exception message.
There are 2000 users and after 10 to 20 user's photo. It starts giving 403 error and using exponential backoff it is taking long time to execute.
try {
Directory directoryService = getDirectoryService(adminEmail);
Photos photos = directoryService.users().photos();
com.google.api.services.admin.directory.Directory.Users.Photos.Get get = photos.get(userEmail);
get.setUserKey(userEmail);
UserPhoto userPhoto = get.execute();
} catch (Exception e) {
if(e.getMessage().contains("403"))
{
try {
Thread.sleep((1 << userCount) * 1000 + randomGenerator.nextInt(1001));
} catch (InterruptedException e1) {
e1.printStackTrace();
log.warning("Exception Interrupted in getting photo1::->"+e1.getCause());
}
}
}
can anyone give me suggestions about this issues.?
Upvotes: 1
Views: 852
Reputation: 1601
Ah, I've found the problem. The rate-limiting is because you request a new OAuth2 access token for every request. You need to cache the result of Directory directoryService = getDirectoryService(adminEmail)
instead of doing that each request.
Under node.js, my tokens are good for one hour (they contain an expires_in
field which is a unix timestamp). I modified my code to only request a new token within 300 seconds of it expiring, and I can now hit things just fine at 10 per second, which is more than fast enough to hit the daily limit of 150,000 requests per day.
Upvotes: 1