azure 如何通过o365和Microsoft Graph API访问共享日历的日历事件?

t1rydlwq  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(170)

通过在Azure中注册应用程序并通过o365调用Microsoft Graph API,我已经成功访问了自己的日历活动。以下是我一直在使用的代码,感谢我在网上找到的一篇文章:

from O365 import Account, MSGraphProtocol

#gets us access to the API
CLIENT_ID = 'my client id'
SECRET_ID = 'my secret id'
credentials = (CLIENT_ID, SECRET_ID)
protocol = MSGraphProtocol()
scopes = ['https://graph.microsoft.com/.default']
account = Account(credentials, protocol=protocol)
if account.authenticate(scopes=scopes):
   print('Authenticated!')

#calls calendar event
schedule = account.schedule()
calendar = schedule.get_default_calendar()

从这里,我可以访问我自己的日历中的事件。
但最终,我需要能够从共享日历访问事件。
根据我找到的o365文档,我尝试将“resource”更改为我想访问的共享日历的电子邮件。

schedule2 = account.schedule(resource = 'name@gmail.com')
calendar2 = schedule2.get_default_calendar()

然后,当我调用get_default_calendar()(即定义calendar 2)函数时,我得到了这个错误:

Client Error: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/name@gmail.com/calendar | Error Message: The requested user 'name@gmail.com' is invalid.

...

    raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), response=response) from None
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/name@gmail.com/calendar | Error Message: The requested user 'name@gmail.com' is invalid.

现在,电子邮件肯定存在,并有自己的outlook帐户。而且,我的API权限包括Calendars. Read. Shared。我猜问题是,这不是获得共享日历的正确方法,但无情的谷歌搜索并没有告诉我其他方法。我也看了this文章,我不知道如何在o365的上下文中使用这些信息。
问题是否出在我插入的资源中?或者是否有其他方法可以访问共享日历?我是一个初学者python用户,所以任何见解都是赞赏的。我觉得这是一个相当简单的任务,但我只是错过了正确的功能。谢谢!

7kqas0il

7kqas0il1#

    • 我在我的环境中尝试,结果出现以下相同错误:**

404 Client Error: Not Found for url:
https://graph.microsoft.com/v1.0/users/name@gmail.com/calendar | Error Message: The requested user 'name@gmail.com' is invalid.

上述错误消息表明Microsoft Graph API中不存在用户**"name@gmail.com"**。
这可能是由于几个原因,如用户帐户被删除,帐户不活动或暂停,或提供了不正确的用户名或电子邮件地址。
检查帐户的许可证,您可以使用相同的代码从共享日历访问事件:

    • 代码:**
from O365 import Account, MSGraphProtocol

#gets us access to the API
CLIENT_ID = 'a525cb22-243f-4a4d-94cd-51515e7d6737'
SECRET_ID = 'mCC8Q~q3V4bGB2KOxOJsyxd8RdryZKHoc1RuHcZJ'
credentials = (CLIENT_ID, SECRET_ID)
protocol = MSGraphProtocol()
scopes = ['https://graph.microsoft.com/.default']
account = Account(credentials, protocol=protocol)
if account.authenticate(scopes=scopes):
        print("Authenticated")
        schedule1 = account.schedule(resource = 'name@gmail.com')
        calendar1= schedule1.get_default_calendar()
        events = calendar1.get_events(include_recurring=False)

您也可以使用此Document通过图形浏览器进行验证。

    • 查询:**
https://graph.microsoft.com/v1.0/users/{userid or userprinciplename}/calendars
    • 输出:**

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('xxxxxxxx')/calendars",
    "value": [
        {
            "id": "AAMkADM1NzUwMTUxLWJlZjQtNDFkYi1iNGRjLWE0ODllYTlkN2YxNABGAAAAAACBV9bxxxxxxxx",
            "name": "Calendar",
            "color": "auto",
            "hexColor": "",
            "isDefaultCalendar": true,
            "changeKey": "s8hj4slJ2Uym2iExxxxxx",
            "canShare": true,
            "canViewPrivateItems": true,
            "canEdit": true,
            "allowedOnlineMeetingProviders": [],
            "defaultOnlineMeetingProvider": "unknown",
            "isTallyingResponses": true,
            "isRemovable": false,
            "owner": {
                "name": "username",
                "address": "username@gmail.com"
            }
        },
        {
            "id": "AAMkADM1NzUwMTUxLWJlZjQtNDFkYi1iNGRjLWE0ODllYTlkN2YxNABGAAAAAACBV9bfxxxxxxxxxxx",
            "name": "United States holidays",
            "color": "auto",
            "hexColor": "",
            "isDefaultCalendar": false,
            "changeKey": "s8hj4slJ2Uym2ixxxxxxxx",
            "canShare": false,
            "canViewPrivateItems": true,
            "canEdit": false,
            "allowedOnlineMeetingProviders": [],
            "defaultOnlineMeetingProvider": "unknown",
            "isTallyingResponses": false,
            "isRemovable": true,
            "owner": {
                "name": "username",
                "address": "username@gmail.com"
            }
        }
     ]
  }

相关问题