Rebuild a Google instance from a previously-serialized dict. The reverse of `to_state()`. The Google SDK credentials object is reconstructed from the stored token dict so subsequent API calls work without forcing the user back through the OAuth2 flow.
(cls, state)
| 409 | |
| 410 | @classmethod |
| 411 | def from_state(cls, state): |
| 412 | """Rebuild a Google instance from a previously-serialized dict. |
| 413 | |
| 414 | The reverse of `to_state()`. The Google SDK credentials object is |
| 415 | reconstructed from the stored token dict so subsequent API calls |
| 416 | work without forcing the user back through the OAuth2 flow. |
| 417 | """ |
| 418 | if not isinstance(state, dict): |
| 419 | return None |
| 420 | obj = cls(client_config=state.get('client_config')) |
| 421 | obj.credentials_json = state.get('credentials_json') |
| 422 | if obj.credentials_json: |
| 423 | # Rebuild google.oauth2.credentials.Credentials from the |
| 424 | # persisted token dict. Credentials() ignores unknown kwargs, |
| 425 | # so id_token (kept in the dict for API parity) is dropped. |
| 426 | from google.oauth2.credentials import Credentials |
| 427 | obj._credentials = Credentials( |
| 428 | token=obj.credentials_json.get('token'), |
| 429 | refresh_token=obj.credentials_json.get('refresh_token'), |
| 430 | token_uri=obj.credentials_json.get('token_uri'), |
| 431 | client_id=obj.credentials_json.get('client_id'), |
| 432 | client_secret=obj.credentials_json.get('client_secret'), |
| 433 | scopes=obj.credentials_json.get('scopes'), |
| 434 | ) |
| 435 | obj._redirect_url = state.get('redirect_url') |
| 436 | obj._project_id = state.get('project_id') |
| 437 | obj._regions = state.get('regions', []) or [] |
| 438 | obj._availability_zones = state.get('availability_zones', {}) or {} |
| 439 | obj._verification_successful = bool( |
| 440 | state.get('verification_successful', False)) |
| 441 | obj._verification_error = state.get('verification_error') |
| 442 | return obj |
| 443 | |
| 444 | def get_auth_url(self, host_url): |
| 445 | """ |