英文:
Credentials object from google service-account has no attribute `to_json`
问题
在官方的谷歌Python客户端库文档中建议使用Oauth2客户端ID创建凭据:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
并使用 to_json
将它们保存:
token.write(creds.to_json())
但如果我尝试像这个答案或以下文档建议的那样使用服务账户凭据:
creds2 = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, SCOPES)
creds2
和 creds
的类型是不同的,而 creds2
(来自服务账户) 没有 to_json
函数。它甚至没有 items()
,所以我无法直接调用 json.dumps({k: v for (k, v) in creds.items()})
。
如何将这种类型的凭据保存到JSON文件中?
库版本:
google-api-python-client==2.84.0
google-auth==2.17.2
英文:
In official google python client library docs it is suggested to create credentials using Oauth2 client id:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
and save them using to_json
:
token.write(creds.to_json())
But if I try to use service-account credentials as suggested in this answer or in the following doc like this:
creds2 = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, SCOPES)
The types of creds2
and creds
are different, and creds2
(from service_account) does not have to_json
function. It does not even have items()
, so I can not call json.dumps({k: v for (k, v) in creds.items()})
directly.
How can this type of credentials be saved into json file?
Libs versions:
google-api-python-client==2.84.0
google-auth==2.17.2
答案1
得分: 2
Service accounts 不需要存储用户凭据,只需在需要时运行授权,客户端库会为您处理一切。
英文:
Service accounts don't need to store user credentials just let it run authorization when needed the client library will handle everything for you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论