我知道这里有人问过这个问题,但我没有找到任何适用于我的解决方案。
我想构建一个将事件发送到学生日历的 Python 脚本,但我没有在学校目录中的 Azure 中注册应用程序的权限,因此我什至无法在 API 中设置权限。
我也尝试过ROPC协议,但同样不行。
有人找到了解决此问题的方法或其他方法来绕过它吗? 那么当我使用学校帐户时,其他组织如何能够与 Office 交互呢?
注意:要与 Microsoft Graph API 通信并通过 Python 脚本向用户发送事件,您需要 Microsoft Entra ID 应用程序。请参阅 Sajeetharan 的 SO 主题。
创建应用程序并授予以下API权限:
使用以下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())
或者检查此 PowerShell 脚本为此创建事件,您必须拥有足够的权限。使用
Connect-MgGraph -scopes Calendars.ReadWrite
并尝试。
参考: