You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在Colab环境中运行Google Python API快速启动指南遇模块缺失及RefreshError问题的求助

在Colab环境中运行Google Python API快速启动指南遇模块缺失及RefreshError问题的求助

这其实是我两年前问过的一个关于Gmail Python API个人使用授权问题的后续。最近我重新捡起来之前做的工作,想让老脚本跑起来……结果又卡在了Google认证这一步,真的头大!我在本地机器和Google Colab notebook里都试过,两边的错误还不一样,看起来都是基础环境配置类的问题。我其实更倾向于让Colab版本能跑通,当然本地能解决也行。

本地环境的模块缺失问题

按照官方快速启动指南操作时,执行导入语句一直报错:

>>> from google.oauth2.credentials import Credentials
Traceback (most recent call last):
  File "<python-input-8>", line 1, in <module>
    from google.oauth2.credentials import Credentials
ModuleNotFoundError: No module named 'google'
>>> from google_auth_oauthlib.flow import InstalledAppFlow
Traceback (most recent call last):
  File "<python-input-9>", line 1, in <module>
    from google_auth_oauthlib.flow import InstalledAppFlow
ModuleNotFoundError: No module named 'google_auth_oauthlib'
>>> from googleapiclient.discovery import build
Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
    from googleapiclient.discovery import build
ModuleNotFoundError: No module named 'googleapiclient'

但我用pip freeze查看,明明已经安装了相关依赖:

beautifulsoup4==4.11.2
cachetools==5.3.0
certifi==2022.12.7
charset-normalizer==3.1.0
google==3.0.0
google-api-core==2.11.0
google-api-python-client==2.81.0
google-auth==2.16.2
google-auth-httplib2==0.1.0
google-auth-oauthlib==1.0.0
googleapis-common-protos==1.58.0

Colab环境的RefreshError问题

在Colab里运行时,遇到了这个授权刷新错误:

RefreshError                              Traceback (most recent call last)
<ipython-input-15-3a42be915110> in <cell line: 0>()
     20       , SCOPES
     21   )
---> 22   creds.refresh(Request())
     23 # If there are no (valid) credentials available, let the user log in.
     24 if not creds or not creds.valid:

2 frames
/usr/local/lib/python3.11/dist-packages/google/oauth2/_client.py in _handle_error_response(response_data, retryable_error)
     67         error_details = json.dumps(response_data)
     68 
---> 69     raise exceptions.RefreshError(
     70         error_details, response_data, retryable=retryable_error
     71     )

RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})

备注:内容来源于stack exchange,提问作者Scott Borden

火山引擎 最新活动