We want to retrieve only Personnalise(custom) user attributes using Python.
So far succès in getting token but failed at making requests to graph.
#code de chatgpt
import msal
# Replace with your Azure AD B2C configuration
tenant_id = 'your-tenant-id'
client_id = 'your-client-id'
client_secret = 'your-client-secret'
authority = f'https://login.microsoftonline.com/{tenant_id}'
# Replace with the custom attributes you want to retrieve
custom_attributes = ['customAttribute1', 'customAttribute2']
# Create a confidential client application
app = msal.ConfidentialClientApplication(
client_id=client_id,
client_credential=client_secret,
authority=authority
)
# Acquire a token
result = app.acquire_token_for_client(scopes=['https://{your-tenant-name}.onmicrosoft.com/{policy-name}/read'])
access_token = result['access_token']
# Make a request to retrieve user attributes
import requests
user_id = 'user-object-id' # Replace with the object ID of the user
graph_url = f'https://graph.microsoft.com/v1.0/users/{user_id}?$select={",".join(custom_attributes)}'
response = requests.get(graph_url, headers={'Authorization': f'Bearer {access_token}'})
if response.status_code == 200:
user_data = response.json()
print("User attributes:", user_data)
else:
print("Error:", response.status_code, response.text)
Error: 400 {"error":{"code":"BadRequest","message":"Parsing OData Select and Expand failed: Term 'customAttribute1', 'customAttribute2' is not valid in a $select or $expand expression.","innerError":{"date":"2023-08-31T16:41:31","request-id":"c82856a0-e6f8-9939-d7e11e47ddea","client-request-id":"c82856a0-e6f8-9939-d7e11e47ddea"}}}