Python Forum

Full Version: I need help trying to access an email box using msal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am fairly new to python. I have been working on this for a few weeks. A previous employee used basic authentication to access email on our exchange server. According to Microsoft this is going away October 1st. I am working on using modern authentication, and am having no luck. I have looked through Microsoft's documentation on this, and it did not help. I have searched the web, to no avail. I am able to get an access token, but when I try to access the email box I get a 400 response instead of 200. Below is the code snippet that appears to be the problem, and the code I use to get the access token, incase that helps. The ConfidentialClientApplication actually does have the proper data, and the <user> is the actual username.


app = ConfidentialClientApplication(
        "",
        authority="",
        client_credential=""
        )

result = None
result = app.acquire_token_silent(["https://graph.microsoft.com/.default"], account=None)

if not result:
    result = app.acquire_token_for_client(scopes="https://graph.microsoft.com/.default")

if "access_token" in result:
    print(result["token_type"])
else:
    print(result.get("error"))
    print(result.get("error_description"))
    print(result.get("correlation_id"))  # You might need this when reporting a bug.

print(result["access_token"])
headers = {
    'Authorization': 'Bearer ' + result["access_token"]
}

# Step 2. Retrieve emails

response = requests.get('https://graph.microsoft.com/v1.0/users/<user>/mailFolder/inbox/messages', headers=headers)

print(response.json())
if response.status_code != 200:
    raise Exception(response.json())
The error that I am receiving is below.

'error': {'code': 'Request_BadRequest', 'message': 'Unexpected segment DynamicPathSegment. Expected property/$value.'


I would greatly appreciate any help, or even some sort of clear documentation or examples that you could provide.
Thank you for taking the time to read this.