无法从“google.auth”导入名称“external_account_authorized_user”

Sub*_*rty 13 google-oauth google-api-python-client

from pprint import pprint
from Goo_gle import Create_Service


CLIENT_SECRET_FILE = 'Client_Calendar.json'
API_NAME = 'calendar'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth']


service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
Run Code Online (Sandbox Code Playgroud)

输出:

Traceback (most recent call last):   File "d:\Play with code\PROGRAMMINGS\Python\Artificial Inteligence\calen_dar.py", line 2, in <module>
    from Goo_gle import Create_Service   File "d:\Play with code\PROGRAMMINGS\Python\Artificial Inteligence\Goo_gle.py", line 5, in <module>
    from google_auth_oauthlib.flow import Flow, InstalledAppFlow   File "C:\Python\lib\site-packages\google_auth_oauthlib\__init__.py", line 21, in <module>
    from .interactive import get_user_credentials   File "C:\Python\lib\site-packages\google_auth_oauthlib\interactive.py", line 27, in <module>
    import google_auth_oauthlib.flow   File "C:\Python\lib\site-packages\google_auth_oauthlib\flow.py", line 69, in <module>
    import google_auth_oauthlib.helpers   File "C:\Python\lib\site-packages\google_auth_oauthlib\helpers.py", line 27, in <module>
    from google.auth import external_account_authorized_user ImportError: cannot import name 'external_account_authorized_user' from 'google.auth' (C:\Python\lib\site-packages\google\auth\__init__.py)
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会发生这种情况。这段代码在过去 3 个月一直有效,但是今天更新 google 库后,出现了这个错误。如何解决这个问题?

Ste*_*ike 13

看起来与模块最近的更改有关google-auth-library-python-oauth:

https://github.com/googleapis/google-auth-library-python-oauthlib/pull/240/files#diff-63cdf7d9059947cf2f03b72a8137b8a1901de52f685705c251886d58a75b9381L8

将 google-auth 升级到 2.13.0 可能会修复它。


小智 8

google_auth_oauthlib当使用最新版本(0.6.0)时,您会收到该错误。请参阅此链接:https://github.com/googleapis/google-auth-library-python-oauthlib/blob/v0.6.0/google_auth_oauthlib/helpers.py#L27

尝试使用以下命令降级 google-auth-oauthlib lib: pip install google-auth-oauthlib==0.4.6

或更新您的requirement.txt以添加此行:

...
google-auth-oauthlib==0.4.6
...
Run Code Online (Sandbox Code Playgroud)

  • 对于使用 h5pyd 的 Ubuntu 用户(Python 3.8),这是遇到此错误时的适当修复 (2认同)