Hello, i receive this error:
{{
“error”: “Authorization field/api_key missing in request. If you do not have a token, please sign up for one at https://openrouteservice.org”
}}
You can try adding a Content-Type Header similar to the Accept header with "Content-Type: application/json; charset=utf-8".
That might be the problem, but not quite sure as i’m not familiar with C#.
If you can get the request to work, would be great if you let us know how, so we can update the C# example of the Playground.
No way, seems related to Authenticate so i decided to change apporoach: I followed example about posting json:
Quotas are ok; I checked result JSON format with online tool.
var url = "https://api.openrouteservice.org/v2/directions/driving-car?api_key=your_api_key&start=8.681495,49.41461&end=8.687872,49.420318";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Headers["Authorization"] = "Bearer your_api_key";
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
Console.WriteLine(httpResponse.StatusCode);
I’m trying to test POST method with an online working tool. No way to work it give same error given by my app:
For the get request both api_key parameter and Authorization header can be used.
However, in the Header you don’t need Bearer, just Authorization: 5b3ce3.....
In your online tool, you don’t have th right path to the directions endpoint. It should be something like: https://api.openrouteservice.org/v2/directions/driving-car
Please check the documentation for available ones.
You can try generating a curl request with the example code button, to see which the minimal Headers are that you nead for the request.
Ok, you are right: in the GET it doesn’t need Bearer; it work without it.
For the POST following code works very well:
var url = "https://api.openrouteservice.org/v2/directions/driving-car/json";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Accept = "application/json";
httpRequest.Headers["Authorization"] = "Bearer 5b3ce3597851110001cf6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
httpRequest.ContentType = "application/json";
var data = @"{""coordinates"":[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]]}";
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
Console.WriteLine(httpResponse.StatusCode);
but,
your example in the API Playground dont work (ok, in the online tool with correct uri, works) check screen with result error:
AH, I FORGOT to say that, i’m using Xamarin Forms that support only mono framework (yes in short will be support fully .net 6 and c#9) taht is similar to .net standard 2.
using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
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.Authorization =
new AuthenticationHeaderValue("Bearer", "xxx");
// quotes might have to be escaped
using (var content = new StringContent("{\"coordinates\":[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]]}", Encoding.UTF8, "application/json"))
{
using (var response = await httpClient.PostAsync("/v2/directions/driving-car", content))
{
string responseData = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject(responseData);
var serialised = JsonConvert.SerializeObject(data);
Console.WriteLine(serialised);
}
}
}
OMG It works. I’m sure that i have tested your solution and it gave me:
{
“error”: “Daily quota reached or API key unauthorized”
}
maybe i was worng in something…
Thank you for fully support you gave me!!!