| 1 |
.. _example_group_listing: |
| 2 |
|
| 3 |
Group Listing Script |
| 4 |
-------------------- |
| 5 |
|
| 6 |
This example script provides a CSV-formatted listing of the current user's |
| 7 |
groups. |
| 8 |
|
| 9 |
It uses the tutorial client ID from the :ref:`tutorials <tutorials>`. |
| 10 |
For simplicity, the script will prompt for login on each use. |
| 11 |
|
| 12 |
.. code-block:: python |
| 13 |
|
| 14 |
import globus_sdk |
| 15 |
from globus_sdk.scopes import GroupsScopes |
| 16 |
|
| 17 |
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2" |
| 18 |
auth_client = globus_sdk.NativeAppAuthClient(CLIENT_ID) |
| 19 |
|
| 20 |
auth_client.oauth2_start_flow( |
| 21 |
requested_scopes=GroupsScopes.view_my_groups_and_memberships |
| 22 |
) |
| 23 |
authorize_url = auth_client.oauth2_get_authorize_url() |
| 24 |
print(f"Please go to this URL and login:\n\n{authorize_url}\n") |
| 25 |
|
| 26 |
auth_code = input("Please enter the code here: ").strip() |
| 27 |
tokens = auth_client.oauth2_exchange_code_for_tokens(auth_code) |
| 28 |
groups_tokens = tokens.by_resource_server["groups.api.globus.org"] |
| 29 |
|
| 30 |
# construct an AccessTokenAuthorizer and use it to construct the |
| 31 |
# TransferClient |
| 32 |
groups_client = globus_sdk.GroupsClient( |
| 33 |
authorizer=globus_sdk.AccessTokenAuthorizer(groups_tokens["access_token"]) |
| 34 |
) |
| 35 |
|
| 36 |
# print out in CSV format |
| 37 |
# note that 'name' could have a comma in it, so this is slightly unsafe |
| 38 |
print("ID,Name,Type,Session Enforcement,Roles") |
| 39 |
for group in groups_client.get_my_groups(): |
| 40 |
# parse the group to get data for output |
| 41 |
if group.get("enforce_session"): |
| 42 |
session_enforcement = "strict" |
| 43 |
else: |
| 44 |
session_enforcement = "not strict" |
| 45 |
roles = ",".join({m["role"] for m in group["my_memberships"]}) |
| 46 |
|
| 47 |
print( |
| 48 |
",".join( |
| 49 |
[ |
| 50 |
group["id"], |
| 51 |
group["name"], |
| 52 |
group["group_type"], |
| 53 |
session_enforcement, |
| 54 |
roles, |
| 55 |
] |
| 56 |
) |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
.. _example_group_listing_with_token_storage: |
| 61 |
|
| 62 |
Group Listing With Token Storage |
| 63 |
-------------------------------- |
| 64 |
|
| 65 |
``globus_sdk.token_storage.legacy`` provides tools for managing refresh tokens. |
| 66 |
The following example script shows how you might use this to provide a complete |
| 67 |
script which lists the current user's groups using refresh tokens. |
| 68 |
|
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
import os |
| 73 |
|
| 74 |
from globus_sdk import GroupsClient, NativeAppAuthClient, RefreshTokenAuthorizer |
| 75 |
from globus_sdk.token_storage.legacy import SimpleJSONFileAdapter |
| 76 |
|
| 77 |
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2" |
| 78 |
AUTH_CLIENT = NativeAppAuthClient(CLIENT_ID) |
| 79 |
MY_FILE_ADAPTER = SimpleJSONFileAdapter( |
| 80 |
os.path.expanduser("~/.list-my-globus-groups-tokens.json") |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
def do_login_flow(): |
| 85 |
AUTH_CLIENT.oauth2_start_flow( |
| 86 |
requested_scopes=GroupsClient.scopes.view_my_groups_and_memberships, |
| 87 |
refresh_tokens=True, |
| 88 |
) |
| 89 |
authorize_url = AUTH_CLIENT.oauth2_get_authorize_url() |
| 90 |
print(f"Please go to this URL and login:\n\n{authorize_url}\n") |
| 91 |
auth_code = input("Please enter the code here: ").strip() |
| 92 |
tokens = AUTH_CLIENT.oauth2_exchange_code_for_tokens(auth_code) |
| 93 |
return tokens |
| 94 |
|
| 95 |
|
| 96 |
if not MY_FILE_ADAPTER.file_exists(): |
| 97 |
# do a login flow, getting back initial tokens |
| 98 |
response = do_login_flow() |
| 99 |
# now store the tokens and pull out the Groups tokens |
| 100 |
MY_FILE_ADAPTER.store(response) |
| 101 |
tokens = response.by_resource_server[GroupsClient.resource_server] |
| 102 |
else: |
| 103 |
# otherwise, we already did login; load the tokens from that file |
| 104 |
tokens = MY_FILE_ADAPTER.get_token_data(GroupsClient.resource_server) |
| 105 |
|
| 106 |
# construct the RefreshTokenAuthorizer which writes back to storage on refresh |
| 107 |
authorizer = RefreshTokenAuthorizer( |
| 108 |
tokens["refresh_token"], |
| 109 |
AUTH_CLIENT, |
| 110 |
access_token=tokens["access_token"], |
| 111 |
expires_at=tokens["expires_at_seconds"], |
| 112 |
on_refresh=MY_FILE_ADAPTER.on_refresh, |
| 113 |
) |
| 114 |
# use that authorizer to authorize the activity of the groups client |
| 115 |
groups_client = GroupsClient(authorizer=authorizer) |
| 116 |
|
| 117 |
# print out in CSV format |
| 118 |
# note that 'name' could have a comma in it, so this is slightly unsafe |
| 119 |
print("ID,Name,Type,Session Enforcement,Roles") |
| 120 |
for group in groups_client.get_my_groups(): |
| 121 |
# parse the group to get data for output |
| 122 |
if group.get("enforce_session"): |
| 123 |
session_enforcement = "strict" |
| 124 |
else: |
| 125 |
session_enforcement = "not strict" |
| 126 |
roles = ",".join({m["role"] for m in group["my_memberships"]}) |
| 127 |
|
| 128 |
print( |
| 129 |
",".join( |
| 130 |
[ |
| 131 |
group["id"], |
| 132 |
group["name"], |
| 133 |
group["group_type"], |
| 134 |
session_enforcement, |
| 135 |
roles, |
| 136 |
] |
| 137 |
) |
| 138 |
) |