I try to create an azure ad b2c user flow using microsoft graph API :
user_flow.json
{
"id": "Customer",
"userFlowType": "signUpOrSignIn",
"userFlowTypeVersion": 3
}
main.py
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' msgraph_token
}
with open(jsonConfigFile) as data :
payload = (json.load(data))
data.close()
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
{ "error": { "code": "BadRequest", "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.", "innerError": { "date": "2022-04-13T13:09:00", "request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "client-request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" } } }
But if I hardcode the json
as follow, it works :
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' msgraph_token
}
payload = json.dumps({
"id": "Customer",
"userFlowType": "signUpOrSignIn",
"userFlowTypeVersion": 3
})
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
Could someone explain me what I am doing wrong while loading my json from a file ?
CodePudding user response:
json.load
creates a new dictionary with the key-value pairs of the JSON string and it returns this new dictionary.
You can read the data as a string and send it in the request
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' msgraph_token
}
with open(jsonConfigFile) as data :
payload = data.read()
data.close()
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
or call json.dumps for load JSON dictionary.
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' msgraph_token
}
with open(jsonConfigFile) as data :
payload = (json.load(data))
data.close()
r = requests.request("POST", url, headers=headers, data=json.dumps(payload))
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())