Skip to content

Commit 0e6eade

Browse files
author
Jon Wayne Parrott
authored
Allow access_type parameter to be overriden (googleapis#16)
1 parent d330c99 commit 0e6eade

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

packages/google-auth-oauthlib/google_auth_oauthlib/flow.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ class Flow(object):
8787
https://console.developers.google.com/apis/credentials
8888
"""
8989

90-
def __init__(self, oauth2session, client_type, client_config):
90+
def __init__(
91+
self, oauth2session, client_type, client_config,
92+
redirect_uri=None):
9193
"""
9294
Args:
9395
oauth2session (requests_oauthlib.OAuth2Session):
@@ -96,6 +98,9 @@ def __init__(self, oauth2session, client_type, client_config):
9698
``installed``.
9799
client_config (Mapping[str, Any]): The client
98100
configuration in the Google `client secrets`_ format.
101+
redirect_uri (str): The OAuth 2.0 redirect URI if known at flow
102+
creation time. Otherwise, it will need to be set using
103+
:attr:`redirect_uri`.
99104
100105
.. _client secrets:
101106
https://developers.google.com/api-client-library/python/guide
@@ -107,6 +112,7 @@ def __init__(self, oauth2session, client_type, client_config):
107112
"""Mapping[str, Any]: The OAuth 2.0 client configuration."""
108113
self.oauth2session = oauth2session
109114
"""requests_oauthlib.OAuth2Session: The OAuth 2.0 session."""
115+
self.redirect_uri = redirect_uri
110116

111117
@classmethod
112118
def from_client_config(cls, client_config, scopes, **kwargs):
@@ -200,9 +206,9 @@ def authorization_url(self, **kwargs):
200206
:class:`Flow` instance to obtain the token, you will need to
201207
specify the ``state`` when constructing the :class:`Flow`.
202208
"""
209+
kwargs.setdefault('access_type', 'offline')
203210
url, state = self.oauth2session.authorization_url(
204-
self.client_config['auth_uri'],
205-
access_type='offline', **kwargs)
211+
self.client_config['auth_uri'], **kwargs)
206212

207213
return url, state
208214

@@ -229,10 +235,9 @@ def fetch_token(self, **kwargs):
229235
:meth:`credentials` to obtain a
230236
:class:`~google.auth.credentials.Credentials` instance.
231237
"""
238+
kwargs.setdefault('client_secret', self.client_config['client_secret'])
232239
return self.oauth2session.fetch_token(
233-
self.client_config['token_uri'],
234-
client_secret=self.client_config['client_secret'],
235-
**kwargs)
240+
self.client_config['token_uri'], **kwargs)
236241

237242
@property
238243
def credentials(self):

packages/google-auth-oauthlib/tests/test_flow.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ def test_authorization_url(self, instance):
8181
access_type='offline',
8282
prompt='consent')
8383

84+
def test_authorization_url_access_type(self, instance):
85+
scope = 'scope_one'
86+
instance.oauth2session.scope = [scope]
87+
authorization_url_patch = mock.patch.object(
88+
instance.oauth2session, 'authorization_url',
89+
wraps=instance.oauth2session.authorization_url)
90+
91+
with authorization_url_patch as authorization_url_spy:
92+
url, _ = instance.authorization_url(access_type='meep')
93+
94+
assert CLIENT_SECRETS_INFO['web']['auth_uri'] in url
95+
assert scope in url
96+
authorization_url_spy.assert_called_with(
97+
CLIENT_SECRETS_INFO['web']['auth_uri'],
98+
access_type='meep')
99+
84100
def test_fetch_token(self, instance):
85101
fetch_token_patch = mock.patch.object(
86102
instance.oauth2session, 'fetch_token', autospec=True,

0 commit comments

Comments
 (0)