Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API management URL is giving Missing subscription key Issue

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.

like image 628
lokanath das Avatar asked Jul 25 '18 13:07

lokanath das


People also ask

Where do I find my OCP-Apim-subscription-key?

The subscription key can be found under user profile in the API Manager Portal. The subscription key is assigned to the Ocp-Apim-Subscription-Key parameter the header.

How do I pass my postman subscription key?

In the request Authorization tab, select API Key from the Type list. Enter your key name and value, and select either Header or Query Params from the Add to dropdown list. You can store your values in variables for additional security.


3 Answers

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.

like image 138
VinuBibin Avatar answered Sep 29 '22 16:09

VinuBibin


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.

like image 5
RaviK Avatar answered Sep 29 '22 17:09

RaviK


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;
            }
like image 4
Kurkula Avatar answered Sep 29 '22 16:09

Kurkula