Debian Codegraph @

docs/examples/native_app.rst

raw
1 .. _examples_native_app_login:
2
3 Native App Login
4 ----------------
5
6 This is an example of the use of the Globus SDK to carry out an OAuth2
7 Native App Authentication flow.
8
9 The goal here is to have a user authenticate in Globus Auth, and for the SDK
10 to procure tokens which may be used to authenticate SDK calls against various
11 services for that user.
12
13 Get a Client
14 ~~~~~~~~~~~~
15
16 In order to complete an OAuth2 flow to get tokens, you must have a client
17 definition registered with Globus Auth.
18 To do so, follow the relevant documentation for the
19 `Globus Auth Service <https://docs.globus.org/api/auth/>`_ or go directly to
20 `developers.globus.org <https://developers.globus.org/>`_ to do the
21 registration.
22
23 Make sure, when registering your application, that you enter
24 ``https://auth.globus.org/v2/web/auth-code`` into the "Redirect URIs" field.
25 This is necessary to leverage the default behavior of the SDK, and is typically
26 sufficient for this type of application.
27
28
29 Do the Flow
30 ~~~~~~~~~~~
31
32 If you want to copy-paste an example, you'll need at least a ``client_id`` for
33 your ``AuthClient`` object.
34 You should also specifically use the :class:`NativeAppAuthClient
35 <globus_sdk.NativeAppAuthClient>` type of ``AuthClient``, as it has been
36 customized to handle this flow.
37
38 The shortest version of the flow looks like this:
39
40 .. code-block:: python
41
42 import globus_sdk
43
44 # you must have a client ID
45 # for demonstration purposes, this is the tutorial client ID
46 CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2"
47
48 client = globus_sdk.NativeAppAuthClient(CLIENT_ID)
49 client.oauth2_start_flow()
50
51 authorize_url = client.oauth2_get_authorize_url()
52 print("Please go to this URL and login: {0}".format(authorize_url))
53
54 auth_code = input("Please enter the code you get after login here: ").strip()
55 token_response = client.oauth2_exchange_code_for_tokens(auth_code)
56
57 # the useful values that you want at the end of this
58 globus_auth_data = token_response.by_resource_server["auth.globus.org"]
59 globus_transfer_data = token_response.by_resource_server["transfer.api.globus.org"]
60 globus_auth_token = globus_auth_data["access_token"]
61 globus_transfer_token = globus_transfer_data["access_token"]
62
63
64 With Refresh Tokens
65 ~~~~~~~~~~~~~~~~~~~
66
67 The flow above will give you access tokens (short-lived credentials), good for
68 one-off operations.
69 However, if you want a persistent credential to access the logged-in user's
70 Globus resources, you need to request a long-lived credential called a Refresh
71 Token.
72
73 ``refresh_tokens`` is a boolean option to the ``oauth2_start_flow`` method.
74 When False, the flow will terminate with a collection of Access Tokens, which
75 are simple limited lifetime credentials for accessing services. When True, the
76 flow will terminate not only with the Access Tokens, but additionally with a
77 set of Refresh Tokens which can be used **indefinitely** to request new Access
78 Tokens. The default is False.
79
80 Simply add this option to the example above:
81
82 .. code-block:: python
83
84 client.oauth2_start_flow(refresh_tokens=True)