OAuth2 (With Python)

I am trying to get an OAuth2 token with python. I am using the foloowing code:

payload = {
    "grant_type": "authorization_code",
    "client_id": CLIENT_ID,
    "client_secret": CLIENT_SECRET,
    "code": code,
    "redirect_uri": redirect_uri,
r = requests.post("https://www.openstreetmap.org/oauth2/token", data=payload)

(Where code is returned from the first auth part.)

Instead of the token I keep getting HTTP 401 responses with:

{"error":"invalid_client","error_description":"Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method."}

Any help would be appreciated!

Thank you!

have you followed OAuth - OpenStreetMap Wiki ?

Yes. I did. I get a url with code+state params

Habu you registered as OAuth consumer?

  1. Register an OAuth2 application in OSM with redirect uri ‘’ and store the application id and secret
  2. run this code
from requests_oauthlib import OAuth2Session

# Set up your OAuth2 credentials - localhost
client_id = <client-id>
client_secret = <client-secret>
redirect_uri = ''
scope = ["read_gpx","write_gpx","write_api"]

oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope)

authorization_url, state = oauth.authorization_url('https://www.openstreetmap.org/oauth2/authorize')
print ("Please visit:\n" + authorization_url)
print ("\n\n")
authorization_response = "https://"+input('Enter the redirect url:\n')
token = oauth.fetch_token(
print ("\n\n")
print ("Access token is:")

I unfortunately don’t know stuff about Python, haven’t used it in over a year, but have once implemented OpenStreetMap OAuth for @Zverik’s simple-revert:

As this, at least for the time I implemented it, worked, I’d try starting from there.