403 Forbidden - Daily quota reached or API key unauthorized

Ok, your key is valid, yep (not a good idea to make that public btw…).

Can you print the full URL, passed parameters and headers? It needs Content-Type: application/json and well, the POST request actually has to post a JSON object (not sure if your lib does that automatically from that string you pass). That shouldn’t result in this API error, but the rest actually looks fine to me.

I removed that code from the Open Route page,
this is what returns me:

|response|{StatusCode: 403, ReasonPhrase: ‘Forbidden’, Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Connection: keep-alive Date: Wed, 08 May 2019 15:56:15 GMT Server: nginx/1.10.3 Server: (Ubuntu) Content-Length: 61 Content-Type: application/json }}|System.Net.Http.HttpResponseMessage|

We can’t help if you don’t give us one full exact HTTP request that fails: full URL, POST parameters and headers. Please print them in your debug window and paste them here (minus the Authorization header pls).

Paste. Please. This is not a nice way to request free support… And READ!! Your content-type is text/plain! If you use our POST endpoint, you have to POST a JSON, not a string! That’s what I was telling you here:

Can you print the full URL, passed parameters and headers? It needs Content-Type: application/json and well, the POST request actually has to post a JSON object (not sure if your lib does that automatically from that string you pass). That shouldn’t result in this API error, but the rest actually looks fine to me.

The example is the one that you propose on your page

Hi @Juan_Atenas,

This is C# i suppose? Due to limited human resources we cannot test every code example. They are generated automatically from a template which might not be working for C#.
The problem should be with either the AuthenticationHeaderValue or the TryAddWithoutValidation part.
If you get this to work, you might want to provide us with your solution, so other people won’t run into the same issue :slight_smile:

regards, Amandus

Team

I am also getting Error “Daily quota reached or API key unauthorized!” . Please find the uploaded screen shot for reference.

My Input Latitude and Longitude is : {locations:[[8.609168, 49.893230],[8.589405, 50.055435]]}

var baseAddress = new Uri(“https://api.openrouteservice.org”);
string responseData = “”;
using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));
httpClient.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue(“Authorization”, “xxx”);

            var SensorLocationsData = _notiUnitOfWork.NotificationRepository.GetLastSensorLocationData();
            string locations = "{locations:[locs]}";
            string loc = string.Empty;
            foreach (var SensorLocations in SensorLocationsData)
            {
                string str = string.Format("[{0}, {1}]", SensorLocations.Longitude, SensorLocations.Latitude);
                if (str != "")
                    loc += str + ","; 
            }

            locations = locations.Replace("locs", loc.Substring(0, loc.Length - 1));
            using (var content = new StringContent(locations))
            {
                using (HttpResponseMessage response = httpClient.PostAsync("/v2/matrix/driving-car", content).Result)
                {
                    responseData = response.Content.ReadAsStringAsync().Result;
                }
            }
        }
        string jsonString = JsonConvert.SerializeObject(responseData);
        return new JsonResult() { Data = jsonString, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

Thanks,
Vamsi

Please provide us with:

  • the exact URL incl endpoint
  • the POST body
  • the headers

in a human-readable and reproducible format, i.e. text. C# must have the option to print a “debuggable” version of the request, e.g. https://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient/35215502

Hallo Nils,

Please find the below details:

Exact URL incl endpoint : https://api.openrouteservice.org/v2/matrix/driving-car

Headers: {Content-Type: text/plain; charset=utf-8 Content-Length: 57}

“error”: “Daily quota reached or API key unauthorized”


My complete code i posted in above posts.

Cheers
Vamsi

Multiple things potentially wrong here:

Content-Type: application/json not text.

Authorization: Bearer <key>, not Authorization <key>.

Also, I don’t see your POST body… Please don’t make me look at screenshots:

in a human-readable and reproducible format, i.e. text.

Hi Nils,

i changed from text to json as content type and Bearer Key

Headers = {accept: application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8 Authorization: Bearer key}

RequestMessage:

{Method: POST, RequestUri: ‘https://api.openrouteservice.org/v2/matrix/driving-car’, Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
accept: application/json
accept: application/geo+json
accept: application/gpx+xml
accept: img/png; charset=utf-8
Authorization: Bearer key
Content-Type: application/json; charset=utf-8
Content-Length: 57
}}

Responce : {StatusCode: 500, ReasonPhrase: ‘Internal Server Error’, Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
X-Ratelimit-Limit: 500
X-Ratelimit-Remaining: 495
X-Ratelimit-Reset: 1575367946
Date: Mon, 02 Dec 2019 11:41:25 GMT
Server: nginx/1.10.3
Server: (Ubuntu)
Content-Length: 438
Content-Type: application/json
}}

I am using below code:

