簡體   English   中英

Azure API管理無效訪問令牌

[英]Azure API Management invalid access token

我正在嘗試為我的API管理生成訪問令牌。 我已經在Azure門戶中啟用了Management REST API,然后嘗試通過門戶以及以編程方式使用這兩個選項生成令牌。 這兩個選項均不起作用,並且出現錯誤響應:

“ {\\”錯誤\\“:{\\”代碼\\“:\\” InvalidAuthenticationToken \\“,\\”消息\\“:\\”訪問令牌無效。\\“}}”

我嘗試訪問的REST API: https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/reports//byApi?%24filter=timestamp%20ge%20datetime%272019-08-01T00%3A00%3A00%27%20and%20timestamp%20le%20datetime%272019-08-09T00%3A00%3A00%27&api-version=2019-01-01 : https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/reports//byApi?%24filter=timestamp%20ge%20datetime%272019-08-01T00%3A00%3A00%27%20and%20timestamp%20le%20datetime%272019-08-09T00%3A00%3A00%27&api-version=2019-01-01 {subscriptionID}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/reports//byApi https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/reports//byApi?%24filter=timestamp%20ge%20datetime%272019-08-01T00%3A00%3A00%27%20and%20timestamp%20le%20datetime%272019-08-09T00%3A00%3A00%27&api-version=2019-01-01

我的代碼:

public string GetAnalytics()
{
    string data = String.Empty;

    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri(_url);
        string token = GetToken();
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
        data = client.GetAsync(_url).Result.Content.ReadAsStringAsync().Result;
    }

    return data;
}

private string GetToken()
{
    var id = "integration";
    var key = _key;
    var expiry = DateTime.UtcNow.AddDays(10);
    string token = String.Empty;
    using (var encoder = new HMACSHA512(Encoding.UTF8.GetBytes(key)))
    {
        var dataToSign = id + "\n" + expiry.ToString("O", CultureInfo.InvariantCulture);
        var hash = encoder.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
        var signature = Convert.ToBase64String(hash);
        token = string.Format("SharedAccessSignature uid={0}&ex={1:o}&sn={2}", id, expiry, signature);
    }
    return token;
}

參考文獻:

https://docs.microsoft.com/zh-cn/rest/api/apimanagement/apimanagementrest/azure-api-management-rest-api-authentication

https://docs.microsoft.com/zh-cn/rest/api/apimanagement/2019-01-01/reports/listbyapi

請幫忙嗎?

您使用的API是Azure API,而不是Azure APIM API。 共享訪問簽名僅適用於Azure APIM API,不適用於Azure API。 為了使共享訪問簽名正常工作,請使用具有基本URL的API- https://{servicename}.management.azure-api.net

為了使Azure API正常工作,請使用OAuth2憑據。 如前所述設置客戶端-https: //docs.microsoft.com/zh-cn/rest/api/azure/#register-your-client-application-with-azure-ad

您使用的URL是azure rest api端點。 如果要調用天藍色的REST API,則需要獲取天藍色的廣告訪問令牌。 但是,您獲得的令牌是SAS令牌。 它僅可用於調用Azure API管理Rest API。 有關更多詳細信息,請參閱https://docs.microsoft.com/zh-cn/rest/api/apimanagement/apimanagementrest/api-management-rest

https://docs.microsoft.com/zh-cn/rest/api/azure/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM