python - Microsoft Graph API REQUESTS with Python:进行简单调用时出现“权限不足,无法完成操作”错误
问题描述
在 Python 中使用 REQUESTS 执行基本 Graph API POST 时收到以下错误响应:
{
"error": {
"code": "Authorization_RequestDenied",
"message": "Insufficient privileges to complete the operation.",
"innerError": {
"request-id": "36c01b2f-5c5c-438a-bd10-b3ebbc1a17c9",
"date": "2019-04-05T22:39:37"
}
}
}
这是我在 Python 中使用 REQUESTS 的令牌请求和图形请求:
redirect_uri = "https://smartusys.sharepoint.com"
client_id = 'd259015e-****-4e99-****-aaad67057124'
client_secret = '********'
tennant_id = '15792366-ddf0-****-97cb-****'
scope = 'https://graph.microsoft.com/.default'
####GET A TOKEN
payload = "client_id="+client_id+"&scope="+scope+"&client_secret="+client_secret+"&grant_type=client_credentials"
headers = {'content-type':'application/x-www-form-urlencoded'}
tokenResponse = requests.post('https://login.microsoftonline.com/'+tennant_id+'/oauth2/v2.0/token',headers=headers, data=payload)
json_tokenObject = json.loads(tokenResponse.text)
authToken = json_tokenObject['access_token']
#### Make a call to the graph API
graphResponse = requests.get('https://graph.microsoft.com/v1.0/me/',headers={'Authorization':'Bearer '+authToken})
if tokenResponse.status_code != 200:
print('Error code: ' +graphResponse.status_code)
print(graphResponse.text)
exit()
print('Request successfull: Response: ')
print(graphResponse.text)
print('Press any key to continue...')
x=input()
根据此 /me 调用的文档(https://docs.microsoft.com/en-us/graph/api/resources/users?view=graph-rest-1.0),我只需要以下权限之一:
- User.ReadBasic.All
- 用户读
- 用户读写
- 用户阅读全部
- User.ReadWrite.All
- 目录.阅读.全部
- Directory.ReadWrite.All
- Directory.AccessAsUser.All
我在 azure 应用程序管理器中的应用程序和委派权限上都拥有所有这些权限。
我在这里做错了什么?我觉得这是一件小事,但我就是想不通。
我使用http://calebb.net/解码了我的令牌,但我没有看到“AUD”或“角色”或“范围”的位置,所以也许这就是我做错的地方?
我到处找,找不到解决方案,非常感谢任何帮助。
谢谢你。
解决方案
这听起来像您忘记“授予权限”到您的应用程序。
看到这个答案。
推荐阅读
- forms - 提交密码的正确方法
- java - 使用 MapStruct 列出两个不同 bean 的映射
- python - 难以模糊python图像
- matlab - 如何让Matlab自动按顺序输入文件?
- amazon-web-services - Sagemaker Studio 每次连接“打开工作室”时都会错误加载屏幕以清除工作区
- multithreading - 如何测试该方法等待异步调用完成
- reactjs - 使用useEffect在react中设置本地存储后如何在渲染中将数据填充到购物车中
- javascript - Ionic 中的多个构造函数打字稿:怎么做?
- go - 使用 Go 语法的函数组合
- image - React Native:模态屏幕未通过点击关闭