5555
5656import json
5757
58- import requests_oauthlib
59-
6058import google .auth .transport .requests
6159import google .oauth2 .credentials
62-
63- _REQUIRED_CONFIG_KEYS = frozenset (('auth_uri' , 'token_uri' , 'client_id' ))
60+ import google .oauth2 .oauthlib
6461
6562
6663class Flow (object ):
@@ -82,8 +79,32 @@ class Flow(object):
8279 https://console.developers.google.com/apis/credentials
8380 """
8481
85- def __init__ (self , client_config , scopes , ** kwargs ):
82+ def __init__ (self , oauth2session , client_type , client_config ):
8683 """
84+ Args:
85+ oauth2session (requests_oauthlib.OAuth2Session):
86+ The OAuth 2.0 session from ``requests-oauthlib``.
87+ client_type (str): The client type, either ``web`` or
88+ ``installed``.
89+ client_config (Mapping[str, Any]): The client
90+ configuration in the Google `client secrets`_ format.
91+
92+ .. _client secrets:
93+ https://developers.google.com/api-client-library/python/guide
94+ /aaa_client_secrets
95+ """
96+ self .client_type = client_type
97+ """str: The client type, either ``'web'`` or ``'installed'``"""
98+ self .client_config = client_config [client_type ]
99+ """Mapping[str, Any]: The OAuth 2.0 client configuration."""
100+ self .oauth2session = oauth2session
101+ """requests_oauthlib.OAuth2Session: The OAuth 2.0 session."""
102+
103+ @classmethod
104+ def from_client_config (cls , client_config , scopes , ** kwargs ):
105+ """Creates a :class:`requests_oauthlib.OAuth2Session` from client
106+ configuration loaded from a Google-format client secrets file.
107+
87108 Args:
88109 client_config (Mapping[str, Any]): The client
89110 configuration in the Google `client secrets`_ format.
@@ -92,6 +113,9 @@ def __init__(self, client_config, scopes, **kwargs):
92113 kwargs: Any additional parameters passed to
93114 :class:`requests_oauthlib.OAuth2Session`
94115
116+ Returns:
117+ Flow: The constructed Flow instance.
118+
95119 Raises:
96120 ValueError: If the client configuration is not in the correct
97121 format.
@@ -100,29 +124,19 @@ def __init__(self, client_config, scopes, **kwargs):
100124 https://developers.google.com/api-client-library/python/guide
101125 /aaa_client_secrets
102126 """
103- self .client_config = None
104- """Mapping[str, Any]: The OAuth 2.0 client configuration."""
105- self .client_type = None
106- """str: The client type, either ``'web'`` or ``'installed'``"""
107-
108127 if 'web' in client_config :
109- self .client_config = client_config ['web' ]
110- self .client_type = 'web'
128+ client_type = 'web'
111129 elif 'installed' in client_config :
112- self .client_config = client_config ['installed' ]
113- self .client_type = 'installed'
130+ client_type = 'installed'
114131 else :
115132 raise ValueError (
116133 'Client secrets must be for a web or installed app.' )
117134
118- if not _REQUIRED_CONFIG_KEYS .issubset (self .client_config .keys ()):
119- raise ValueError ('Client secrets is not in the correct format.' )
135+ session , client_config = (
136+ google .oauth2 .oauthlib .session_from_client_config (
137+ client_config , scopes , ** kwargs ))
120138
121- self .oauth2session = requests_oauthlib .OAuth2Session (
122- client_id = self .client_config ['client_id' ],
123- scope = scopes ,
124- ** kwargs )
125- """requests_oauthlib.OAuth2Session: The OAuth 2.0 session."""
139+ return cls (session , client_type , client_config )
126140
127141 @classmethod
128142 def from_client_secrets_file (cls , client_secrets_file , scopes , ** kwargs ):
@@ -142,7 +156,7 @@ def from_client_secrets_file(cls, client_secrets_file, scopes, **kwargs):
142156 with open (client_secrets_file , 'r' ) as json_file :
143157 client_config = json .load (json_file )
144158
145- return cls (client_config , scopes = scopes , ** kwargs )
159+ return cls . from_client_config (client_config , scopes = scopes , ** kwargs )
146160
147161 @property
148162 def redirect_uri (self ):
@@ -226,18 +240,8 @@ def credentials(self):
226240 Raises:
227241 ValueError: If there is no access token in the session.
228242 """
229- if not self .oauth2session .token :
230- raise ValueError (
231- 'There is no access token for this session, did you call '
232- 'fetch_token?' )
233-
234- return google .oauth2 .credentials .Credentials (
235- self .oauth2session .token ['access_token' ],
236- refresh_token = self .oauth2session .token ['refresh_token' ],
237- token_uri = self .client_config ['token_uri' ],
238- client_id = self .client_config ['client_id' ],
239- client_secret = self .client_config ['client_secret' ],
240- scopes = self .oauth2session .scope )
243+ return google .oauth2 .oauthlib .credentials_from_session (
244+ self .oauth2session , self .client_config )
241245
242246 def authorized_session (self ):
243247 """Returns a :class:`requests.Session` authorized with credentials.
0 commit comments