图形REST API-阅读电子邮件(Java)-NoPermissionsInAccessToken

时间:2020-02-25 21:44:02

标签: java microsoft-graph-api microsoft-graph-mail

我追求的目标是我希望从Outlook 365中获得电子邮件,而无需任何人工干预。 因此,我尝试使用MS Graph REST API和Java。 要获取访问令牌,我使用:

resource=https://graph.microsoft.com

response = Unirest.post(
                String.format("https://login.microsoftonline.com/%s/oauth2/token", 
                    auth.getProperty("tenant_id")))
              .header("Content-Type", "application/x-www-form-urlencoded")
              .field("grant_type", "client_credentials")
              .field("resource", auth.getProperty("resource"))
              .field("client_id", auth.getProperty("app_id"))
              .field("client_secret", auth.getProperty("app_secret"))
              .asString();

返回一个字符串,该字符串被解析为JSON,并传递给:

jsonNode = Unirest.get(
        String.format("https://graph.microsoft.com/v1.0/users/%s/mailFolders", 
                prop.getProperty("user_email")))
            .header("Accept", "application/json")
            .header("Authorization", json.get("token_type") + " " + json.get("access_token"))
            .asString();

我收到以下错误消息:“令牌不包含任何权限,或者权限无法理解。”

我没有对Azure AD的管理员访问权限,但是我具有对user_mail收件箱的访问权限,可以通过https://outlook.office365.com/mail/inbox访问该收件箱。

我觉得我切线了,但是不确定我还能做什么。

有人知道要让我通过MS-Graph API接收电子邮件需要做什么吗?还是更好的方法?

感谢我能获得的所有帮助。

1 个答案:

答案 0 :(得分:0)

您需要在租户中的Azure AD中注册一个应用程序,并提供用户需要访问的权限。

在您尝试访问时 v1.0 / users /%s / mailFolders

好像您想使用应用程序权限(非委托权限)。因此,您将需要使用Mail.ReadWrite应用程序权限,这也需要管理员的同意。

您也可以使用本教程来了解如何也使用JAVA连接Microsoft Graph https://docs.microsoft.com/en-us/graph/tutorials/java

相关问题