首页 > 解决方案 > Microsoft 365 Graph API - 如何读取所有用户的邮箱

问题描述

我有一个 Microsoft 365 管理员用户。我尝试使用 Microsoft 365 图形 API 阅读其他用户的电子邮件。

我根据Microsoft 教程从github采用了以下内容:

import requests

graph_endpoint = 'https://graph.microsoft.com/v1.0{0}'


def make_api_get_call(url, token, parameters=None):
    headers = {
        "Authorization": f'Bearer {token}',
        "Accept": "application/json",
    }
    return requests.get(url, headers=headers, params=parameters, timeout=10)


def get_messages(access_token, user, results_to_return="10"):
    get_messages_url = graph_endpoint.format(
        f'/users/{user}/mailfolders/inbox/messages')

    query_parameters = {
        "$top": results_to_return,
        "$select": "receivedDateTime,subject,from,body",
        "$orderby": "receivedDateTime DESC",
    }

    return make_api_get_call(get_messages_url, access_token, parameters=query_parameters)

我能够阅读自己的电子邮件,但由于某种原因,当我尝试将变量用户设置为另一个电子邮件帐户时 - 我收到以下错误:

404: {"error":{"code":"ErrorItemNotFound","message":"在商店中找不到指定的对象。,进程未能获得正确的属性。","innerError":{"date ":"2021-09-13T04:33:48","re​​quest-id":"42bba137-a852-43e2-97e7-387a95892223","client-request-id":"42bba137-a852-43e2-97e7-387a95892223 "}}}

我需要做什么才能使其工作?谢谢。

在此处输入图像描述/rareB.png

标签: pythonapimicrosoft-graph-apioffice365microsoft-graph-mail

解决方案


我有一个 Microsoft 365 管理员用户。我尝试使用 Microsoft 365 图形 API 阅读其他用户的电子邮件。

管理员用户没有读取其他人邮箱的权限,例如,如果您尝试在 OWA 或 Outlook 中执行此操作,您会遇到同样的问题。您需要使用 Add-MailboxPermission https://docs.microsoft.com/en-us/powershell/module/exchange/add-mailboxpermission?view=exchange-ps之类的东西明确委派对邮箱的访问


推荐阅读