openrouteservice API - "error": "Authorization field missing"

645 views Asked by At

I have a project for my university and I need to use openrouteservice API. I registered there and I generate an api-key. At API playground I choose Directions and then I get a basic route between two points with the profile provided. Returned response is in GeoJSON format. This method does not accept any request body or parameters other than profile, start coordinate, and end coordinate.

I need to do this in C#, so this is my class where I want to call this API.

class OpenRouteServiceCall{

        private string \_apiKey = "my_key_number";

        private HttpClient _client;
        private Uri _baseAddress;
    
        public OpenRouteServiceCall()
        {
            _client = new HttpClient();
            
            _client.DefaultRequestHeaders.Clear();
            _client.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
        }
    
        public async Task<RouteInfo> GetRouteInfo()
        {
            _baseAddress = new Uri($"https://api.openrouteservice.org/v2/directions/driving-car?api_key={_apiKey}&start=27.589244,%2047.160306&end=27.72833,%2046.641804");
            _client.BaseAddress = _baseAddress;
         
            using (var response = await _client.GetAsync("directions"))
            {
                string responseData = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseData);
                var data = JsonConvert.DeserializeObject<RouteInfo>(responseData);
                return data;
            }
        }

}

There is a line Console.WriteLine(responseData); and it shows me:

"error": "Authorization field missing"

And yes, my_key_number is a long string, I have it on my dashboard and the key is still valid.

It's the first time I use an API and I don't know how to proceed in this situation. Please, someone help me.

I need the details from JSON: distance, duration, speed, congestion.

0

There are 0 answers