I resolved this issue by correcting the way my scopes were declared in my code. Although my scopes were declared properly in Microsoft Azure, I did not correctly refer to them in my code. Having one scope as scopes = ['myscope']
seems to work fine, but more than one scope cannot be declared by name. For example `scopes = ['myscope1', 'myscope2'] will not work.
In order to access a user's profile with all scopes which are declared in Microsoft Azure, use scopes = ['https://graph.microsoft.com/.default']
(a list is required). https://graph.microsoft.com/.default
refers to all scopes set for the program in Microsoft Azure, and the user will be prompted to agree to all scopes when logging in.
Here is my log-in flow code for Python 3.12:
import webbrowser
import msal
from msal import PublicClientApplication
from msgraph import GraphServiceClient
from msgraph.generated.users.item.messages.messages_request_builder import MessagesRequestBuilder
from kiota_abstractions.base_request_configuration import RequestConfiguration
applicationID = '_put_yours_here_'
authorityURL = 'https://login.microsoftonline.com/consumers/'
OUTLOOK_BASEURL = 'https://graph.microsoft.com/v1.0/'
scopes = ['https://graph.microsoft.com/.default'] #Sets scopes to all scopes which are set in Microsoft Azure backend
thisApp = PublicClientApplication(applicationID, authority = authorityURL)
logInFlow = thisApp.initiate_device_flow(scopes = scopes)
print(logInFlow['user_code']) #User must type this into the Outlook login page
webbrowser.open(logInFlow['verification_uri'])
userToken = thisApp.acquire_token_by_device_flow(logInFlow)['access_token']
headers = {'Authorization': 'Bearer ' + userToken}
OUTLOOK_HEADERS = headers
OUTLOOK_SERVICE = GraphServiceClient(headers, scopes)
response = requests.get(OUTLOOK_BASEURL + '/me', headers = OUTLOOK_HEADERS)
OUTLOOK_BASEURL = OUTLOOK_BASEURL + '/me/'