0
votes

I am using below code :

import requests
import json
import pandas as pd
import pyodbc

app_id = '9a48d65b-XXXXXXXXX' #Application Id - on the azure app overview page
client_secret = 'loOBJ9=-XXXXXXX' 
#Use the redirect URL to create a token url
token_url = 'https://login.microsoftonline.com/1ab41d5a-XXX-4af0-XXXXX-e3c54d03997b/oauth2/token'
token_data = {
 'grant_type': 'password',
 'client_id': app_id,
 'client_secret': client_secret,
 'resource': 'https://graph.microsoft.com',
 'scope':'https://graph.microsoft.com',
 'username':'[email protected]', #Account with no 2MFA
 'password':'XXXXXXX!',
}
token_r = requests.post(token_url, data=token_data)
token = token_r.json().get('access_token')

print(token_r.content)

users_url = 'https://graph.microsoft.com/v1.0/users?$top=500'

headers = {
 'Authorization': 'Bearer {}'.format(token)
}
user_response_data = json.loads(requests.get(users_url, headers=headers).text)

print(user_response_data)
# user_response_data[‘@odata.nextLink’]
#initial user data
#get all users
for user in user_response_data['value']:
 userId.append(user['id'])
 displayName.append(user['displayName'])
 mailAddress.append(user['userPrincipalName'])

users_dict = {'userId':userId,'displayName':displayName,'mailAddress':mailAddress}
users_df = pd.DataFrame(data=users_dict)
#additional user query for paging
while '@odata.nextLink' in user_response_data:
 user_response_data = json.loads(requests.get(users_url, headers=headers).text)
 if '@odata.nextLink' in user_response_data: 
    users_url = user_response_data['@odata.nextLink']

 for user in user_response_data['value']:
    userId.append(user['id'])
    displayName.append(user['displayName'])
    mailAddress.append(user['userPrincipalName'])

users_dict = {'userId':userId,'displayName':displayName,'mailAddress':mailAddress}
users_df = pd.DataFrame(data=users_dict)
users_df.head()

But,i am getting below error: b'{"token_type":"Bearer","scope":"offline_access openid profile User.Read","expires_in":"3599","ext_expires_in":"3599","expires_on":"1580421349","not_before":"1580417449","resource":"https://graph.microsoft.com","access_token":"eyJ0eXAiOiJKV1QiLCJub25jZSI6Il {'error': {'code': 'Authorization_RequestDenied', 'message': 'Insufficient privileges to complete the operation.', 'innerError': {'request-id': '4fc27125-8960-44e6-9510-e3cfca8bce7f', 'date': '2020-01-30T20:55:50'}}} Traceback (most recent call last): File "sNowDelete.py", line 35, in for user in user_response_data['value']: KeyError: 'value'

Please help.

1
Any update for this issue? - Tony Ju
@TonyJu Nope, Still same error. - AllTech
I just noticed that you have User.Read permission, but no User.Read.All permission in the error message you provided. Have you added User.Read.All permission and granted admin consent? - Tony Ju
If the answer is helpful for you, you can accept it as answer. This can be beneficial to other community members. Thank you. - Tony Ju

1 Answers

1
votes

It seems that you don't have enough privileges. Please go to Azure portal to check your permissions. You should have User.Read.All delegated permission.

enter image description here

Also, this permission needs grant admin consent. You need to click Grant admin consent button and sign in with the admin account to grant admin consent.