Debian Codegraph @

docs/examples/minimal_transfer_script/transfer_consent_required_proactive.py

raw
1 import argparse
2
3 import globus_sdk
4 from globus_sdk.scopes import TransferScopes
5
6 parser = argparse.ArgumentParser()
7 parser.add_argument("SRC")
8 parser.add_argument("DST")
9 args = parser.parse_args()
10
11 CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2"
12 auth_client = globus_sdk.NativeAppAuthClient(CLIENT_ID)
13
14
15 # we will need to do the login flow potentially twice, so define it as a
16 # function
17 #
18 # we default to using the Transfer "all" scope, but it is settable here
19 # look at the ConsentRequired handler below for how this is used
20 def login_and_get_transfer_client(*, scopes=TransferScopes.all):
21 # note that 'requested_scopes' can be a single scope or a list
22 # this did not matter in previous examples but will be leveraged in
23 # this one
24 auth_client.oauth2_start_flow(requested_scopes=scopes)
25 authorize_url = auth_client.oauth2_get_authorize_url()
26 print(f"Please go to this URL and login:\n\n{authorize_url}\n")
27
28 auth_code = input("Please enter the code here: ").strip()
29 tokens = auth_client.oauth2_exchange_code_for_tokens(auth_code)
30 transfer_tokens = tokens.by_resource_server["transfer.api.globus.org"]
31
32 # return the TransferClient object, as the result of doing a login
33 return globus_sdk.TransferClient(
34 authorizer=globus_sdk.AccessTokenAuthorizer(transfer_tokens["access_token"])
35 )
36
37
38 # get an initial client to try with, which requires a login flow
39 transfer_client = login_and_get_transfer_client()
40
41 # now, try an ls on the source and destination to see if ConsentRequired
42 # errors are raised
43 consent_required_scopes = []
44
45
46 def check_for_consent_required(target):
47 try:
48 transfer_client.operation_ls(target, path="/")
49 # catch all errors and discard those other than ConsentRequired
50 # e.g. ignore PermissionDenied errors as not relevant
51 except globus_sdk.TransferAPIError as err:
52 if err.info.consent_required:
53 consent_required_scopes.extend(err.info.consent_required.required_scopes)
54
55
56 check_for_consent_required(args.SRC)
57 check_for_consent_required(args.DST)
58
59 # the block above may or may not populate this list
60 # but if it does, handle ConsentRequired with a new login
61 if consent_required_scopes:
62 print(
63 "One of your endpoints requires consent in order to be used.\n"
64 "You must login a second time to grant consents.\n\n"
65 )
66 transfer_client = login_and_get_transfer_client(scopes=consent_required_scopes)
67
68 # from this point onwards, the example is exactly the same as the reactive
69 # case, including the behavior to retry on ConsentRequiredErrors. This is
70 # not obvious, but there are cases in which it is necessary -- for example,
71 # if a user consents at the start, but the process of building task_data is
72 # slow, they could revoke their consent before the submission step
73 #
74 # in the common case, a single submission with no retry would suffice
75
76 task_data = globus_sdk.TransferData(
77 source_endpoint=args.SRC, destination_endpoint=args.DST
78 )
79 task_data.add_item(
80 "/share/godata/file1.txt", # source
81 "/~/example-transfer-script-destination.txt", # dest
82 )
83
84
85 def do_submit(client):
86 task_doc = client.submit_transfer(task_data)
87 task_id = task_doc["task_id"]
88 print(f"submitted transfer, task_id={task_id}")
89
90
91 try:
92 do_submit(transfer_client)
93 except globus_sdk.TransferAPIError as err:
94 if not err.info.consent_required:
95 raise
96 print(
97 "Encountered a ConsentRequired error.\n"
98 "You must login a second time to grant consents.\n\n"
99 )
100 transfer_client = login_and_get_transfer_client(
101 scopes=err.info.consent_required.required_scopes
102 )
103 do_submit(transfer_client)