1

第一个问题:

我无法通过 Graph API 在 Microsoft Planner 中创建存储桶。即使我已经指定了正确的计划 ID,我仍然收到以下错误:


    {"error": {"code": "", "message": "Schema validation has failed. Validation for field 'PlanId', on entity 'Bucket' has failed: A non-null value must be specified for this field.", "innerError": {"date": "2021-04-13T08:21:23", "request-id": "7f73320f-c273-4c8f-aedf-e4c413343d99", "client-request-id": "7f73320f-c273-4c8f-aedf-e4c413343d99"}}}

第二个问题:

我无法通过 Graph API 更新 Microsoft Planner 中的任务。我收到 200 响应状态代码,但 Planner 上的实际任务实际上并未反映更改。

import requests_oauthlib
import config
import os
import json

MSGRAPH = requests_oauthlib.OAuth2Session(config.CLIENT_ID,
                                          scope=config.SCOPES,
                                          redirect_uri=config.REDIRECT_URI)

# Enable non-HTTPS redirect URI for development/testing.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
# Allow token scope to not match requested scope. (Other auth libraries allow
# this, but Requests-OAuthlib raises exception on scope mismatch by default.)
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
os.environ['OAUTHLIB_IGNORE_SCOPE_CHANGE'] = '1'

def call_endpoint(do, **kwargs):
    BASE_URL = "https://graph.microsoft.com"
    RESOURCE = "v1.0/planner"
    headers = {'Authorization': f'Bearer {config.ACCESS_TOKEN}'}
    
    ENDPOINTS = {
        "Create Plan": {"URL": f"{BASE_URL}/{RESOURCE}/plans", "data": {'owner': config.GROUP_ID, 'title': kwargs.get('title', "")}},
        "Create Bucket": {"URL": f"{BASE_URL}/{RESOURCE}/buckets", "data": {"planId": kwargs.get("planId", ""), "name": kwargs.get("name", ""), 'orderHint': " !"}},
        "Create Task": {"URL": f"{BASE_URL}/{RESOURCE}/tasks", "data": {'planId': kwargs.get('planId', ""), 'bucketId': kwargs.get('bucketId', ""), 'title': kwargs.get('title', ""), 'assignments': {}}},
        "Get Plans": {"URL": f"{BASE_URL}/v1.0/groups/{config.GROUP_ID}/planner/plans", "data": {}},
        "Get Buckets": {"URL": f"{BASE_URL}/v1.0/planner/plans/{kwargs.get('planId', '')}/buckets", 'data': {}},
        "Get Tasks": {"URL": f"{BASE_URL}/{RESOURCE}/plans/{kwargs.get('planId', '')}/tasks", "data": {}},
        "Get Task": {'URL': f"{BASE_URL}/{RESOURCE}/tasks/{kwargs.get('taskId', '')}/details", "data": {}},
        "Get Bucket": {"URL": f"{BASE_URL}/{RESOURCE}/buckets/{kwargs.get('bucketId', '')}", "data": {}},
        "Update Task": {'URL': f"{BASE_URL}/{RESOURCE}/tasks/{kwargs.get('taskId', '')}/details", "data": {'title': kwargs.get('title', ''), 'description': kwargs.get('description', ''), 'previewType': 'description'}},
    }
    
    endpoint = ENDPOINTS[do]['URL']
    data = ENDPOINTS[do]['data']

    print(f'{do} at {endpoint} with {data}')

    if 'Create' in do:
        response = MSGRAPH.post(endpoint, headers=headers, data=data).json()
    
    elif 'Get' in do:
        response = MSGRAPH.get(endpoint, headers=headers).json()

    elif 'Update' in do:
        response = MSGRAPH.get(ENDPOINTS['Get Task']['URL'], headers=headers, data=ENDPOINTS['Get Task']['data']).json()
        print(response)
        eTag = response['@odata.etag']
        headers['If-Match'] = eTag
        headers['Prefer'] = "return=representation"
        response = MSGRAPH.patch(endpoint, headers=headers, data=data)
        print(response)
        response = MSGRAPH.get(ENDPOINTS['Get Task']['URL'], headers=headers, data=ENDPOINTS['Get Task']['data']).json()

    if 'error' not in response:
        print(f"Successful API call: {do}")

    else:
        print(f"API call {do} failed due to {response['error']}")

    return response

response = call_endpoint("Create Bucket", name='wtf', planId='qHfOXBxd5UGoPCaiNOKWBskAE2rj')
response = call_endpoint("Update Task", taskId='IEUy7GJAd0WNTcfLhqXbKMkAIvYH', title="title created by API", description="description created by API")

任何建议将不胜感激。

4

1 回答 1

0
  • 只是为了隔离问题,我们尝试了相同的 API 调用及其与 Microsoft Graph Explorer 的配合。
  • 您发现您需要 json.dumps(data) 并将 "Content-Type": "application/json" 添加到标题中才能正常工作。
于 2021-04-16T17:26:03.483 回答