15
15
"""OAuth 2.0 Authorization Flow
16
16
17
17
This module provides integration with `requests-oauthlib`_ for running the
18
- `OAuth 2.0 Authorization Flow`_ and acquiring user credentials.
18
+ `OAuth 2.0 Authorization Flow`_ and acquiring user credentials. See
19
+ `Using OAuth 2.0 to Access Google APIs`_ for an overview of OAuth 2.0
20
+ authorization scenarios Google APIs support.
19
21
20
- Here's an example of using :class:`Flow` with the installed application
21
- authorization flow::
22
+ Here's an example of using :class:`InstalledAppFlow`::
22
23
23
- from google_auth_oauthlib.flow import Flow
24
+ from google_auth_oauthlib.flow import InstalledAppFlow
24
25
25
26
# Create the flow using the client secrets file from the Google API
26
27
# Console.
27
- flow = Flow.from_client_secrets_file(
28
- 'path/to/client_secrets.json',
29
- scopes=['profile', 'email'],
30
- redirect_uri='urn:ietf:wg:oauth:2.0:oob')
28
+ flow = InstalledAppFlow.from_client_secrets_file(
29
+ 'client_secrets.json',
30
+ scopes=['profile', 'email'])
31
31
32
- # Tell the user to go to the authorization URL.
33
- auth_url, _ = flow.authorization_url(prompt='consent')
34
-
35
- print('Please go to this URL: {}'.format(auth_url))
36
-
37
- # The user will get an authorization code. This code is used to get the
38
- # access token.
39
- code = input('Enter the authorization code: ')
40
- flow.fetch_token(code=code)
32
+ flow.run_local_server()
41
33
42
34
# You can use flow.credentials, or you can just get a requests session
43
35
# using flow.authorized_session.
44
36
session = flow.authorized_session()
45
- print(session.get('https://www.googleapis.com/userinfo/v2/me').json())
46
37
47
- This particular flow can be handled entirely by using
48
- :class:`InstalledAppFlow`.
38
+ profile_info = session.get(
39
+ 'https://www.googleapis.com/userinfo/v2/me').json()
40
+
41
+ print(profile_info)
42
+ # {'name': '...', 'email': '...', ...}
49
43
50
44
.. _requests-oauthlib: http://requests-oauthlib.readthedocs.io/en/stable/
51
45
.. _OAuth 2.0 Authorization Flow:
52
46
https://tools.ietf.org/html/rfc6749#section-1.2
47
+ .. _Using OAuth 2.0 to Access Google APIs:
48
+ https://developers.google.com/identity/protocols/oauth2
49
+
53
50
"""
54
51
from base64 import urlsafe_b64encode
55
52
import hashlib
56
53
import json
57
54
import logging
55
+ import warnings
58
56
59
57
try :
60
58
from secrets import SystemRandom
72
70
73
71
74
72
_LOGGER = logging .getLogger (__name__ )
73
+ _OOB_REDIRECT_URIS = [
74
+ "urn:ietf:wg:oauth:2.0:oob" ,
75
+ "urn:ietf:wg:oauth:2.0:oob:auto" ,
76
+ "oob" ,
77
+ ]
75
78
76
79
77
80
class Flow (object ):
@@ -211,6 +214,17 @@ def redirect_uri(self):
211
214
212
215
@redirect_uri .setter
213
216
def redirect_uri (self , value ):
217
+ if value in _OOB_REDIRECT_URIS :
218
+ warnings .warn (
219
+ "'{}' is an OOB redirect URI. The OAuth out-of-band (OOB) flow is deprecated. "
220
+ "New clients will be unable to use this flow starting on Feb 28, 2022. "
221
+ "This flow will be deprecated for all clients on Oct 3, 2022. "
222
+ "Migrate to an alternative flow. "
223
+ "See https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob" .format (
224
+ value
225
+ ),
226
+ DeprecationWarning ,
227
+ )
214
228
self .oauth2session .redirect_uri = value
215
229
216
230
def authorization_url (self , ** kwargs ):
@@ -325,9 +339,7 @@ class InstalledAppFlow(Flow):
325
339
local development or applications that are installed on a desktop operating
326
340
system.
327
341
328
- This flow has two strategies: The console strategy provided by
329
- :meth:`run_console` and the local server strategy provided by
330
- :meth:`run_local_server`.
342
+ This flow uses a local server strategy provided by :meth:`run_local_server`.
331
343
332
344
Example::
333
345
@@ -348,8 +360,8 @@ class InstalledAppFlow(Flow):
348
360
# {'name': '...', 'email': '...', ...}
349
361
350
362
351
- Note that these aren 't the only two ways to accomplish the installed
352
- application flow, they are just the most common ways . You can use the
363
+ Note that this isn 't the only way to accomplish the installed
364
+ application flow, just one of the most common. You can use the
353
365
:class:`Flow` class to perform the same flow with different methods of
354
366
presenting the authorization URL to the user or obtaining the authorization
355
367
response, such as using an embedded web view.
@@ -381,6 +393,15 @@ def run_console(
381
393
):
382
394
"""Run the flow using the console strategy.
383
395
396
+ .. deprecated:: 0.5.0
397
+ Use :meth:`run_local_server` instead.
398
+
399
+ The OAuth out-of-band (OOB) flow is deprecated. New clients will be unable to
400
+ use this flow starting on Feb 28, 2022. This flow will be deprecated
401
+ for all clients on Oct 3, 2022. Migrate to an alternative flow.
402
+
403
+ See https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob"
404
+
384
405
The console strategy instructs the user to open the authorization URL
385
406
in their browser. Once the authorization is complete the authorization
386
407
server will give the user a code. The user then must copy & paste this
@@ -399,6 +420,13 @@ def run_console(
399
420
for the user.
400
421
"""
401
422
kwargs .setdefault ("prompt" , "consent" )
423
+ warnings .warn (
424
+ "New clients will be unable to use `InstalledAppFlow.run_console` "
425
+ "starting on Feb 28, 2022. All clients will be unable to use this method starting on Oct 3, 2022. "
426
+ "Use `InstalledAppFlow.run_local_server` instead. For details on the OOB flow deprecation, "
427
+ "see https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob" ,
428
+ DeprecationWarning ,
429
+ )
402
430
403
431
self .redirect_uri = self ._OOB_REDIRECT_URI
404
432
0 commit comments