2

I'm trying to automate inviting users to an Azure AD using the MS Graph API but get an 'Unable to read JSON request payload' error.

I'm pulling data from a ticketing system, retrieving the current AAD users and diff-ing both. Then I'll be pushing the new ones into the AAD and updating them to include them in an Attendees AD Security group.

I created a Python Azure Function that calls the Graph API with Requests :

def insert_users(users_emails):

    logging.info('Inserting new users in AAD')

    token                       =   generate_auth_token()

    users_emails    =   users_emails[:2]
    added_attendees =   []

    for email in users_emails:

        req_body        =   {
                                    "invitedUserEmailAddress"       :   email
                                ,   "inviteRedirectUrl"             :   "https://myapp.com"
                            }

        body_length     =   sys.getsizeof(req_body)

        req_headers     =   {
                                    'Authorization'     :   'Bearer {0}'.format(token)
                                ,   'Content-Type'      :   'application/json; charset=utf-8'
                                ,   'Content-Length'    :   str(body_length)
                            }

        response    =   requests.post(
                                'https://graph.microsoft.com/v1.0/invitations'
                            ,   headers =   req_headers
                            ,   data    =   req_body
                        )

        response    =   response.json()

        logging.info(response)

        added_attendees.append(email)

        return added_attendees

The Graph API sends back the following error message :

{'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': 
                    {'request-id': '4ff5332d-d280-4b0d-9e04-a7359ab0e2fb', 'date': '2020-05-27T14:51:18'}
    }
}

I tried adding the charset to the Content-Type header but it won't work. I read someplace the Content-Length could be useful so I added it too, to no avail.

Tests run ok in Postman and I'm already performing a POST request against the Azure AD API to get an Access Token so the Requests JSON body is parsed fine then. I also tried using single or double quotes in the JSON payload but it didn't work either.

My take is something is misinterpreted by the Graph API but I can't figure out what.

Thanks forward for your help !

1 Answer 1

2

i found a solution. Instead of passing a data argument to the request.post method, I passed a json= argument

    response    =   requests.post(
                            'https://graph.microsoft.com/v1.0/invitations'
                        ,   json={'invitedUserEmailAddress':email,'inviteRedirectUrl':'https://myapp.com'}
                        ,   headers =   req_headers
                    )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sharing this, I got the same error for different situation and was able to solve it after reading your post and other similar posts. My issue was with posting emojis/special character in Microsoft Teams and was able to resolve it by switching the library. I posted my solution here in case someone ran to the same issue. stackoverflow.com/questions/56731805/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.