我编写了一个 Python 脚本 (Google_add.py),用于在 Google Workspace 中创建用户。但是,当它运行时,发生了 AttributeError。我该如何解决这个错误?
・Google_add.py‖(摘录)
PATH = '/opt/Google/credentials.json'
CLIENT_SECRETS = '/opt/Google/client_secrets.json'
credentials = None
#If there are credentials
if os.path.exists(PATH):
credentials = Credentials.from_authorized_user_file(
path = PATH,
scopes = SCOPES)
#If there are no (valid) credentials available
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
FLOW = flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file = CLIENT_SECRETS,
scopes = SCOPES)
credentials = FLOW.run_console()
with open(PATH, 'w') as token:
token.write(credentials.to_json())
・错误讯息
File "Google_add.py", line 186, in <module>
main()
File "Google_add.py", line 172, in main
FLOW.run_console()
AttributeError: 'InstalledAppFlow' object has no attribute 'run_console'
我写的时候参考了下面的网站。 https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md#sending-users-to-googles-oauth-20-server https://developers.google.com/admin-sdk/directory/v1/quickstart/python?hl=en
在脚本中,我想使用用户帐户(不是服务帐户)进行授权。并且“run_local_server()”方法不起作用,因为我的 linux 服务器不是 web 服务器(没有 httpd、nginx 等)。
(如果我的文章难以阅读,我很抱歉。)
问题可能是
google-api-python-client
库与其他库的版本兼容性问题,例如
你只需要确保你的
google-api-python-client
版本支持你安装到它的依赖库的版本。
您可以查看google-api-python-client
库的完整更改日志here
https://googleapis.github.io/google-api-python-client/docs/auth.html
https://googleapis.github.io/google-api-python-client/docs/oauth.html
在 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