如何在python中使用Microsoft Graph API创建计划器任务?

问题描述

我正在使用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'会产生以下错误

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"
    }
  }
}

翻译为以下错误

enter image description here

不应该在那儿,因为我已将Group.Read.All和Group.ReadWrite.All的权限授予我的应用程序。创建新任务时,是否在Microsoft Graph资源管理器中传递相同的标头和请求主体?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)