Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR 403: Forbidden Request #76

Open
g-aaroncruz opened this issue Feb 28, 2024 · 2 comments
Open

ERROR 403: Forbidden Request #76

g-aaroncruz opened this issue Feb 28, 2024 · 2 comments

Comments

@g-aaroncruz
Copy link

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
Copy link

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
Copy link

JustinKottinger commented Mar 5, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants