Search code examples
c#.netvisual-studioopenstreetmap

How to fix the error : the remote server returned an error: (403) forbidden | OpenStreet - Nominatim API


I'm trying to access the Nominatim API from OpenStreet to get Latitude and Longitude but it's getting the error: the remote server returned an error: (403) forbidden

My .Net Framework is 4.6

Here's my code:

private void getLatLon(ref float latitude, ref float longitude)
        {
            dynamic CallOpenStreetApiRestService()
            {
                dynamic result;

                string uri = $"http://nominatim.openstreetmap.org/search?street=Maracanã Stadium&format=json";
                var req = (HttpWebRequest)HttpWebRequest.Create(uri);
                req.Method = "GET";
                //req.ContentType = "application/json";
                //req.UseDefaultCredentials = true;
                //req.Credentials = CredentialCache.DefaultCredentials;
                //req.Proxy.Credentials = CredentialCache.DefaultCredentials;

                using (var resp = req.GetResponse())
                {
                    var results = new StreamReader(resp.GetResponseStream()).ReadToEnd();
                    result = JArray.Parse(results);
                }

                return result;
            }

            JArray result = CallOpenStreetApiRestService();
            foreach (dynamic it in result)
            {
                var lat = Convert.ToInt32(it.lat.ToString());
                var lon = Convert.ToInt32(it.lon.ToString());

                latitude = lat;
                longitude = lon;
            }

        }

As you can see, I already tried to put the default credentials but not lucky The uri works on Insomina with and without validate certificates


Solution

  • operations.osmfoundation.org/policies/nominatim

    In the Usage Policy, it requires at last the UserAgent

    This works to me: req.UserAgent = ".NET Framework Test Client";