var baseAddress = new Uri(“https://api.openrouteservice.org”);
httpClient.DefaultRequestHeaders.Add(“Authorization”, "Bearer " + “xxx”);

using (var contents = new StringContent(“{locations:[[8.609168,49.893230],[8.589405,50.055435]]}”, Encoding.UTF8, “application/json”))
{
HttpResponseMessage response = httpClient.PostAsync(“/v2/matrix/driving-car”, contents).Result;
responseJson = response.Content.ReadAsStringAsync().Result;
}
string jsonString = JsonConvert.SerializeObject(responseJson);
return new JsonResult() { Data = jsonString, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

I still don’t see any POST parameters… I don’t see how I can help you… It works for everyone else, so I’m pretty sure that’s a basic programming mistake on your side and we can’t fix that for you.

Nils,

i am passing inputparameters: {coordinates:[[8.609168, 49.893230],[8.589405, 50.055435]]}

httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));
httpClient.DefaultRequestHeaders.Add(“Authorization”, “Bearer " + “key”);
var serializedAuthor = JsonConvert.SerializeObject(data);
var serializedData = serializedAuthor.Replace(@”“”", “”);

            using (var content = new StringContent(serializedData, Encoding.UTF8, "application/json"))
            {
                    using (HttpResponseMessage response = await httpClient.PostAsync("/v2/directions/driving-car", content))
                    { 
                      responseData =  await response.Content.ReadAsStringAsync();
                    }
                } 
            } 

Headers {Accept: application/json Authorization: Bearer key}

RequestMessage {Method: POST, RequestUri: ‘https://api.openrouteservice.org/v2/directions/driving-car’, Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Accept: application/json
Authorization: Bearer key
Content-Type: application/json; charset=utf-8
Content-Length: 59
}}

Stil i am not getting any responce from server.

my complete code also i mentioned in above screen shorts.

Thanks
Vamsi

No idea… Please format the code, this is markdown. It’s painful to look at… Unfortunately that’s not only applying to the formatting…

No idea about C#, but this looks wrong: Content: System.Net.Http.StringContent as the content for a POST request. Why a string? Compare with

There it’s a Content: System.Net.Http.ObjectContent, which makes a whole lot more sense. But like I said, no idea about C#… Please post a question on stack overflow, this is not a an issue with our software, but your code…

Hi all, I found a solution, using C# Code:

  1. Clear DefaultRequestHeader
  2. User Bearer ( not Authorization ) as Key
  3. Escape double quotes inside StringContent
  4. Add Encoding and MediaType to StringContent constructor

Hope this helps :slight_smile:

Hi @SonGokuOnBike,
Thanks for the working example.
I will adjust the C# example in the API Playground with this.

If you want to paste code please use

```
using {var
...
}
```

or the preformatted text button.
then it can be copied :wink:

Best regards

Hi @amandus,

hope this looks better as example code :slight_smile:

private async void buttonTestOpenRouteService_ClickAsync(object sender, EventArgs e)
{
    //Common testing requirement. If you are consuming an API in a sandbox/test region, uncomment this line of code ONLY for non production uses.
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    //Be sure to run "Install-Package Microsoft.Net.Http" from your nuget command line.
    var baseAddress = new Uri("https://api.openrouteservice.org");

    using (var httpClient = new HttpClient { BaseAddress = baseAddress })
    {
        httpClient.DefaultRequestHeaders.Clear();
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "5b3 .. c5");

        using (var content = new StringContent("{\"coordinates\":[[8.678,49.551],[8.502,49.531]],\"attributes\":[\"avgspeed\",\"percentage\",\"detourfactor\"],\"elevation\":\"true\",\"extra_info\":[\"suitability\",\"surface\",\"waycategory\",\"waytype\",\"traildifficulty\",\"osmid\",\"countryinfo\"],\"instructions_format\":\"html\",\"language\":\"de\",\"preference\":\"recommended\"}", Encoding.UTF8, "application/json"))
        {
            using (var response = await httpClient.PostAsync("/v2/directions/cycling-regular/json", content))
            {
                string responseData = await response.Content.ReadAsStringAsync();

                var data = JsonConvert.DeserializeObject(responseData);
            }
        }
    }
}

And inside the designer ( it’s a winforms application):

this.buttonTestOpenRouteService.Click += new System.EventHandler(this.buttonTestOpenRouteService_ClickAsync);

Best regards

SonGokuOnBike

@SonGokuOnBike,

Some update on the template: I did add some of your changes, but the escaping of the backslashes sadly didn’t work. I added a comment, to escape quotes.

In a bit more detail: We use a highlighting plugin for the different code templates for readability. Before the template is passed, the quotes actually are escaped. But after beautifying it, escaped quotes are replaced by normal ones :confused: There is a setting to disable this which had no effect.

To further look into this is not really worth the outcome right now.
I know it should be a one click running template, but it has to stay like this for now.

We are using the vue-markdown component to render the templates and PrismJS for highlighting if someone wants to investigate this.

Best regards