How to Create A Minimal Script¶
This is a basic tutorial in the use of the Globus SDK.
You can jump right in by using the CLIENT_ID
seen in the example code blocks below!
That is the ID of the tutorial client, which lets you get started quickly and easily.
When you are ready to create your own application, follow
How to Register an App in Globus Auth and use
its CLIENT_ID
in the rest of the tutorial to get your own app setup.
For readers who prefer to start with complete working examples, jump ahead to the example scripts at the end before reviewing the doc.
Define your App Object¶
Accessing Globus APIs as a user requires that you login to your new app and get it tokens, credentials providing access the service.
The SDK provides a construct which represents an application. A
GlobusApp
is an object which can respond to requests for new or
existing tokens and store those tokens (by default, in ~/.globus/app/
).
UserApp
is the type of GlobusApp
used for human user,
login-driven scenarios – this is always the right type of application object
to use when you want to interact with services using your own account.
Start by defining an application object using UserApp
:
import globus_sdk
# this is the tutorial client ID
# replace this string with your ID for production use
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2"
# create your app
my_app = globus_sdk.UserApp("my-user-app", client_id=CLIENT_ID)
Note
The default behavior for a UserApp
is to do a CLI-based login flow.
This behavior, and more, can be disabled or customized in numerous ways.
For the full menu of options, look at the documentation about Using a GlobusApp!
Access the APIs via Clients¶
Once you have an app defined, you can use it with client objects to access various Globus APIs.
When you attempt to interact with a service using an app-bound service client, the app will automatically prompt you to login if valid credentials are unavailable.
Start by defining the client object:
groups_client = globus_sdk.GroupsClient(app=my_app)
And now you can use it to perform some simple interaction, like listing your groups:
# call out to the Groups service to get a listing
my_groups = groups_client.get_my_groups()
# print in CSV format
print("ID,Name,Roles")
for group in my_groups:
roles = "|".join({m["role"] for m in group["my_memberships"]})
print(",".join([group["id"], f'"{group["name"]}"', roles]))
When groups_client.get_my_groups()
runs in the example above, the SDK
will prompt you to login.
Summary: Complete Examples¶
For ease of use, here are a pair of examples.
One of them is exactly the same as the tutorial steps above, in a single block. The other example includes an explicit login step, so you can control when that login flow happens!
These examples are complete. They should run without errors “as is”.
import globus_sdk
# this is the tutorial client ID
# replace this string with your ID for production use
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2"
# create your app
my_app = globus_sdk.UserApp("my-user-app", client_id=CLIENT_ID)
# create a client with your app
groups_client = globus_sdk.GroupsClient(app=my_app)
# call out to the Groups service to get a listing
my_groups = groups_client.get_my_groups()
# print in CSV format
print("ID,Name,Roles")
for group in my_groups:
roles = "|".join({m["role"] for m in group["my_memberships"]})
print(",".join([group["id"], f'"{group["name"]}"', roles]))
This example is very similar to the tutorial, but uses a separate login step.
import globus_sdk
# this is the tutorial client ID
# replace this string with your ID for production use
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2"
# create your app
my_app = globus_sdk.UserApp("my-user-app", client_id=CLIENT_ID)
# create a client with your app
groups_client = globus_sdk.GroupsClient(app=my_app)
# Important! The login step needs to happen after the `groups_client` is created
# so that the app will know that you need credentials for Globus Groups
my_app.login()
# call out to the Groups service to get a listing
my_groups = groups_client.get_my_groups()
# print in CSV format
print("ID,Name,Roles")
for group in my_groups:
roles = "|".join({m["role"] for m in group["my_memberships"]})
print(",".join([group["id"], f'"{group["name"]}"', roles]))