forked from Neoteroi/BlackSheep-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_http_example.py
42 lines (34 loc) · 1.28 KB
/
client_http_example.py
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
34
35
36
37
38
39
40
41
42
"""
This module shows an example of how the client credentials flow with secret can be used
directly using an HTTP Client, for Azure Active Directory.
This should be used only in those scenario when legacy applications would not support
MSAL for Python.
"""
import os
import httpx
from dotenv import load_dotenv
# read .env file into environment variables
load_dotenv()
def ensure_success(response: httpx.Response) -> None:
if response.status_code < 200 or response.status_code > 399:
body = response.text
raise ValueError(
f"The response status does not indicate success {response.status_code}; "
f"Response body: {body}"
)
def get_access_token() -> str:
response = httpx.post(
os.environ["AAD_AUTHORITY"].rstrip("/") + "/oauth2/v2.0/token",
data={
"grant_type": "client_credentials",
"client_id": os.environ["APP_CLIENT_ID"],
"client_secret": os.environ["APP_CLIENT_SECRET"],
"scope": os.environ["APP_CLIENT_SCOPE"],
},
)
ensure_success(response)
data = response.json()
assert "access_token" in data, "The response body must include an access token"
return data["access_token"]
if __name__ == "__main__":
print("Access token: " + get_access_token())