验证由于缺少“授权”标头而失败

时间:2019-05-07 06:49:44

标签: azure oauth-2.0 microsoft-graph

bearerauthorization_uri =“ https://login.windows.net/{tenantid}”,error =“ invalid_token”,error_description =“由于缺少“ Authorization”标头,身份验证失败。”

状态码:未经授权

使用client_id, client_secret, grant_type, 资源

enter image description here getting accesstoken

create certificate预先感谢

error description

2 个答案:

答案 0 :(得分:1)

您需要先grant Contributor role to your application,然后获取带有资源(https://management.azure.com/)的访问令牌。 enter image description here

然后您将可以调用api。

enter image description here

您还可以通过使用api文档中的try函数直接调用api。 enter image description here

答案 1 :(得分:0)

您可以使用下面的代码来获取令牌。然后使用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;
        }