23

I am new to API management. I have created a Basic WEB API & hosted to the API APP(App service). URL is working as expected & it's returning the data. i.e. http://xyz.azurewebsites.net/api/webapi

But when I am adding the API App in the API management, I am getting different URL with Extra suffix I am adding, But when I am trying to open in browser Link--> https://abc.azure-api.net/God am getting the below error

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

If its no issue with API APP then it shouldn't be with API management. Please guid me if something I am missing.

NB--> I have tried adding the Subscription Key in fiddler its different issue is coming. but to access a URL it doesn't require Subscription Key basically.

lokanath das
  • 736
  • 1
  • 10
  • 35
  • A work-around : Disable subscription key in product settings - [Azure Api management Is it possible to disable Subscription Key](https://stackoverflow.com/q/51376248/465053) – RBT Sep 22 '21 at 07:41
  • check this link if want to know your subscription key which is to be passed to the API in request header - [Where to find my Ocp-Apim-Subscription-Key in Windows azure](https://stackoverflow.com/q/40867834/465053) – RBT Sep 22 '21 at 07:43

3 Answers3

25

If you enable the option to Require subscription for the product settings, then you must pass the below header Ocp-Apim-Subscription-Key. Even you provide subscription key, the key should belong to the product which the API includes. If you don't want the subsciption option, disable it in the product settings.

VinuBibin
  • 679
  • 8
  • 19
  • But although I had supplied the proper Subscription from Unlimted Product (as I had created the API as Unlimited) still I was getting the same issue. Removal of the Option is the solution, but in the live application how to deal with it? – lokanath das Jul 25 '18 at 13:33
  • You should provide subscription key for your apis. its a measure of authentication. What error you are getting when you provide subscription key – VinuBibin Jul 25 '18 at 13:39
  • { "statusCode": 401, "message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API." } This error is coming when m trying top open in the browser also. – lokanath das Jul 26 '18 at 08:54
  • when you open in browser directly, you have no option to add the header. Hence the error is coming. if you add the header and call from postman, which will works perfect! – VinuBibin Jul 26 '18 at 09:01
  • check this link if you want to pass headers while opening in browser https://stackoverflow.com/questions/19021226/programmatically-pass-additional-header-info-along-with-url-to-open-a-browser – VinuBibin Jul 26 '18 at 09:04
6

If you enable the option to Require subscription for the product settings, then you must pass the below header Ocp-Apim-Subscription-Key. Even you provide subscription key, the key should belong to the product which the API includes. Add the your APIs in your products.

  1. Select the Products menu/link from Azure portal.
  2. Select the product from list.
  3. Select the APIs from selected product options.
  4. Click on Add button and select your API from list and click on Select.

You are good to use your API using Postman or your code. You have to pass the subscription key in header key (Ocp-Apim-Subscription-Key).

You can find the subscription key (Primary/Secondary) in api developer portal on profile screen.

RaviK
  • 81
  • 1
  • 2
4

You have to pass your subscription key in request headers.

Add this to your C# code

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

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

Add this to your app settings file

"OcpApimSubscriptionKey": "your key",

Sample code:

 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;
            }
Kurkula
  • 6,386
  • 27
  • 127
  • 202