Udi
(Udi)
May 28, 2023, 1:11pm
1
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!
Udi
(Udi)
May 28, 2023, 4:59pm
3
Yes. I did. I get a url with code+state params
Habu you registered as OAuth consumer?
rkcosm
(Rkcosm)
June 9, 2023, 9:47am
5
Register an OAuth2 application in OSM with redirect uri ‘http://127.0.0.1:8000 ’ and store the application id and secret
run this code
from requests_oauthlib import OAuth2Session
# Set up your OAuth2 credentials - localhost
client_id = <client-id>
client_secret = <client-secret>
redirect_uri = 'http://127.0.0.1:8000'
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(
'https://www.openstreetmap.org/oauth2/token',
authorization_response=authorization_response,
client_secret=client_secret)
print ("\n\n")
print ("Access token is:")
print(token["access_token"])
Emilius123
(Emil Linus Albrecht)
June 16, 2023, 10:09pm
6
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 :
#Common things for oauth with OSM
#Written for API 0.6's OAuth
import os
import json
import requests_oauthlib as oauth
CONFIG_NAME = os.path.join(os.path.dirname(__file__), 'authConfig.json')
API_ENDPOINT = 'https://www.openstreetmap.org/api/0.6/'
OAUTH_ENDPOINT = 'https://www.openstreetmap.org/oauth/'
#Save the OAuth details to file
def writeAuthConfig(client_key: str, client_secret: str, token: str, token_secret: str) -> bool:
config = {
"client_key": client_key,
"client_secret": client_secret,
"token": token,
"token_secret": token_secret
}
#Save to file
This file has been truncated. show original
As this, at least for the time I implemented it, worked, I’d try starting from there.