【问题标题】:Access token validation failure when creating Microsoft Graph webhook using the "Web API on-behalf-of flow"使用“代表流的 Web API”创建 Microsoft Graph webhook 时访问令牌验证失败
【发布时间】:2017-03-27 03:38:00
【问题描述】:

我想做的是使用微软在this article 中描述的“Web API 代表流”场景来创建一个网络挂钩。

所以我从 Microsoft github example 开始,并确保我可以通过 Graph API 成功获取用户个人资料。

然后我修改了获取用户配置文件的代码以创建网络挂钩,因此代码如下所示:

// Authentication and get the access token on behalf of a WPF desktop app.
// This part is unmodified from the sample project except for readability.

const string authority = "https://login.microsoftonline.com/mycompany.com";
const string resource = "https://graph.windows.net";
const string clientId = "my_client_id";
const string clientSecret = "my_client_secret";
const string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";

var user = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var authenticationContext = new AuthenticationContext(authority,new DbTokenCache(user));
var assertion = ((BootstrapContext) ClaimsPrincipal.Current.Identities.First().BootstrapContext).Token;
var userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null
    ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value
    : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;

var result = await authenticationContext.AcquireTokenAsync(resource,new ClientCredential(clientId,clientSecret),new UserAssertion(assertion,assertionType,userName));

var accessToken = result.AccessToken;


// After getting the access token on behalf of the desktop WPF app,
// subscribes to get notifications when the user receives an email.
// This is the part that I put in.

var subscription = new Subscription
{
    Resource = "me/mailFolders('Inbox')/messages",
    ChangeType = "created",
    NotificationUrl = "https://mycompany.com/subscription/listen",
    ClientState = Guid.NewGuid().ToString(),
    ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 4230, 0)
};

const string subscriptionsEndpoint = "https://graph.microsoft.com/v1.0/subscriptions/";
var request = new HttpRequestMessage(HttpMethod.Post, subscriptionsEndpoint);

var contentString = JsonConvert.SerializeObject(subscription, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
request.Content = new StringContent(contentString, System.Text.Encoding.UTF8, "application/json");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = await new HttpClient().SendAsync(request);

if (response.IsSuccessStatusCode)
{
    // Parse the JSON response.
    var stringResult = await response.Content.ReadAsStringAsync();
    subscription = JsonConvert.DeserializeObject<Subscription>(stringResult);
}

我从响应中得到的错误是:

{
 "error":
 {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure.",
    "innerError":
    {
      "request-id": "f64537e7-6663-49e1-8256-6e054b5a3fc2",
      "date": "2017-03-27T02:36:04"
    }
  }
}

webhook 创建代码直接取自ASP.NET webhook github sample project,我也确保可以成功运行。

相同的访问令牌代码与原始用户配置文件读取代码一起使用:

// Call the Graph API and retrieve the user's profile.

const string requestUrl = "https://graph.windows.net/mycompany.com/me?api-version=2013-11-08";
request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await new HttpClient().SendAsync(request);

所以我想知道:

  1. 是否支持通过图形 API 使用代表流创建 webhook?不确定this SO question 是否是我在这里寻找的。​​li>
  2. 如果支持,我在这里缺少什么?
  3. 如果不支持,是否有替代方法来实现?例如。我可以使用现有 Office 365 API 中的任何内容吗?

【问题讨论】:

    标签: azure azure-active-directory webhooks microsoft-graph-api azure-ad-graph-api


    【解决方案1】:

    "message": "访问令牌验证失败。",

    该错误意味着您获得的资源访问令牌不正确。根据您的代码,您获得了资源的访问令牌:https://graph.windows.net(Azure AD Graph API),但随后您使用该访问令牌访问 Microsoft Graph API(https://graph.microsoft.com),因此访问令牌验证失败。

    【讨论】:

    • 看准南宇!获取访问令牌时,我将资源更改为“graph.microsoft.com”,现在我可以接收发送到“mycompany.com/subscription/listen”的验证令牌。将令牌返回到 Graph API 后,我现在从 Web 挂钩响应中获得 code: ExtensionError, message: Operation: Create; Exception: [Status Code: Forbidden; Reason: Forbidden]。我已经为我的多租户 Web 应用程序授予了从“Microsoft Graph”API 中“读取用户邮件”的委托权限。你知道我在这里还缺少什么吗?
    • 请先检查访问令牌中的“scp”声明是否包含正确的权限。
    • 你是对的 Nan Yu,scp 声明只有“User.Read”。经过一番摆弄,我发现在 Azure AD 应用程序注册门户中,我不仅要为我的多租户 Web 应用程序授予“读取用户邮件”的委托权限,而且还必须使用“授予权限”按钮在“所需权限”面板的顶部“为当前目录中的所有帐户授予多租户 Web 应用程序的权限”。只有在此之后,我的访问令牌中的 scp 声明才能变为“Mail.Read User.Read”。
    • @Leon Zhou,很高兴听到你解决了问题
    • 来自 MS 的两个具有相同名称的独立 API,这真的很难。我花了好几个小时才找到胶水:(而且没有离线(类似服务的)应用程序身份验证库(是吗?)。我只是为此使用了 HttpClient + 手动缓存
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2018-02-05
    • 2021-06-13
    • 1970-01-01
    • 2021-03-21
    • 2019-07-16
    相关资源
    最近更新 更多