预信息
我有一个正在运行的 ColdFusion 应用程序(基于网络),并实现了完整的 Google Oauth2 验证流程。这很好用,我可以通过 post 命令检索电子邮件。现在我想对 Microsoft 的 Outlook 执行同样的操作。
<小时/>问题
当我尝试对 token URL 执行 HTTP POST 请求时,我收到一条响应,指出我的重定向 URI 无效,因为它与我用于获取代码的 URL 不同。
<小时/>什么有效
我可以通过向 Oauth 端点发出 GET 请求来检索代码。我在 azure 门户中添加了一个重定向 URI(多个 URI)。当我调用此 URL 时,我会从 Outlook 获得登录屏幕,之后我成功重定向回我的应用程序。所以我猜重定向 URI 是正确的并且一切正常。
当我尝试使用收到的代码获取 token 时,我收到重定向错误。我完全不知道这些 URI 不相同的事实是如何产生的。我已经对所有内容进行了三重检查,但找不到任何问题。
<小时/>错误
这是我收到的错误: "error":"invalid_grant","error_description":"AADSTS70000: 提供的“redirect_uri”值无效。该值必须与用于获取授权代码的重定向 URI 完全匹配。
我尝试过不同类型的格式。末尾有破折号或没有破折号。但事实上,重定向 URL 对于前面的步骤是有效的。那么为什么不选择这个呢?
当我使用它时,我还会收到诸如“代码已过期”之类的消息,所以我知道它工作正常。我已经尝试过 azure 门户中的选项。我关注了 Oauth2 Outlook Playground。但我仍然无法摆脱这个消息。我怎样才能知道这里发生了什么?
请求
我尝试了不同的方法来完成该请求。最简单的是这个:
cfhttp(method="POST", charset="utf-8", url="https://login.microsoftonline.com/common/oauth2/v2.0/token", result="result") {
cfhttpparam(name="Content-Type", type="header", value="application/x-www-form-urlencoded");
cfhttpparam(name="grant_type", type="formfield", value="authorization_code");
cfhttpparam(name="code", type="formfield", value="M090efafb-1ce6-1d54-fda7-e48f57c33cba");
cfhttpparam(name="scope", type="formfield", value="openid offline_access profile https://outlook.office.com/mail.read https://outlook.office.com/mail.read.shared https://outlook.office.com/mail.readwrite https://outlook.office.com/mail.readwrite.shared https://outlook.office.com/mail.send https://outlook.office.com/mail.send.shared");
cfhttpparam(name="redirect_uri", type="formfield", value="#URLencode('https://bizz.bmk-is.nl/')#");
cfhttpparam(name="client_id", type="formfield", value="cdee5a0a-a409-4c41-9572-726c5bdbe93e");
cfhttpparam(name="client_secret", type="formfield", value="[HIDDEN VALUE]");
}
## 新编辑##
我将在这里发布更多信息。首先,这是我正在使用的 get:
response_url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
response_url = "#response_url#?response_type=code";
response_url = "#response_url#&client_id=cdee5a0a-a409-4c41-9572-726c5bdbe93e";
response_url = "#response_url#&redirect_url=#URLencode('https://bizz.bmk-is.nl/')#";
response_url = "#response_url#&scope=openid offline_access profile";
response_url = "#response_url# https://outlook.office.com/mail.read";
response_url = "#response_url# https://outlook.office.com/mail.read.shared";
response_url = "#response_url# https://outlook.office.com/mail.readwrite";
response_url = "#response_url# https://outlook.office.com/mail.readwrite.shared";
response_url = "#response_url# https://outlook.office.com/mail.send";
response_url = "#response_url# https://outlook.office.com/mail.send.shared&prompt=login";
这是我现在遇到的错误: "error":"invalid_request","error_description":"AADSTS90102: 'redirect_uri' 值必须是有效的绝对 Uri。
最佳答案
您可能需要对“redirect_uri”进行 URL 编码。
以下是 MS Outlook REST 入门页面中的示例。注意 URI 上的编码:
GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=<CLIENT ID>&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F&response_type=code&scope=openid+Mail.Read
然后获取AccessToken:
POST https://login.microsoftonline.com/common/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=AwABAAAA...cZZ6IgAA&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F&client_id=<CLIENT ID>&client_secret=<CLIENT SECRET>
关于azure - 重定向 URI 对于 Outlook MAIL REST API 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591001/