azure 使用Microsoft图形API添加事件时出错

soat7uwm  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

我试图在Python中使用Microsoft图形API添加日历事件。但是我得到了

{'error': {'code': 'ErrorInvalidUser', 'message': "The requested user '[email protected]' is invalid."}}

字符串
下面是我使用的代码:

def get_access_token(tenant_id, client_id, client_secret):
    url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded",
    }
    data = {
        "grant_type": "client_credentials",
        "client_id": client_id,
        "client_secret": client_secret,
        "scope": "https://graph.microsoft.com/.default",
    }
    response = requests.post(url, headers=headers, data=data)
    access_token = response.json().get("access_token")
    return access_token

def create_event(access_token, user_id):
    url = f"https://graph.microsoft.com/v1.0/users/{user_id}/events"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json",
    }
    data = {
        "subject": f"{event_date['Event']}",
        "start": {
            "dateTime": f"{event_date['Date']}",
            "timeZone": "Indian Standard Time",
         },
        "end": {
            "dateTime": f"{event_date['Date']}",
            "timeZone": "Indian Standard Time",
        },
    }
    response = requests.post(url, headers=headers, json=data)
    print(response.json())


我已在Azure Entra ID(Active Directory)中注册了我的应用程序。允许以下API权限:


的数据
我的帐户在Azure Active Directory中注册为Guest用户类型。我还将应用程序分配给此用户ID。
有什么我遗漏的吗?请告诉我。谢谢

ulmd4ohb

ulmd4ohb1#

您使用的客户端凭据流需要应用程序权限,而不是委派权限。
尝试添加应用权限Calendars.ReadWrite,可以删除委托权限。
此外,请检查此article。可能有一个公司策略阻止访问某些(或所有)邮箱,并要求创建一个新的应用程序管理策略,以便能够访问/修改这些邮箱。

相关问题