Я использую следующий код:
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':'abc@company.com', #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()
Но я получаю ошибку ниже: b '{"token_type": "Bearer", "scope": "offline_access openid profile User.Read", "expires_in": "3599", "ext_expires_in": "3599", "expires_on": "1580421349", "not_before": "1580417449", "ресурс": "https://graph.microsoft.com", "access_token ":" eyJ0eXAiOiJKV1QiLCJub25jZSI6Il {'error': {'code': 'Authorization_RequestDenied', 'message': 'Недостаточно прав для завершения операции.', 'innerError': {'request-id': '4fc27125-8960-44e6- 9510-e3cfca8bce7f ',' date ':' 2020-01-30T20: 55: 50 '}}} Traceback (последний вызов был последним): файл "sNowDelete.py", строка 35, для пользователя в user_response_data [' value ' ]: KeyError: 'value'
Пожалуйста, помогите.