0
我正在嘗試使用Azure Management API來獲取指標數據。但是它拋出了403例外。有誰知道爲什麼這段代碼不起作用嗎?我覺得這段代碼很簡單,基於MSDN的文章,但仍然拋出一個403.任何幫助非常感謝!Azure管理API產生403錯誤
private string GetAccessToken()
{
var authenticationContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", TenantID));
var credential = new ClientCredential(clientId: AppID, clientSecret: SecretKey);
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/",
clientCredential: credential);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
return token;
}
...
using (var client = new HttpClient())
{
string header = GetAccessToken();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer", header
);
if (! resourceID.StartsWith("/"))
{
resourceID = "/" + resourceID;
}
string url = "https://management.azure.com" + resourceID +
"/providers/microsoft.insights/metricdefinitions?api-version=2016-03-01";
var response = client.GetStringAsync(url).Result;
return response;
}
相信在'新AuthenticationHeaderValue「承載」(「承載」,標題)後應該有一個空間'即,它應該是「承載」。請嘗試一下,看看是否能解決問題。 –
關於此主題有任何更新? –