【问题标题】:the authentication failed because of missing 'authorization' header由于缺少“授权”标头,身份验证失败
【发布时间】:2019-09-24 18:52:24
【问题描述】:

Bearer authorization_uri="https://login.windows.net/{tenantid}", error="invalid_token", error_description="身份验证失败,因为缺少'Authorization'标头。"

状态码:未授权

使用client_id, 客户秘密, 授予类型, 资源

提前致谢

【问题讨论】:

  • 你要调用哪个api?
  • management.azure.com, graph.microsoft.com
  • 你需要获取访问令牌并像这样将其添加到标题中 Authorization:Bearer {accesstoken} 看看这篇文章docs.microsoft.com/en-us/rest/api/azure
  • 正在使用任何库或使用 API 进行身份验证
  • @Nagarajan.M 您是否添加了权限并单击管理员授予按钮?

标签: azure oauth-2.0 microsoft-graph-api


【解决方案1】:

您需要grant Contributor role to your application,然后使用资源(https://management.azure.com/)获取访问令牌。

然后就可以调用api了。

您也可以使用api文档中的try函数直接调用该api。

【讨论】:

  • 你能告诉我吗?是来自应用注册的 microsoft graph 的权限问题吗?
  • @Nagarajan.M 您指的是哪个文件?
  • 请参考我的图片(获取访问令牌,创建证书(使用访问令牌),错误描述)
  • @Nagarajan.M 我已经在我身边尝试过这个 api 并且它有效。我会用详细信息更新我的答案。
  • 我收到此错误状态代码:禁止,内容类型:应用程序/json; charset=utf-8, Content-Length: 445) {"error":{"code":"AuthorizationFailed","message":"对象 id 为 '' 的客户端 '' 无权执行操作 'Microsoft. Devices/provisioningServices/certificates/write' 覆盖范围 '/subscriptions/**/resourceGroups/**/providers/Microsoft.Devices/provisioningServices/**/certificates/*'。"}}
【解决方案2】:

您可以使用下面的代码来获取令牌。然后使用 Bearer 关键字在标头中传递此令牌。如需更多参考,您可以查看此链接 (https://github.com/inzi25/AzureFunctionAPIMBackup)

private static async Task<String> GetToken()
        {
            string clientID = "xxxxxxxxxxxxxxxxxxxxxx";
            string username = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            string password = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

            var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxx");
            var credential = new UserPasswordCredential(username, password);

            var result = await authenticationContext.AcquireTokenAsync("https://management.azure.com/", clientID, credential);

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }

            return result.AccessToken;
        }

【讨论】:

  • 嗨 Mohamed Infaaz,请参考我的邮递员照片。我得到了访问令牌,但使用此访问令牌时出现此错误身份验证失败,因为缺少“授权”标头
  • 好的。您现在可以通过以下格式进行排序吗,授权:Bearer xxxxxxxxxxxxxxxx
  • 你能告诉我吗?是来自应用注册的 microsoft graph 的权限问题吗?
  • 好的。你还在出错吗?我可以得到错误信息吗?顺便说一句,您尝试使用哪个 API ...我的意思是哪个功能?
  • 请参考我的图片(获取访问令牌,创建证书(使用访问令牌),错误描述)
猜你喜欢
  • 2019-06-14
  • 2020-10-21
  • 2021-09-24
  • 2015-05-05
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
相关资源
最近更新 更多