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.
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.
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.
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With