Skip to content

Globus User Credential Lookup Sample Code

Steven Lee edited this page Aug 7, 2018 · 1 revision

Usage

  1. Install Globus SDK by running pip install globus-sdk as root.
  2. Obtain access and refresh tokens if you don't have them:
    1. Define CLIENT_ID environment variable with your app's client ID.
    2. Run the get_refresh_token.py code below.
  3. Update the following environment variables with the information obtained from the previous step:
    • ACCESS_TOKEN
    • REFRESH_TOKEN
    • EXPIRES_AT_S
  4. Run the get_globus_identities_from_sub.py like this:
    get_globus_identities_from_sub.py -s <globus sub>
    

Code

get_refresh_token.py

#!/usr/bin/python
#
# get_refresh_token.py
#
import os
import globus_sdk
from globus_sdk import AuthClient,AccessTokenAuthorizer

CLIENT_ID = os.environ['CLIENT_ID']

client = globus_sdk.NativeAppAuthClient(CLIENT_ID)
client.oauth2_start_flow(refresh_tokens=True)

authorize_url = client.oauth2_get_authorize_url()
print('Please go to this URL and login: {0}'.format(authorize_url))

# this is to work on Python2 and Python3 -- you can just use raw_input() or
# input() for your specific version
get_input = getattr(__builtins__, 'raw_input', input)
auth_code = get_input(
    'Please enter the code you get after login here: ').strip()
token_response = client.oauth2_exchange_code_for_tokens(auth_code)

print token_response.by_resource_server['auth.globus.org']

get_globus_identities_from_sub.py

#!/usr/bin/python
import os
import argparse
import globus_sdk
from globus_sdk import AuthClient,AccessTokenAuthorizer

parser = argparse.ArgumentParser(description='Returns Globus user identity fields')
parser.add_argument('-s', dest='globus_sub', required=True,
                   help='Globus sub to look up')
args = parser.parse_args()

CLIENT_ID = os.environ['CLIENT_ID']
client = globus_sdk.NativeAppAuthClient(CLIENT_ID)

my_authorizer = globus_sdk.RefreshTokenAuthorizer(
	os.environ['REFRESH_TOKEN'], client, 
	access_token=os.environ['ACCESS_TOKEN'], 
	expires_at=int(os.environ['EXPIRES_AT_S'])) 

ac = AuthClient(authorizer=my_authorizer)
r = ac.get_identities(ids=args.globus_sub)
print r.data['identities']
You can’t perform that action at this time.