首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请求gmail-api时HttpError 429 :超出用户速率限制

请求gmail-api时HttpError 429 :超出用户速率限制
EN

Stack Overflow用户
提问于 2021-02-25 19:52:31
回答 1查看 282关注 0票数 0

我正在使用Gmail-api从接收箱中检索电子邮件,我面临这个错误已经有一段时间了: googleapiclient.errors.HttpError:https://gmail.googleapis.com/gmail/v1/users/***/?alt=json returned "User-rate limit exceeded。假设链接中指示的错误的json是:

代码语言:javascript
复制
{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Login Required.",
        "domain": "global",
        "reason": "required",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

我想知道这个错误是由于过期的凭据,还是达到了每个用户的速率限制,或者其他什么原因。如果错误是由于已存在的凭据导致的,我如何通过python代码自动刷新它们。如果没有,解决方案是什么?

与gmail_api建立连接的代码如下:

代码语言:javascript
复制
def connect(self):
    SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
    SERVICE_ACCOUNT_FILE = 'credentials.json'

    # The user we want to "impersonate"
    USER_EMAIL = "box@company.com"

    credentials = service_account.Credentials. \
        from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    delegated_credentials = credentials.with_subject(USER_EMAIL)

    service = discovery.build('gmail', 'v1', credentials=delegated_credentials)

    
    return service

之后,我将获得要处理的电子邮件列表:

代码语言:javascript
复制
def emails_list_by_labels(self, service, user_id='me', label_ids=[]):
    try:
        response = service.users().messages().list(userId=user_id,
                                                   labelIds=label_ids).execute()
        messages = []
        if 'messages' in response:
            messages.extend(response['messages'])
        while 'nextPageToken' in response:
            page_token = response['nextPageToken']
            response = self.service.users().messages().list(userId=user_id,
                                                            labelIds=label_ids,
                                                            pageToken=page_token).execute()
            messages.extend(response['messages'])
        return messages
    except errors.HttpError as error:
        logging.error('an error occured: %s' % error)

如果有人有什么建议,我将不胜感激。我真的不知道如何解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2021-02-25 20:45:20

查看您的错误消息-

HttpError 429在请求https://gmail.googleapis.com/gmail/v1/users/***/?alt=json时返回"User-rate limit exceeded“

这里的链接不是用来“点击”的,它是失败请求的url。当你尝试访问它时,你会得到一个未认证的错误,因为你没有通过认证来查看它,但你的应用程序是这样的,所以它没有这个问题。

你的应用程序遇到的问题是请求太多,如429错误所示。有关它的详细信息,请访问here。你必须调查错误的确切原因,并从中寻找合适的解决方案-指数回退,缓存等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66367951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档