-
Notifications
You must be signed in to change notification settings - Fork 345
Description
Is your feature request related to a problem? Please describe.
I need to serialize the Credentials data as JSON, and I just discovered the to_json method. It's very helpful, as the pickle-related techniques in the quickstart (e.g. https://developers.google.com/docs/api/quickstart/python) rely on the file system and binary data, which means the token cannot be persisted in an environment variable (useful for Heroku and other server deployments).
Anyways, the to_json method has the following in the doc string:
Returns:
str: A JSON representation of this instance, suitable to pass to
from_json().
I looked and looked for from_json() and couldn't find it in this library. If it does exist, I can't find it, and maybe should be easier to find. Assuming it doesn't exist, I found (mostly) what I needed in the class method from_authorized_user_info:
if os.path.exists('token.json'):
with open('token.json', 'r') as token:
token_file = token.read()
token_json = json.loads(token_file)
creds = Credentials.from_authorized_user_info(token_json, scopes=SCOPES)
creds.token = token_json['token']That last line is used to ensure creds.valid returns True.
Describe the solution you'd like
The documentation and/or API for serializing/de-serializing as JSON should be clearer. If there is a from_json method, it should be mentioned somewhere in the docs. If there isn't, the doc string in to_json should specify the (class) method most suitable to ingest the serialized JSON. If that method happens to be from_authorized_user_info, it should ingest the token key as well, avoiding the need for the sort of hacky value set after instantiation/building.
Describe alternatives you've considered
The alternative I considered is the code sample for my workaround mentioned earlier in this issue.
Additional context
None I can think of.