metaodi / osmapi

Python wrapper for the OpenStreetMap API
http://osmapi.metaodi.ch/
GNU General Public License v3.0
212 stars 41 forks source link

Oauth authentication - Username/Password missing #158

Closed docentYT closed 7 months ago

docentYT commented 7 months ago

I am trying to run the example code from readme, but I get an error: osmapi.errors.UsernamePasswordMissingError: Username/Password missing. I am using the dev server as it is in the example.

metaodi commented 7 months ago

I'll look into it. Maybe #157 is needed (?)

metaodi commented 7 months ago

@docentYT what version of osmapi are you using? The error you describe will happen in osmapi < v3.0.0, please make sure to use the latest version and check again.

docentYT commented 7 months ago

I am using version 4.0.0. After pasting the authorization code, I get errors about missing username or password.

metaodi commented 7 months ago

I can't reproduce this behavior.

The example code sets auth on the custom HTTP session, which is then passed to the OsmApi constructor:

# create a new requests session using the OAuth authorization
auth_code = input("Paste the authorization code here: ")
auth = OAuth2AuthorizationCodeAuth(
    oauth2client,
    auth_code,
    redirect_uri=redirect_uri,
)
oauth_session = requests.Session()
oauth_session.auth = auth

# use the custom session
api = osmapi.OsmApi(
    api="https://api06.dev.openstreetmap.org",
    session=oauth_session
)

The only place in OsmApi where UsernamePasswordMissingError is raised is in http.py if a call requires authentication (auth) and no authentication is set (self._auth):

https://github.com/metaodi/osmapi/blob/be9549d79b4d50c2b8cc87d27e9449bf473479f7/osmapi/http.py#L68-L69

self._auth is set in the constructor of OsmApiSession:

https://github.com/metaodi/osmapi/blob/be9549d79b4d50c2b8cc87d27e9449bf473479f7/osmapi/http.py#L22-L24

And OsmApiSession is created in the constructor of OsmApi using the session parameter:

https://github.com/metaodi/osmapi/blob/be9549d79b4d50c2b8cc87d27e9449bf473479f7/osmapi/OsmApi.py#L145-L152

Am I missing something?

metaodi commented 7 months ago

@docentYT okay I found it, it's been too long since I wrote this code. It's actually part of code I wrote after Release 4.0.0, I should probably release 4.1.0 soon to make this available.

Sorry about that!

metaodi commented 7 months ago

@docentYT I just release osmapi 4.1.0, which should resolve your issue.