Skip to content

Instantly share code, notes, and snippets.

@pnorman
Created January 23, 2024 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnorman/19c103add9fcc6b9ee8a5792d2598ef4 to your computer and use it in GitHub Desktop.
Save pnorman/19c103add9fcc6b9ee8a5792d2598ef4 to your computer and use it in GitHub Desktop.
from requests_oauthlib import OAuth2Session
client_id = r'0IrEz7VIm6zis60pP1jKW5ZXdLpVP0yojuG8Hy45z1o'
client_secret = r'Iejv415Rd-E0YqY2knXOrL8a1cmnJ-LtKMxPdmuvkyI'
redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
scope = ["read_prefs"]
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(f"Visit {authorization_url} and paste the code in")
code = input()
token = oauth.fetch_token('https://www.openstreetmap.org/oauth2/token',
code=code,
client_secret=client_secret)
print(token)
# save the token in a file somewhere or something
prefs = oauth.request(method='GET',url='https://www.openstreetmap.org/api/0.6/user/preferences')
print(prefs.text)
$ ./auth_example.py
Visit https://www.openstreetmap.org/oauth2/authorize?response_type=code&client_id=0IrEz7VIm6zis60pP1jKW5ZXdLpVP0yojuG8Hy45z1o&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=read_prefs&state=vlG4PUbPTexvFFc9Z31oscxFcs5gN2 and paste the code in
oue_9LNZLk-OqS46GRjUMPZrAWi04kbapo26y5SSJ_g
{'access_token': 'DIdA2CDA-0pe6rysQLGlHx1tux_42V3lg3MELhoKseE', 'token_type': 'Bearer', 'scope': ['read_prefs'], 'created_at': 1706004195}
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<preferences>
<preference k="diary.default_language" v="en"/>
<preference k="gps.trace.visibility" v="identifiable"/>
</preferences>
</osm>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment