无需在 Azure 中注册应用程序即可与 graphAPI 通信

问题描述 投票:0回答:1

我知道这里有人问过这个问题,但我没有找到任何适用于我的解决方案。

我想构建一个将事件发送到学生日历的 Python 脚本,但我没有在学校目录中的 Azure 中注册应用程序的权限,因此我什至无法在 API 中设置权限。

我也尝试过ROPC协议,但同样不行。

有人找到了解决此问题的方法或其他方法来绕过它吗? 那么当我使用学校帐户时,其他组织如何能够与 Office 交互呢?

azure python-requests
1个回答
0
投票

注意:要与 Microsoft Graph API 通信并通过 Python 脚本向用户发送事件,您需要 Microsoft Entra ID 应用程序。请参阅 Sajeetharan 的 SO 主题

  • Microsoft Graph 是 Microsoft 提供的 RESTful Web API,旨在使开发人员能够与各种 Microsoft 云服务资源进行交互。在 Azure AD 中注册您的应用程序并获取用户或服务的身份验证令牌后。
  • 您可以使用这些令牌向 Microsoft Graph API 发送请求。这允许您以编程方式访问和操作数据。
  • 因此您需要创建一个 Microsoft Entra ID 应用程序。要创建您需要具有应用程序管理员角色。如果没有联系您的管理员。

创建应用程序并授予以下API权限

enter image description here

使用以下Python代码创建事件:

import asyncio
import aiohttp
from azure.identity.aio import ClientSecretCredential

scopes = ['https://graph.microsoft.com/.default']
tenant_id = 'TenantID'
client_id = 'ClientID'
client_secret = 'ClientSecret'

credential = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)

async def create_event():
   
    token = await credential.get_token(*scopes)
    headers = {
        'Authorization': f'Bearer {token.token}',
        'Content-Type': 'application/json'
    }

    request_body = {
        "subject": "Test",
        "body": {
            "contentType": "HTML",
            "content": "Does next month work for you?"
        },
        "start": {
            "dateTime": "2024-07-10T12:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "end": {
            "dateTime": "2024-07-10T14:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "location": {
            "displayName": "Meeting"
        },
        "attendees": [
            {
                "emailAddress": {
                    "address": "[email protected]",
                    "name": "Student"
                },
                "type": "required"
            }
        ],
        "isOnlineMeeting": True,
        "onlineMeetingProvider": "teamsForBusiness"
    }

    async with aiohttp.ClientSession() as session:
        async with session.post('https://graph.microsoft.com/v1.0/users/UserID/calendars/CalendarID/events', headers=headers, json=request_body) as response:
            result = await response.json()
            print(result)

asyncio.run(create_event())

enter image description here

或者检查此 PowerShell 脚本为此创建事件,您必须拥有足够的权限。使用

Connect-MgGraph -scopes Calendars.ReadWrite
并尝试。

参考:

身份验证和授权基础知识 - Microsoft Graph |微软

© www.soinside.com 2019 - 2024. All rights reserved.