Globus SDK for Python

4.5.0 · active · verified Thu Apr 16

The Globus SDK for Python provides a convenient Pythonic interface to Globus web APIs, including the Auth, Transfer, Search, Flows, and Compute services. Currently at version 4.5.0, the library maintains an active development pace with minor releases typically occurring on a monthly or bi-monthly basis, incorporating new features, bug fixes, and Python version support updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a typical Native App authentication flow to obtain refresh and access tokens, then uses these tokens to create a `TransferClient` and list the user's Globus endpoints. It assumes you have registered a Native App at developers.globus.org and set its redirect URL. For production use, tokens should be stored securely.

import os
import globus_sdk

# NOTE: Register your app at developers.globus.org and get a Client ID.
# For a native app, ensure 'Native App' is checked and set 'Redirects' to
# 'https://auth.globus.org/v2/web/auth-code'
CLIENT_ID = os.environ.get('GLOBUS_CLIENT_ID', 'YOUR_NATIVE_APP_CLIENT_ID')

def authenticate_and_list_endpoints():
    client = globus_sdk.NativeAppAuthClient(CLIENT_ID)
    client.oauth2_start_flow(refresh_tokens=True)

    authorize_url = client.oauth2_get_authorize_url()
    print(f"Please go to this URL and login:\n\n{authorize_url}\n")

    auth_code = input("Please enter the code here: ").strip()
    tokens = client.oauth2_exchange_code_for_tokens(auth_code)

    # Store the tokens securely for future use. For this example, we just extract them.
    transfer_tokens = tokens.by_resource_server['transfer.api.globus.org']
    transfer_access_token = transfer_tokens['access_token']
    transfer_refresh_token = transfer_tokens['refresh_token']

    # Use a RefreshTokenAuthorizer for long-lived access
    authorizer = globus_sdk.RefreshTokenAuthorizer(
        transfer_refresh_token, client, access_token=transfer_access_token
    )
    transfer_client = globus_sdk.TransferClient(authorizer=authorizer)

    print("\nYour Globus endpoints:")
    for ep in transfer_client.endpoint_search(filter_scope='managed'):
        print(f"  {ep['display_name']} (ID: {ep['id']})")

if __name__ == "__main__":
    if CLIENT_ID == 'YOUR_NATIVE_APP_CLIENT_ID':
        print("WARNING: Replace 'YOUR_NATIVE_APP_CLIENT_ID' with your actual Globus Client ID or set the GLOBUS_CLIENT_ID environment variable.")
    authenticate_and_list_endpoints()

view raw JSON →