Debian Codegraph @

docs/user_guide/usage_patterns/service_accounts/using_client_app/index.rst

raw
1 .. _userguide_using_client_app:
2
3 Using a ClientApp with a Service Account
4 ========================================
5
6 Once you have a client ID and secret from an app registration for a service
7 account, the SDK has a few tools which can use those credentials to acquire
8 tokens, to talk to various Globus Services.
9
10 The easiest tool for this job is a ``ClientApp``, a flavor of ``GlobusApp``
11 designed to work with service accounts.
12
13 Instantiating a ClientApp
14 -------------------------
15
16 Constructing an app takes three required parameters,
17
18 - a human readable name to identify your app in HTTP requests and token caching (e.g., "My Cool Weathervane").
19 - this does not need match the name you supplied during client registration.
20 - the client ID
21 - the client secret
22
23 as in:
24
25 .. code-block:: python
26
27 import globus_sdk
28
29 CLIENT_ID = "YOUR ID HERE"
30 CLIENT_SECRET = "YOUR SECRET HERE"
31
32 app = globus_sdk.ClientApp(
33 "sample-app",
34 client_id=CLIENT_ID,
35 client_secret=CLIENT_SECRET,
36 )
37
38 Using the App with a Globus Service
39 -----------------------------------
40
41 The resulting app can then be passed to any SDK client class to create an API
42 client object which uses the app for authentication requirements.
43 For example, to use the ``app`` object to run an ``ls`` on one of the tutorial
44 collections:
45
46 .. code-block:: python
47
48 TUTORIAL_COLLECTION = "6c54cade-bde5-45c1-bdea-f4bd71dba2cc"
49
50 with globus_sdk.TransferClient(app=app) as tc:
51 tc.add_app_data_access_scope(TUTORIAL_COLLECTION)
52 ls_result = tc.operation_ls(TUTORIAL_COLLECTION, path="/home/share/godata")
53
54 .. note::
55
56 Unfortunately, there are two different meanings of the word "client" in use
57 in this example!
58
59 A ``TransferClient`` is a "client" in the sense that it is a local object
60 which provides access to the Globus Transfer Service.
61 The ``CLIENT_ID``, ``CLIENT_SECRET``, and ``ClientApp`` are all using the
62 word "client" in reference to the OAuth2 standard's definition of a "client"
63 as a registered app, a different meaning for the same word.
64
65 Using Memory Storage
66 --------------------
67
68 Unlike user logins, client credentials can't be "logged out" vs "logged in" --
69 unless they are deleted via the Globus Auth service, they are always active.
70
71 As a result, unlike applications which provide user logins, ``ClientApp``\s will
72 very often prefer to store any tokens they are using in memory. The tokens will
73 be cached and reused over the lifetime of the process, but never persisted to
74 disk.
75
76 To configure an app in this way, simply add a ``config`` to the app
77 initialization to select the ``"memory"`` storage type:
78
79 .. code-block:: python
80
81 app = globus_sdk.ClientApp(
82 "sample-app",
83 client_id=CLIENT_ID,
84 client_secret=CLIENT_SECRET,
85 config=globus_sdk.GlobusAppConfig(token_storage="memory"),
86 )
87
88 Complete Example
89 ----------------
90
91 In addition to leveraging all of the elements described above, this example enhances the code sample to
92 use the ``ClientApp``'s context manager interface to close token storage.
93 We also add a loop of ``print()`` usages on the ``ls`` result to show some output:
94
95 .. literalinclude:: client_app_ls.py
96 :caption: ``client_app_ls.py`` [:download:`download <client_app_ls.py>`]
97 :language: python