bearerauthorization_uri =“ https://login.windows.net/{tenantid}”,error =“ invalid_token”,error_description =“由于缺少“ Authorization”标头,身份验证失败。”
状态码:未经授权
使用client_id, client_secret, grant_type, 资源
答案 0 :(得分:1)
您需要先grant Contributor role to your application,然后获取带有资源(https://management.azure.com/)的访问令牌。
然后您将可以调用api。
答案 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;
}