超过管理员sdk 403速率限制 [英] Admin sdk 403 Rate Limit Exceeded

查看:93
本文介绍了超过管理员sdk 403速率限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java中的admin sdk检索用户的照片.我还实现了指数补偿.

I am retrieving user's photos using admin sdk in java.I have implemented exponential backoff also.

但是在几次请求之后,我收到了403错误代码以及速率受限的异常消息.

But after few requests, I am getting 403 error code with rate limited exception message.

有2000位用户,以及10到20位用户的照片.它开始显示403错误 并使用指数补偿会花费很长时间才能执行.

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

推荐答案

嗯,我发现了问题.速率限制是因为您为每个请求都请求了一个新的OAuth2访问令牌.您需要缓存Directory directoryService = getDirectoryService(adminEmail)的结果,而不是执行每个请求.

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.

在node.js下,我的令牌可以使用一小时(它们包含一个expires_in字段,该字段是unix时间戳).我修改了代码,只在过期后的300秒内请求了一个新令牌,现在我可以每秒10次的速度完成请求,这足够快,可以达到每天150,000个请求的每日限制.

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.

这篇关于超过管理员sdk 403速率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