【发布时间】:2020-01-30 21:04:02
【问题描述】:
我正在使用以下代码:
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","resource":"https://graph.microsoft.com","access_token":"eyJ0eXAiOiJKV1QiLCJub25jZSI6Il {'error': {'code': 'Authorization_RequestDenied', 'message': '权限不足,无法完成操作。', 'innerError': {'request-id': '4fc27125-8960-44e6-9510-e3cfca8bce7f' , '日期': '2020-01-30T20:55:50'}}} 回溯(最近一次通话最后): 文件“sNowDelete.py”,第 35 行,在 对于 user_response_data['value'] 中的用户: KeyError:'值'
请帮忙。
【问题讨论】:
-
这个问题有什么更新吗?
-
@TonyJu 不,还是同样的错误。
-
我刚刚注意到您提供的错误消息中有
User.Read权限,但没有User.Read.All权限。您是否添加了 User.Read.All 权限并授予管理员同意? -
如果答案对您有帮助,您可以接受它作为答案。这对其他社区成员可能是有益的。谢谢。
标签: python azure-active-directory