mocnik-science / osm-python-tools

A library to access OpenStreetMap related services
GNU General Public License v3.0
441 stars 48 forks source link

ERROR 403: Forbidden Request #76

Open g-aaroncruz opened 9 months ago

g-aaroncruz commented 9 months ago

Why am I getting this error while following the README examples?

I was able to use the overpass module and the Api module but cannot use the Nominatim class. I'm not on any VPN or any funny proxy business.

from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
areaId = nominatim.query('Vienna, Austria').areaId()

image image

JustinKottinger commented 8 months ago

I have the same problem. Tried setting with the userAgent parameter as per the "General Remarks" API but nothing worked. Would appreciate someone's help on this.

JustinKottinger commented 8 months ago

Update for anyone else with this issue:

The Problem The problem is, in fact, due to incorrect setting of the HTTP User-Agent during the request. As per the general remarks we need to set the userAgent of the Nominatim at instantiation. Doing so adds a prefix to the packages default, which is OSMPythonTools/X.Y.Z (https://github.com/mocnik-science/osm-python-tools). It turns out that the default string that the package provides is what causes the server to refuse the connection (i.e. it makes the server think that a bot is requesting information rather than a human with good intentions).

The Solution I agree with the general remarks that a userAgent is required. It should be something identifiable such as a name and email and should not include any weird package settings or urls. I suggest the contributors change the default string to not include these items. In the meantime, though, end-users can hack a solution together by overriding the method that causes the problem (namely, CacheObject._userAgent(self)). I have a minimal working example below:

from OSMPythonTools.internal.cacheObject import CacheObject
from OSMPythonTools.nominatim import Nominatim

'''
Define a function to override the current implementation
Inputs: self
Output: a valid string to be provided to OSM. 
'''
def _myUserAgent(self):
    return '[your name] [your email]'

if __name__ == '__main__':
    CacheObject._userAgent = _myUserAgent # override the current implementation with my working function
    nominatim = Nominatim()
    areaId = nominatim.query('Vienna, Austria').areaId()
    print(areaId)

Note If this does not work right away, delete your python cache directory and try running the script again. Hope this helps!