验证信息的格式不正确。检查Azure Ratecard Apis中Authorization标头的值

时间:2018-04-25 07:17:04

标签: azure azure-active-directory

要获取价目表,我正在使用api。

https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview&$filter=OfferDurableId+eq+{offer-id}+and+Currency+eq+'USD'+and+Locale+eq+'en-US'+and+RegionInfo+eq+'IN'

我在请求中传递了授权标题,其值为“Bearer eyioe ...”。 它工作得比较早,但最近收到了以下回复

<?xml version="1.0" encoding="utf-8"?>
<Error>
    <Code>InvalidAuthenticationInfo</Code>
    <Message>Authentication information is not given in the correct format. Check the value of Authorization header.
RequestId:5dc4ea49-b01e-00f9-6760-dcfb83000000
Time:2018-04-25T06:42:45.8106146Z</Message>
</Error>

1 个答案:

答案 0 :(得分:0)

可能的情况是,RateCard API正在发回302重定向。在本期问题发布时,这个版本在新版本中发生了变化。

建议的解决方法是:

  1. 获取持有人令牌,
  2. 向ARM发出请求,并将Authentication Header设置为bearer token(这与之前相同)
  3. RateCard将返回302状态代码,该代码表示​​重定向并包含从中获取价目表的第二个URL(这是新的)。
  4. 必须向第二个URL发出第二个请求。请注意,许多http库将自动遵循重定向并为您发出第二个请求。在发出第二个请求时,需要删除包含承载令牌的授权头,如果不是,则会看到错误。如何在代码中实际实现这取决于您使用的是哪个http客户端。对于C#,代码如下:
  5. const string subId = "Subscription Id here";
    const string token = "Auth Token here.";
    
    const string offer = "MS-AZR-0003P";
    const string currency = "USD";
    const string locale = "en-US";
    const string region = "US";
    
    using (var rateCardClient = new HttpClient() { BaseAddress = new Uri("https://management.azure.com/") })
    {
        rateCardClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
        var response = await rateCardClient.GetAsync($"subscriptions/{subId}/providers/Microsoft.Commerce/RateCard?api-version=2016-08-31-preview&$filter=OfferDurableId eq '{offer}' and Currency eq '{currency}' and Locale eq '{locale}' and RegionInfo eq '{region}'");
    
        Console.WriteLine($"StatusCode:{response.StatusCode}");
        var ratecard = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
    
        Console.Write(ratecard);
    }
    

    有关详细信息,请参阅此Github问题页面: https://github.com/MicrosoftDocs/azure-docs/issues/7423