Simple question: How do I authenticate with Graph API with Python app?

Admin Team 1 Reputation point
2021-04-09T08:49:22.317+00:00

I just want to be able to use the Planner API. See code below:

from O365 import Account

credentials = (CLIENT_ID, CLIENT_SECRET)

base_url = "https://graph.microsoft.com"

scopes = [
'User.Read.All',
'offline_access',
'openid',
'profile'
]

account = Account(credentials)

if not account.is_authenticated:
account.authenticate(scopes=scopes)

else:
print("Authenticated")

response = requests.get(f"{base_url}/{version}/{query_parameters}")
print(response)

The output I get is Response 401, even though earlier I did manage to print "Authenticated". I've already registered my app in Azure Portal, and have enabled multi-tenant. My client ID and client secret is also obtained from there. Why am I still getting 401 response error?

Microsoft Security | Microsoft Authenticator
Microsoft Security | Microsoft Graph
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.