Last active
May 17, 2019 15:55
-
-
Save ihardy/1fcd986a36d5e77f15c3da27715deb59 to your computer and use it in GitHub Desktop.
logs in, copy to /usr/local/bin/gettoken and chmod +x. Then $ export token=`gettoken -o snapshot` or `gettoken -o http://someokapiurl`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import argparse | |
| import json | |
| import urllib.request | |
| REFERNCE_ENVS_SHORTCUTS = { | |
| 'snapshot' : 'https://folio-snapshot-okapi.aws.indexdata.com', | |
| 'snapshot-core' : 'https://folio-snapshot-core-okapi.aws.indexdata.com', | |
| 'snapshot-test' : 'https://folio-snapshot-test-okapi.aws.indexdata.com', | |
| 'testing' : 'https://folio-testing-okapi.aws.indexdata.com', | |
| 'testing-core' : 'https://folio-testing-okapi.aws.indexdata.com', | |
| } | |
| def parse_command_line_args(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('-u', '--username', help='username', default='diku_admin', | |
| required=False) | |
| parser.add_argument('-p', '--password', help='password', default='admin', | |
| required=False) | |
| parser.add_argument('-t', '--tenant', help='tenant', default='diku', | |
| required=False) | |
| parser.add_argument('-o', '--okapi-url', help='okapi url', | |
| default='http://localhost:9130', required=False) | |
| args = parser.parse_args() | |
| if args.okapi_url in REFERNCE_ENVS_SHORTCUTS.keys(): | |
| args.okapi_url = REFERNCE_ENVS_SHORTCUTS[args.okapi_url] | |
| return args | |
| def get_token(username, password, tenant, okapi_url): | |
| headers = { | |
| 'X-Okapi-Tenant':tenant, | |
| 'Accept': 'application/json', | |
| 'Content-Type': 'application/json' | |
| } | |
| payload = json.dumps({ | |
| 'username':username, | |
| 'password': password | |
| }).encode('UTF-8') | |
| url = okapi_url + '/authn/login' | |
| req = urllib.request.Request(url, data=payload, headers=headers) | |
| try: | |
| resp = urllib.request.urlopen(req) | |
| response_data = dict(resp.info()) | |
| except urllib.error.HTTPError as e: | |
| sys.exit(' - '.join([ | |
| 'ERROR', 'POST', e.url, | |
| str(e.status), str(e.read().decode('utf-8')) | |
| ])) | |
| return response_data | |
| def main(): | |
| args = parse_command_line_args() | |
| response_data = get_token(args.username, args.password, args.tenant, args.okapi_url) | |
| print(response_data['x-okapi-token']) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment