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
Service accounts don't need to store user credentials just let it run authorization when needed the client library will handle everything for you