-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtoken_generation.py
More file actions
33 lines (26 loc) · 905 Bytes
/
token_generation.py
File metadata and controls
33 lines (26 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Step 1 — Token Generation
=========================
Authenticate with your credentials and create a short-lived bearer token
scoped to the API endpoints you need to call.
Usage:
OPENAPI_USERNAME=<user> OPENAPI_APIKEY=<key> python examples/token_generation.py
"""
import os
from openapi_python_sdk.client import OauthClient
username = os.environ.get("OPENAPI_USERNAME", "<your_username>")
apikey = os.environ.get("OPENAPI_APIKEY", "<your_apikey>")
oauth = OauthClient(username=username, apikey=apikey, test=True)
# Create a token valid for 1 hour, scoped to the endpoints you need
resp = oauth.create_token(
scopes=[
"GET:test.imprese.openapi.it/advance",
"POST:test.postontarget.com/fields/country",
],
ttl=3600,
)
token = resp["token"]
print(f"Token created: {token}")
# Revoke the token when you are done
oauth.delete_token(id=token)
print("Token revoked.")