Admin sdk 403 Rate Limit Exceeded

寵の児 提交于 2019-12-11 05:27:37

问题


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.?


回答1:


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.



来源:https://stackoverflow.com/questions/24184975/admin-sdk-403-rate-limit-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!