?Google OAuth?AttributeError:“InstalledAppFlow”对象没有属性“run_console”

Ano*_*ous 14 google-oauth google-api-python-client google-admin-sdk google-workspace

我编写了一个 Python 脚本(Google_add.py),用于在 Google Workspace 中创建用户。然而,当它运行时,发生了 AttributeError 。如何解决此错误?

\n

\xe3\x83\xbbGoogle_add.py\xe3\x80\x80(摘录\xef\xbc\x89

\n
PATH = \'/opt/Google/credentials.json\'\nCLIENT_SECRETS = \'/opt/Google/client_secrets.json\'\ncredentials = None\n\n#If there are credentials\nif os.path.exists(PATH):\n    credentials = Credentials.from_authorized_user_file(\n      path = PATH,\n      scopes = SCOPES)\n\n#If there are no (valid) credentials available\nif not credentials or not credentials.valid:\n    if credentials and credentials.expired and credentials.refresh_token:\n      credentials.refresh(Request())\n    else:\n      FLOW = flow.InstalledAppFlow.from_client_secrets_file(\n        client_secrets_file = CLIENT_SECRETS,\n        scopes = SCOPES)\n      credentials = FLOW.run_console()\n    with open(PATH, \'w\') as token:\n      token.write(credentials.to_json())\n
Run Code Online (Sandbox Code Playgroud)\n

\xe3\x83\xbb错误消息

\n
File "Google_add.py", line 186, in <module>\nmain()\nFile "Google_add.py", line 172, in main\nFLOW.run_console()\nAttributeError: \'InstalledAppFlow\' object has no attribute \'run_console\'\n
Run Code Online (Sandbox Code Playgroud)\n

我在编写时参考了以下网站。\n https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md#sending-users-to-googles- oauth-20-server \n https://developers.google.com/admin-sdk/directory/v1/quickstart/python?hl=en

\n

在脚本中,我想使用用户帐户(而不是服务帐户)进行授权。并且“run_local_server()”方法不起作用,因为我的Linux服务器不是Web服务器(没有httpd、nginx等)。

\n

(如果我的文章难以阅读,我很抱歉。)

\n

Att*_*igh 13

扩展上面 George 的答案,github 帖子中可用的库版本是:

google-api-python-client==1.7.2
google-auth==1.8.0
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
Run Code Online (Sandbox Code Playgroud)


小智 6

该问题可能是google-api-python-client库与其他库的版本兼容性问题,例如

  • 谷歌认证
  • 谷歌-auth-httplib2
  • 谷歌认证 oauthlib

您只需要确保您的版本google-api-python-client支持您安装到其依赖库的版本。您可以在此处google-api-python-client查看库的完整更改日志

参考:

google-api-python-client 更改日志

https://googleapis.github.io/google-api-python-client/docs/auth.html

https://googleapis.github.io/google-api-python-client/docs/oauth.html

谷歌 API python 客户端

google_auth_oauthlib.flow 模块

Github 发布类似问题