我正在使用 Azure Web 应用页面提供的以下代码示例,它可用于获取有关所有用户的信息:
config = json.load(open('parameters.json'))
# Create a preferably long-lived app instance which maintains a token cache.
app = msal.ConfidentialClientApplication(
config["client_id"], authority=config["authority"],
client_credential=config["secret"])
result = app.acquire_token_silent(config["scope"], account=None)
if "access_token" in result:
# Calling graph using the access token
graph_data = requests.post( # Use token to call downstream service
config["endpoint"],
headers={'Authorization': 'Bearer ' + result['access_token']},
data = request_body).json()
print("Graph API call result: ")
print(json.dumps(graph_data, indent=2))
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id"))
parameters.json 文件的内容如下:(包括客户端 ID 和密钥,因为该文件是在门户网站上创建的)
{
"authority": "https://login.microsoftonline.com/af7826c2-1f19-43d8-891d-6bbcfe3ba828",
"client_id": "<MY CLIENT ID>",
"scope": ["https://graph.microsoft.com/.default"],
"secret": "<MY SECRET>",
"endpoint": "https://graph.microsoft.com/v1.0/planner/tasks"
}
但是运行上面的代码会出现以下错误:
Graph API call result:
{
"error": {
"code": "BadRequest",
"message": "Write requests (excluding DELETE) must contain the Content-Type header declaration.",
"innerError": {
"date": "2020-08-12T14:25:05",
"request-id": "906c4f6b-95b7-4ae2-b2ef-07f33c1a5b42"
}
}
}
但是,添加'Content-type':'application/json'
in 请求标头会产生以下错误。
Graph API call result:
{
"error": {
"code": "UnknownError",
"message": "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>\r\n<title>401 - Unauthorized: Access is denied due to invalid credentials.</title>\r\n<style type=\"text/css\">\r\n<!--\r\nbody{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}\r\nfieldset{padding:0 15px 10px 15px;} \r\nh1{font-size:2.4em;margin:0;color:#FFF;}\r\nh2{font-size:1.7em;margin:0;color:#CC0000;} \r\nh3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} \r\n#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:\"trebuchet MS\", Verdana, sans-serif;color:#FFF;\r\nbackground-color:#555555;}\r\n#content{margin:0 0 0 2%;position:relative;}\r\n.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n-->\r\n</style>\r\n</head>\r\n<body>\r\n<div id=\"header\"><h1>Server Error</h1></div>\r\n<div id=\"content\">\r\n <div class=\"content-container\"><fieldset>\r\n <h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>\r\n <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>\r\n </fieldset></div>\r\n</div>\r\n</body>\r\n</html>\r\n",
"innerError": {
"date": "2020-08-12T14:29:50",
"request-id": "54084565-9008-428c-a892-8f4212ee944a"
}
}
}
不应该在那里,因为我已经允许 Group.Read.All 和 Group.ReadWrite.All 对我的应用程序的权限。创建新任务时在 Microsoft Graph 资源管理器中传递相同的标头和请求正文?