API管理URL正在提供缺少订阅密钥的问题

时间:2018-07-25 13:08:06

标签: azure azure-api-management azure-api-apps

我是API管理的新手。我创建了一个基本的WEB API,并托管到API APP(应用程序服务)。 URL正常工作,并且正在返回数据。即http://xyz.azurewebsites.net/api/webapi

但是当我在API管理中添加API App时,我得到的URL带有附加的后缀,但是当我尝试在浏览器中打开链接时,-> https://abc.azure-api.net/God得到了以下内容错误

{ "statusCode": 401, "message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API." }

如果API APP没问题,那么就不应该使用API​​管理。如果我缺少什么,请引导我。

NB->我试图在小提琴手中添加订阅密钥,这将是另一个问题。但访问URL基本上不需要订阅密钥。

3 个答案:

答案 0 :(得分:7)

如果为产品设置启用了需要订阅的选项,则必须传递下面的标题Ocp-Apim-Subscription-Key。 即使您提供订阅密钥,该密钥也应该属于API包含的产品。 如果您不需要订阅选项,请在产品设置中将其禁用。

答案 1 :(得分:3)

如果启用了产品设置要求“需要订阅”的选项,则必须传递以下标头Ocp-Apim-Subscription-Key。即使您提供订阅密钥,该密钥也应该属于API包含的产品。在您的产品中添加您的API。

  1. 从Azure门户中选择“产品”菜单/链接。
  2. 从列表中选择产品。
  3. 从所选产品选项中选择API。
  4. 单击“添加”按钮,然后从列表中选择您的API,然后单击“选择”。

您最好通过Postman或您的代码使用API​​。 您必须在标头密钥(Ocp-Apim-Subscription-Key)中传递订阅密钥。

您可以在个人资料屏幕上的api开发人员门户中找到订阅密钥(主/次)。

答案 2 :(得分:0)

您必须在请求标头中传递您的订阅密钥。

将此添加到您的C#代码

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", BearerToken);

 request.Headers.Add("Ocp-Apim-Subscription-Key", config["OcpApimSubscriptionKey"]);

将此添加到您的应用设置文件

"OcpApimSubscriptionKey": "your key",

示例代码:

 try
            {

                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(url);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Add("Authorization", BearerToken);
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", config["OcpApimSubscriptionKey"]);
                    HttpResponseMessage response = client.GetAsync(url).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        return response.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        var ResponseResult = await response.Content.ReadAsStringAsync();
                        return ResponseResult;
                    }
                }
            }

            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
                    string errorText = reader.ReadToEnd();
                }
                throw;
            }
            catch (ArgumentNullException ex)
            {
                throw;
            }
            catch (InvalidOperationException ex)
            {
                throw;
            }
            catch (HttpRequestException ex)
            {
                throw;
            }