首页 > 解决方案 > 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),我只需要以下权限之一:

我在 azure 应用程序管理器中的应用程序和委派权限上都拥有所有这些权限。

我在这里做错了什么?我觉得这是一件小事,但我就是想不通。

我使用http://calebb.net/解码了我的令牌,但我没有看到“AUD”或“角色”或“范围”的位置,所以也许这就是我做错的地方?

我到处找,找不到解决方案,非常感谢任何帮助。

谢谢你。

标签: pythonpython-3.xoauth-2.0requestmicrosoft-graph-api

解决方案


这听起来像您忘记“授予权限”到您的应用程序。

看到这个答案。


推荐阅读