Skip to content

Commit e4fa5ff

Browse files
committed
Breaking change: Result of OAuth flows is now an object rather than tuple.
- account_id is added to the flow result.
1 parent e7ebb14 commit e4fa5ff

File tree

1 file changed

+72
-11
lines changed

1 file changed

+72
-11
lines changed

dropbox/oauth.py

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
'DropboxOAuth2Flow',
66
'DropboxOAuth2FlowNoRedirect',
77
'NotApprovedException',
8+
'OAuth2FlowNoRedirectResult',
9+
'OAuth2FlowResult',
810
'ProviderException',
911
]
1012

@@ -24,6 +26,66 @@
2426

2527
OAUTH_ROUTE_VERSION = '1'
2628

29+
30+
class OAuth2FlowNoRedirectResult(object):
31+
"""
32+
Authorization information for an OAuth2Flow performed with no redirect.
33+
"""
34+
35+
def __init__(self, access_token, account_id, user_id):
36+
"""
37+
Args:
38+
access_token (str): Token to be used to authenticate later
39+
requests.
40+
account_id (str): The Dropbox user's account ID. Please use this
41+
instead of the user_id.
42+
user_id (str): For backwards compatibility with API v1, please
43+
avoid using this if possible.
44+
"""
45+
self.access_token = access_token
46+
self.account_id = account_id
47+
self.user_id = user_id
48+
49+
def __repr__(self):
50+
return 'OAuth2FlowNoRedirectResult(%r, %r, %r)' % (
51+
self.access_token,
52+
self.account_id,
53+
self.user_id,
54+
)
55+
56+
57+
class OAuth2FlowResult(OAuth2FlowNoRedirectResult):
58+
"""
59+
Authorization information for an OAuth2Flow with redirect.
60+
"""
61+
62+
def __init__(self, access_token, account_id, user_id, url_state):
63+
"""
64+
Same as OAuth2FlowNoRedirectResult but with url_state.
65+
66+
Args:
67+
url_state (str): The url state that was set by
68+
:meth:`DropboxOAuth2Flow.start`.
69+
"""
70+
super(OAuth2FlowResult, self).__init__(
71+
access_token, account_id, user_id)
72+
self.url_state = url_state
73+
74+
@classmethod
75+
def from_no_redirect_result(cls, result, url_state):
76+
assert isinstance(result, OAuth2FlowNoRedirectResult)
77+
return cls(
78+
result.access_token, result.account_id, result.user_id, url_state)
79+
80+
def __repr__(self):
81+
return 'OAuth2FlowResult(%r, %r, %r, %r)' % (
82+
self.access_token,
83+
self.account_id,
84+
self.user_id,
85+
self.url_state,
86+
)
87+
88+
2789
class DropboxOAuth2FlowBase(object):
2890

2991
def __init__(self, consumer_key, consumer_secret, locale=None):
@@ -63,8 +125,12 @@ def _finish(self, code, redirect_uri):
63125

64126
access_token = d["access_token"]
65127
user_id = d["uid"]
128+
print('BASE', d)
66129

67-
return access_token, user_id
130+
return OAuth2FlowNoRedirectResult(
131+
d['access_token'],
132+
d['account_id'],
133+
d['uid'])
68134

69135
def build_path(self, target, params=None):
70136
"""Build the path component for an API URL.
@@ -170,9 +236,7 @@ def finish(self, code):
170236
171237
:param str code: The authorization code shown to the user when they
172238
approved your app.
173-
:return: A pair of ``(access_token, user_id)``. ``access_token`` is a
174-
string that can be passed to Dropbox. ``user_id`` is the
175-
Dropbox user ID (string) of the user that just approved your app.
239+
:rtype: OAuth2FlowNoRedirectResult
176240
:raises: The same exceptions as :meth:`DropboxOAuth2Flow.finish()`.
177241
"""
178242
return self._finish(code, None)
@@ -288,11 +352,7 @@ def finish(self, query_params):
288352
289353
:param dict query_params: The query parameters on the GET request to
290354
your redirect URI.
291-
:return: A tuple of ``(access_token, user_id, url_state)``.
292-
``access_token`` can be used to construct a
293-
:class:`dropbox.dropbox.Dropbox`. ``user_id`` is the Dropbox user
294-
ID (string) of the user that just approved your app. ``url_state``
295-
is the value you originally passed in to :meth:`start()`.
355+
:rtype: OAuth2FlowResult
296356
:raises: :class:`BadRequestException` If the redirect URL was missing
297357
parameters or if the given parameters were not valid.
298358
:raises: :class:`BadStateException` If there's no CSRF token in the
@@ -366,8 +426,9 @@ def finish(self, query_params):
366426

367427
# If everything went ok, make the network call to get an access token.
368428

369-
access_token, user_id = self._finish(code, self.redirect_uri)
370-
return access_token, user_id, url_state
429+
no_redirect_result = self._finish(code, self.redirect_uri)
430+
return OAuth2FlowResult.from_no_redirect_result(
431+
no_redirect_result, url_state)
371432

372433

373434
class BadRequestException(Exception):

0 commit comments

Comments
 (0)