Forum Discussion
bojanvelevski
2 years agoValued Contributor
Hey aditya_gupta41,
Can you share more details about the authentication type, or maybe the chunk of code that generates the key?
Bojan
aditya_gupta41
2 years agoContributor
bojanvelevski Below is the python files shared to us:
import sys
import json
import msal
from cryptography import x509
from cryptography.hazmat.primitives.serialization import load_pem_private_key
def print_result(result):
if result['access_token']:
print(result['access_token'])
else:
print(result['error'])
print(result['error_description'])
def read_file(file_path):
return open(file_path, "rb").read()
# configuration file is passed via:
# py Azure\ Certificate\ Authentication.py Azure\ Certificate\ Authentication.json
config = json.load(open(sys.argv[1]))
# get private key from pfx file
private_key_path = config["private_key_path"]
certificate_path = config["certificate_path"]
private_key = load_pem_private_key(read_file(private_key_path), None)
certificate = x509.load_pem_x509_certificate(read_file(certificate_path))
# create application for access token request
app = msal.ConfidentialClientApplication(
config["client_id"],
authority=config["authority"],
client_credential={
"thumbprint": config["thumbprint"],
"private_key": private_key}
)
result = None
# request access token from cache
print("Request access token from cache.")
result = app.acquire_token_silent(config["scope"], account=None)
# request access token from aad
if not result:
print("No access token in cache exists.\nRequest access token from AAD.")
result = app.acquire_token_for_client(scopes=config["scope"])
print_result(result)