You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Gmail API凭证刷新多次后抛出TypeError问题求助

问题修复方案

问题成因

你遇到的错误核心是导入了错误的Request类:

  • 你当前误用的是requests库的Request类,它用于构造普通HTTP请求,必须传入url参数;但Google OAuth的Credentials.refresh()方法需要的是一个可调用的OAuth请求适配器(来自google.auth.transport.requests模块),而非普通请求实例。
  • 初始运行正常是因为token未过期,无需触发刷新逻辑;当token过期后执行刷新时,错误的Request类导致了参数缺失或对象不可调用的问题。

修复步骤

1. 替换Request导入

将错误的Request导入替换为Google官方提供的请求适配器:

# 替换原有Request导入,确保使用google.auth下的适配类
from google.auth.transport.requests import Request

2. 修正刷新调用

直接使用正确的Request实例调用refresh,无需手动传入url(它会自动读取凭证中已有的token_uri参数):

if creds and creds.expired and creds.refresh_token:
    creds.refresh(Request())  # 修正后的刷新逻辑,无需额外参数

完整修正后的代码

from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request  # 关键修正:导入正确的Request
from typing import Any  # 确保类型标注可用
import os

CREDENTIALS_FILE_NAME = 'credentials.json'
TOKEN_NAME = 'gmail_token.json'

def authenticate(self) -> Any:
    creds = None
    if os.path.exists(self.TOKEN_NAME):
        creds = Credentials.from_authorized_user_file(self.TOKEN_NAME, self.get_scopes())
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())  # 修正后的刷新调用
        else:
            flow = InstalledAppFlow.from_client_secrets_file(os.path.join(Project.root_path(), self.CREDENTIALS_FILE_NAME), self.get_scopes())
            creds = flow.run_local_server(port=0)
        with open(self.TOKEN_NAME, 'w') as token:
            token.write(creds.to_json())
    return build('gmail', 'v1', credentials=creds)

def get_scopes(self) -> list:
        return ['https://www.googleapis.com/auth/gmail.send']

额外说明

  • google.auth.transport.requests.Request是Google为OAuth2凭证刷新专门设计的请求处理类,会自动处理token刷新所需的HTTP请求,包括使用凭证中的token_uri、refresh_token等参数,无需手动配置。
  • 建议升级相关依赖库到最新版本,避免旧版本API差异问题:
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2

内容的提问来源于stack exchange,提问作者falukky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 11:03:34