-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_copy_paste.py
executable file
·58 lines (37 loc) · 1.87 KB
/
example_copy_paste.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import webbrowser
from globus_sdk import AccessTokenAuthorizer, NativeAppAuthClient, TransferClient
from utils import enable_requests_logging, is_remote_session
CLIENT_ID = "1b0dc9d3-0a2b-4000-8bd6-90fb6a79be86"
REDIRECT_URI = "https://auth.globus.org/v2/web/auth-code"
SCOPES = "openid email profile " "urn:globus:auth:scope:transfer.api.globus.org:all"
TUTORIAL_ENDPOINT_ID = "ddb59aef-6d04-11e5-ba46-22000b92c6ec"
# uncomment the next line to enable debug logging for network requests
# enable_requests_logging()
def do_native_app_authentication(client_id, redirect_uri, requested_scopes=None):
"""
Does a Native App authentication flow and returns a
dict of tokens keyed by service name.
"""
client = NativeAppAuthClient(client_id=client_id)
client.oauth2_start_flow(requested_scopes=SCOPES, redirect_uri=redirect_uri)
url = client.oauth2_get_authorize_url()
print("Native App Authorization URL: \n{}".format(url))
if not is_remote_session():
webbrowser.open(url, new=1)
auth_code = input("Enter the auth code: ").strip()
token_response = client.oauth2_exchange_code_for_tokens(auth_code)
# return a set of tokens, organized by resource server name
return token_response.by_resource_server
def main():
# start the Native App authentication process
tokens = do_native_app_authentication(CLIENT_ID, REDIRECT_URI)
transfer_token = tokens["transfer.api.globus.org"]["access_token"]
authorizer = AccessTokenAuthorizer(access_token=transfer_token)
transfer = TransferClient(authorizer=authorizer)
# print out a directory listing from an endpoint
transfer.endpoint_autoactivate(TUTORIAL_ENDPOINT_ID)
for entry in transfer.operation_ls(TUTORIAL_ENDPOINT_ID, path="/~/"):
print(entry["name"] + ("/" if entry["type"] == "dir" else ""))
if __name__ == "__main__":
main()