小编Tej*_*jas的帖子

Azure Billing Usage API返回401 Unauthorized

我使用Azure REST API来获取结算使用率和价目表详细信息.要使用AcquireToken()方法获取令牌,最初我只使用客户端ID,然后在登录窗口中询问用户凭据.

但是,我正在寻找非交互式方法,因此我使用了Client Credentials,其中我传递了Client IdClient Secret Key.

但它给出了"远程服务器返回错误401未经授权"

当我深入研究错误时,我发现它给出了错误"访问令牌来自错误的受众或资源"

请给我任何解决方案,我可以在没有任何用户交互的情况下访问API.

提前致谢.

这是我的代码:

{
    string token = GetOAuthTokenFromAAD();
    string requestURL = String.Format("{0}/{1}/{2}/{3}",
                   ConfigurationManager.AppSettings["ARMBillingServiceURL"],
                   "subscriptions",
                   ConfigurationManager.AppSettings["SubscriptionID"],
                   "providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview&$filter=OfferDurableId eq 'MS-AZR-*****' and Currency eq 'INR' and Locale eq 'en-IN' and RegionInfo eq 'IN'");

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);

    request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
    request.ContentType = "application/json";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Console.WriteLine(String.Format("RateCard service response status: {0}", response.StatusDescription));
}

public static string …
Run Code Online (Sandbox Code Playgroud)

c# azure azure-billing-api

6
推荐指数
1
解决办法
1178
查看次数

标签 统计

azure ×1

azure-billing-api ×1

c# ×1