Skip to content

Commit aec3b08

Browse files
authored
Create create_account_api_key_enterprise.py
1 parent 572e4e7 commit aec3b08

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
"""
3+
Get an API key for your account
4+
"""
5+
import getpass
6+
import requests
7+
from urllib.parse import urljoin
8+
9+
username = input("Username: ")
10+
password = getpass.getpass()
11+
12+
api_base_url = "http://localhost:8060"#"https://api.probely.com"
13+
auth_endpoint = urljoin(api_base_url, "enterprise/auth/obtain/")
14+
keys_endpoint = urljoin(api_base_url, "keys/")
15+
16+
# Get login token
17+
response = requests.post(auth_endpoint,
18+
data={'username': username, 'password': password})
19+
print(response.json())
20+
token = response.json()['token']
21+
22+
headers = {
23+
'Authorization': "JWT {}".format(token),
24+
'Content-Type': "application/json",
25+
}
26+
27+
api_name = input("API key's name: ")
28+
29+
response = requests.post(
30+
keys_endpoint,
31+
headers=headers,
32+
json={'name': api_name})
33+
34+
if response.status_code == 201:
35+
print("\nAccount API key:", response.json()['key'])
36+
37+
else:
38+
print('\n[%s]\n%s' %(response.status_code, response.text))

0 commit comments

Comments
 (0)