Skip to content

Commit 64e5e48

Browse files
authored
Add scripts to create account-wide API keys
Create create_account_api_key.py
2 parents 4edceef + d0c826a commit 64e5e48

2 files changed

Lines changed: 76 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+
Create 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 = "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+
token = response.json()['token']
20+
21+
headers = {
22+
'Authorization': "JWT {}".format(token),
23+
'Content-Type': "application/json",
24+
}
25+
26+
api_name = input("API key's name: ")
27+
28+
response = requests.post(
29+
keys_endpoint,
30+
headers=headers,
31+
json={'name': api_name})
32+
33+
if response.status_code == 201:
34+
print("\nAccount API key:", response.json()['key'])
35+
print("\nPlease record this key, it will not be shown again.")
36+
37+
else:
38+
print('\n[%s]\n%s' %(response.status_code, response.text))
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+
Create 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 = "https://api.probely.com"
13+
auth_endpoint = urljoin(api_base_url, "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+
token = response.json()['token']
20+
21+
headers = {
22+
'Authorization': "JWT {}".format(token),
23+
'Content-Type': "application/json",
24+
}
25+
26+
api_name = input("API key's name: ")
27+
28+
response = requests.post(
29+
keys_endpoint,
30+
headers=headers,
31+
json={'name': api_name})
32+
33+
if response.status_code == 201:
34+
print("\nAccount API key:", response.json()['key'])
35+
print("\nPlease record this key, it will not be shown again.")
36+
37+
else:
38+
print('\n[%s]\n%s' %(response.status_code, response.text))

0 commit comments

Comments
 (0)