I used following code to get the MS Graph API access token:
# Resource Owner Password Credentials (ROPC)
def ropc_flow_session(AUTHORITY, RESOURCE, username, password, CLIENT_ID):
context = adal.AuthenticationContext(AUTHORITY)
token_response = context.acquire_token_with_username_password(RESOURCE, username, password, CLIENT_ID)
session = requests.Session()
session.headers.update({'Authorization': f'Bearer {token_response["accessToken"]}'})
return session
The RESOURCE
and AUTHORITY
values are:
RESOURCE = 'https://graph.microsoft.com'
AUTHORITY = F"https://login.microsoftonline.com/{tenant}"
When I call the ropc_flow_session
function it returns the session that contains following header:
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Authorization': 'Bearer {ACCESS_TOKEN_VALUE}'}
So my understanding is I have access to the registered app.
When I try to call me
with https://graph.microsoft.com/v1.0/me
the error message says:l
{'error': {'code': 'Authorization_RequestDenied',
'message': 'Insufficient privileges to complete the operation.',
I tried to have access to a shared library that the user is the owner via https://graph.microsoft.com/v1.0/sites/{TENANT}:/sites/{SITE_NAME}:/drives
and it raised:
{'error': {'code': 'accessDenied',
'message': 'Access denied. You do not have permission to perform this action or access this resource.',
This is the list of registered App API permissions:
Based on the error messages it seems like I have connected as an application but I used ROPC and passed a specific user's username and password. Any thought is appreciated
I already mentioned in the comments but to make it clear for others that have the same issue I bring more description here.
In my case, the problem was the registered Azure app has two owners. One is my own work account and another is the service account. I added the delegated permissions with my work account and it was showing on the API permissions list. But when I checked the Enterprise applications
link, it was showing that all the user consent permissions are limited to only 1 specific user
You could check that by clicking on Enterprise applications
and see the user consent
tab
It has two solutions to resolve the issue:
Thanks @shiva-keshav-varma for the helpful advice on the comments