为Microsoft Graph API获取有效的访问令牌

鲁尼尔·德夫

我正在使用ASP.NET MVC5 Web应用程序进行工作,该应用程序使用Azure ADAL库对用户进行身份验证,但是工作正常,但是,当我手动向图形发送请求时,例如:GET https://graph.microsoft.com/v1.0 / me或GET https://graph.microsoft.com/v1.0/groups?$ filter = from / displayName eq'whatever'。

我尝试更新Azure中的应用程序注册以添加所需的Graph权限,并且我也尝试创建新的应用程序注册,无论我做什么,我的请求将始终响应401未经授权,我缺少什么吗?

编辑:来自邮递员的示例响应

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure.",
    "innerError": {
      "request-id": "a142576b-acce-4e59-8a8d-adede61aaf59",
      "date": "2017-04-05T13:27:36"
    }
  }
}

编辑:C#请求示例

public async Task<GroupGraph> GetGroupIdByDisplayName(string displayName)
{
    var accessToken = await authenticationService.GetTokenUserOnly();
    GroupGraph groupGraphResponse = null;
    using (var client = new HttpClient())
    {
        using (var request = new HttpRequestMessage(HttpMethod.Get, $"https://graph.microsoft.com/v1.0/groups?$filter=from/displayName eq '{displayName}'"))
            {
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                using (var response = client.SendAsync(request).Result)
                {
                    if (response.IsSuccessStatusCode)
                    {
                        using (var content = response.Content)
                        {
                            var result = await content.ReadAsStringAsync();
                            groupGraphResponse = JsonConvert.DeserializeObject<GroupGraph>(result);
                        }
                    }
                }
            }
        }
        return groupGraphResponse;
    }

编辑:我获取令牌的方式

public async Task<string> GetTokenUserOnly()
    {
        string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
        string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
        string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

        // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
        ClientCredential clientcred = new ClientCredential(clientId, appKey);
        // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
        AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new TableTokenCache(signedInUserID));
        //AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
        AuthenticationResult authenticationResult = authenticationContext.AcquireToken(graphResourceID, clientcred);
        return authenticationResult.AccessToken;
    }
杰瑞德·霍尔特金

您不能使用ADAL获取图形标记。microsoft .com。ADAL用于图形。Windows .net。

为了获取图形库(graph.windows.com)的令牌,请查看Nuget包Microsoft.Graph。Microsoft还提供了一些有关如何使用Graph提取用户信息的文档

但是请注意,并排使用图形库和ADAL库会导致一些奇怪的副作用,例如清除凭据缓存。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

文件附件在Microsoft Graph API中显示为消息实体

获取Microsoft Graph和单个服务API终结点(Outlook REST API等)的访问令牌

Microsoft Graph API:POST请求的有效负载大小是否有限制

如何从节点脚本获取Microsoft Graph API访问令牌?

使用PHP从Microsoft Graph API获取访问令牌

Microsoft Graph API:省略用户访问令牌

获取Java中Microsoft Graph API的令牌

Microsoft Graph API的访问令牌立即过期

从Node.js脚本获取Microsoft GRAPH访问令牌

Microsoft Graph-获取访问令牌

microsoft-graph api:从图中的刷新令牌获取新的访问令牌,而无需重定向URL

Microsoft graph API:无法使用生成的访问令牌获取用户

Microsoft graph API->查看消息是否为回复

访问令牌验证失败Microsoft Graph API

为Intune批准对Microsoft Graph Api的调用

Microsoft Graph API-如何在没有授权码的情况下获取访问令牌?

给定具有User.Read权限的有效访问令牌,GET https://graph.microsoft.com/v1.0/me返回401(未经授权)

Microsoft Graph API:为不是我的用户创建事件

Microsoft Graph API One Drive无法读取JSON请求有效负载

从 Microsoft Graph API 接收访问令牌但不接收刷新令牌

Microsoft Graph API - 无法刷新访问令牌

Graph API Onedrive approot 创建为“Microsoft Graph”

如何使用 ajax 调用获取 Microsoft Graph API 访问令牌

无法获取 Microsoft Graph OAuth 访问令牌

从 Microsoft Graph Api 为 SchemaExtensions 获取 400 个错误请求

从 Microsoft Graph for Azure 获取访问令牌

如何使用 Microsoft Graph API 为应用设置访问令牌生存期

为 Microsoft Graph API 正确格式化 curl 请求

为什么为 microsoft graph api 请求不记名访问令牌不起作用?