Office365 使用Office 365 Mail REST服务从后台守护程序服务中删除电子邮件

Office365 使用Office 365 Mail REST服务从后台守护程序服务中删除电子邮件,office365,exchangewebservices,azure-active-directory,office365api,office365-restapi,Office365,Exchangewebservices,Azure Active Directory,Office365api,Office365 Restapi,我正在构建一个访问Office 365邮箱的后台服务,以便在我们的应用程序中检索和处理电子邮件。处理完成后,应删除电子邮件 我已使用X509证书成功设置了web服务的身份验证,以获取OAuth访问令牌(多亏了这一点),并可以通过HTTP get读取用户的电子邮件: (“[emailAddressHere]”)/邮件?$top=50 这将返回消息传递对象所需的JSON响应 我现在希望在邮件处理完毕后删除邮件,为此我尝试了两种方法: HTTP DELETE: https://outlook.of

我正在构建一个访问Office 365邮箱的后台服务,以便在我们的应用程序中检索和处理电子邮件。处理完成后,应删除电子邮件

我已使用X509证书成功设置了web服务的身份验证,以获取OAuth访问令牌(多亏了这一点),并可以通过HTTP get读取用户的电子邮件:

  • (“[emailAddressHere]”)/邮件?$top=50
这将返回消息传递对象所需的JSON响应

我现在希望在邮件处理完毕后删除邮件,为此我尝试了两种方法:

HTTP DELETE: https://outlook.office365.com/api/v2.0/users('[emailAddress]')/messages/[messageId]
以及:

包括:

_request.Content = New StringContent("{""DestinationId"": ""DeletedItems""}", Encoding.utf8, "application/json")
我最初在Azure AD中设置了我的应用程序,该应用程序对其他应用程序(包括Office 365 Exchange Online)具有访问权限,并且具有仅阅读邮件的应用程序权限和委派权限(此处不应适用)。此时,从http get请求读取邮件工作正常

然后,我尝试向后台服务添加一个新方法,在消息完成后删除它们。每当我运行删除或移动到已删除的项目时(如上所述),我都会收到一条403,其中包含以下消息:

x-ms-diagnostics: 2000008;reason="The token contains not enough scope to make this call.";error_category="invalid_grant"
我返回并向应用程序和委托添加了读/写权限,但在发送请求时没有任何更改

作为一个实验,我随后从Azure AD中的应用程序中删除了Office 365的所有权限。正如预期的那样,这会阻止我检索以前工作过的电子邮件。这证明我正在成功地连接到我的 应用程序

但是,我随后重新添加了权限,现在对于所有请求,我都会收到401,其中包括:

  x-ms-diagnostics: 2000008;reason="The token contains no permissions, or permissions can not be understood.";error_category="invalid_grant"
此后,我尝试在Azure AD中从应用程序中删除Office 365并重新添加它,但无法解决该问题

添加/更新权限是否有延迟

我如何修复应用程序,使其能够检索、删除和/或移动电子邮件

我在删除问题时的完整回复是:

StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  request-id: 9734281b-60f6-48e0-bea4-a3ce0c8b9744
  client-request-id: 1f7ee398-340e-45c4-985a-e89a34d3a4d9
  client-request-id: 1f7ee398-340e-45c4-985a-e89a34d3a4d9
  X-CalculatedBETarget: HE1PR04MB0971.eurprd04.prod.outlook.com
  X-BackEndHttpStatus: 403
  Cache-Control: private
  Set-Cookie: ClientId=XFUHAPKC3UISCO9J0WCOG; expires=Thu, 12-Jan-2017 13:30:18 GMT; path=/; secure; HttpOnly
  Set-Cookie: exchangecookie=ed0bfaa0c16e44ffac392df7da2dacd3; expires=Fri, 13-Jan-2017 13:30:18 GMT; path=/; HttpOnly
  Set-Cookie: ClientId=XFUHAPKC3UISCO9J0WCOG; expires=Thu, 12-Jan-2017 13:30:18 GMT; path=/; secure; HttpOnly
  Server: Microsoft-IIS/8.0
  WWW-Authenticate: Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trusted_issuers="00000001-0000-0000-c000-000000000000@*", token_types="app_asserted_user_v1 service_asserted_app_v1", error="invalid_token"
  x-ms-diagnostics: 2000008;reason="The token contains not enough scope to make this call.";error_category="invalid_grant"
  OData-Version: 4.0
  X-AspNet-Version: 4.0.30319
  X-DiagInfo: HE1PR04MB0971
  X-BEServer: HE1PR04MB0971
  X-Powered-By: ASP.NET
  Date: Wed, 13 Jan 2016 13:30:18 GMT
  Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; IEEE754Compatible=false; charset=utf-8
}
对当前权限问题的完整响应是:

StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  request-id: 9734281b-60f6-48e0-bea4-a3ce0c8b9744
  client-request-id: 1f7ee398-340e-45c4-985a-e89a34d3a4d9
  client-request-id: 1f7ee398-340e-45c4-985a-e89a34d3a4d9
  X-CalculatedBETarget: HE1PR04MB0971.eurprd04.prod.outlook.com
  X-BackEndHttpStatus: 403
  Cache-Control: private
  Set-Cookie: ClientId=XFUHAPKC3UISCO9J0WCOG; expires=Thu, 12-Jan-2017 13:30:18 GMT; path=/; secure; HttpOnly
  Set-Cookie: exchangecookie=ed0bfaa0c16e44ffac392df7da2dacd3; expires=Fri, 13-Jan-2017 13:30:18 GMT; path=/; HttpOnly
  Set-Cookie: ClientId=XFUHAPKC3UISCO9J0WCOG; expires=Thu, 12-Jan-2017 13:30:18 GMT; path=/; secure; HttpOnly
  Server: Microsoft-IIS/8.0
  WWW-Authenticate: Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trusted_issuers="00000001-0000-0000-c000-000000000000@*", token_types="app_asserted_user_v1 service_asserted_app_v1", error="invalid_token"
  x-ms-diagnostics: 2000008;reason="The token contains not enough scope to make this call.";error_category="invalid_grant"
  OData-Version: 4.0
  X-AspNet-Version: 4.0.30319
  X-DiagInfo: HE1PR04MB0971
  X-BEServer: HE1PR04MB0971
  X-Powered-By: ASP.NET
  Date: Wed, 13 Jan 2016 13:30:18 GMT
  Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; IEEE754Compatible=false; charset=utf-8
}

由于您修改了权限,管理员必须登录并授予他们对新权限的同意。在此之前,您将继续获得具有“旧”权限集的令牌。因此,基本上你需要做的是重新做博客文章的“授予同意…”部分。

是的,这很有效,谢谢!对于其他阅读本文的人,我必须重新下载清单文件,重新添加证书详细信息,并让我的管理员再次上传它。
StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  request-id: 9734281b-60f6-48e0-bea4-a3ce0c8b9744
  client-request-id: 1f7ee398-340e-45c4-985a-e89a34d3a4d9
  client-request-id: 1f7ee398-340e-45c4-985a-e89a34d3a4d9
  X-CalculatedBETarget: HE1PR04MB0971.eurprd04.prod.outlook.com
  X-BackEndHttpStatus: 403
  Cache-Control: private
  Set-Cookie: ClientId=XFUHAPKC3UISCO9J0WCOG; expires=Thu, 12-Jan-2017 13:30:18 GMT; path=/; secure; HttpOnly
  Set-Cookie: exchangecookie=ed0bfaa0c16e44ffac392df7da2dacd3; expires=Fri, 13-Jan-2017 13:30:18 GMT; path=/; HttpOnly
  Set-Cookie: ClientId=XFUHAPKC3UISCO9J0WCOG; expires=Thu, 12-Jan-2017 13:30:18 GMT; path=/; secure; HttpOnly
  Server: Microsoft-IIS/8.0
  WWW-Authenticate: Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trusted_issuers="00000001-0000-0000-c000-000000000000@*", token_types="app_asserted_user_v1 service_asserted_app_v1", error="invalid_token"
  x-ms-diagnostics: 2000008;reason="The token contains not enough scope to make this call.";error_category="invalid_grant"
  OData-Version: 4.0
  X-AspNet-Version: 4.0.30319
  X-DiagInfo: HE1PR04MB0971
  X-BEServer: HE1PR04MB0971
  X-Powered-By: ASP.NET
  Date: Wed, 13 Jan 2016 13:30:18 GMT
  Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; IEEE754Compatible=false; charset=utf-8
}