如何在 Azure 函数中使用 Azure 资源健康 API?
How to use Azure resource health API in Azure function?
我有一种情况想在 public 论坛上显示我的 Azure 服务的运行状况我正在尝试构建它,比如 Azure 计时器功能将获取资源的状态并转储到数据库中可以通过调用 API 由外部应用程序显示我尝试了一些东西但是 Azure 资源健康 API 受 Azure 安全性保护并且期望 Oauth2 隐式流我认为是在浏览器中并通过用户启动的interaction 有没有什么方法可以绕过这个并简单地访问资源运行状况 API
在 Azure 函数中,我们可以使用 Azure MSI 来请求令牌,然后我们可以使用令牌调用资源运行状况 API。
例如
-
将 Azure RABC 角色贡献者角色分配给 MSI
代码
// Install package "Azure.Identity"
private static HttpClient httpClient = new HttpClient();
[FunctionName("Function2")]
public static async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup=true)]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
var creds = new DefaultAzureCredential();
var scopes = new string[] { "https://management.azure.com/.default" };
AccessToken token= await creds.GetTokenAsync(new TokenRequestContext(scopes));
string uri = " ";
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,uri)) {
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
using (HttpResponseMessage response = await httpClient.SendAsync(request)) {
if (response.IsSuccessStatusCode) {
var str = await response.Content.ReadAsStringAsync();
log.LogInformation(str);
}
}
}
我有一种情况想在 public 论坛上显示我的 Azure 服务的运行状况我正在尝试构建它,比如 Azure 计时器功能将获取资源的状态并转储到数据库中可以通过调用 API 由外部应用程序显示我尝试了一些东西但是 Azure 资源健康 API 受 Azure 安全性保护并且期望 Oauth2 隐式流我认为是在浏览器中并通过用户启动的interaction 有没有什么方法可以绕过这个并简单地访问资源运行状况 API
在 Azure 函数中,我们可以使用 Azure MSI 来请求令牌,然后我们可以使用令牌调用资源运行状况 API。
例如
将 Azure RABC 角色贡献者角色分配给 MSI
代码
// Install package "Azure.Identity"
private static HttpClient httpClient = new HttpClient();
[FunctionName("Function2")]
public static async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup=true)]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
var creds = new DefaultAzureCredential();
var scopes = new string[] { "https://management.azure.com/.default" };
AccessToken token= await creds.GetTokenAsync(new TokenRequestContext(scopes));
string uri = " ";
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,uri)) {
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
using (HttpResponseMessage response = await httpClient.SendAsync(request)) {
if (response.IsSuccessStatusCode) {
var str = await response.Content.ReadAsStringAsync();
log.LogInformation(str);
}
}
}