|
1 | 1 | from pyDataverse.api import Api |
2 | 2 | import json |
3 | 3 | import dvconfig |
| 4 | +from pathlib import Path |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
4 | 8 | base_url = dvconfig.base_url |
5 | | -api_token = dvconfig.api_token |
6 | | -api = Api(base_url, api_token) |
7 | | -username = 'dataverseAdmin' |
8 | | -password = 'admin1' |
| 9 | +api = Api(base_url, '') |
| 10 | + |
| 11 | +username = os.getenv('DATAVERSE_USER', 'dataverseAdmin') |
| 12 | +password = os.getenv('DATAVERSE_PASSWORD', 'admin1') |
| 13 | +# On K8s or with Docker we should get secrets from files, not env vars |
| 14 | +if Path(password).is_file(): |
| 15 | + f = open(Path(password), 'r') |
| 16 | + password = f.read().strip() |
| 17 | + f.close() |
| 18 | + |
9 | 19 | endpoint = '/builtin-users/' + username + '/api-token' |
10 | 20 | params = {} |
11 | 21 | params['password'] = password |
12 | | -resp = api.get_request(endpoint, params=params, auth=True) |
13 | | -api_token = resp.json()['data']['message'] |
14 | | -print(api_token) |
| 22 | + |
| 23 | +resp = api.get_request(endpoint, params=params, auth=False) |
| 24 | +if resp.json()['status'] == "OK": |
| 25 | + api_token = resp.json()['data']['message'] |
| 26 | + print(api_token) |
| 27 | + sys.exit(0) |
| 28 | +else: |
| 29 | + print("ERROR receiving API token:", file=sys.stderr) |
| 30 | + print(resp.json(), file=sys.stderr) |
| 31 | + print("Did you enable :AllowApiTokenLookupViaApi configuration option?", file=sys.stderr) |
| 32 | + print("See http://guides.dataverse.org/en/latest/installation/config.html#allowapitokenlookupviaapi", file=sys.stderr) |
| 33 | + sys.exit(1) |
0 commit comments