Credentials对象来自Google服务帐户没有`to_json`属性。

huangapple go评论56阅读模式
英文:

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)

creds2creds 的类型是不同的,而 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

huangapple
  • 本文由 发表于 2023年4月10日 19:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定