Skip to content

Instantly share code, notes, and snippets.

@mmd-osm
Created January 23, 2024 18:50
Show Gist options
  • Select an option

  • Save mmd-osm/b61956bb4b92e9b37488189379b380c9 to your computer and use it in GitHub Desktop.

Select an option

Save mmd-osm/b61956bb4b92e9b37488189379b380c9 to your computer and use it in GitHub Desktop.
OAuth2 example as shell script
#!/bin/bash
# Register your own app under /oauth2/applications, use "urn:ietf:wg:oauth:2.0:oob" as redirect URL
CLIENT_ID="f4K_7SUtJluo94xj3hnN7NJ-U5ZtoOo87mpxuNKIxWs"
CLIENT_SECRET="pgnjYXX0jfSotaPavMswIhgEV3NQAQB1k8JqOd3y3bU"
DOMAIN="https://master.apis.dev.openstreetmap.org"
AUTHORIZATION_ENDPOINT=$(curl --silent $DOMAIN/.well-known/oauth-authorization-server | jq --raw-output '.authorization_endpoint')
TOKEN_ENDPOINT=$(curl --silent $DOMAIN/.well-known/oauth-authorization-server | jq --raw-output '.token_endpoint')
echo "Navigate to the following URL in your browser: " "$AUTHORIZATION_ENDPOINT?response_type=code&client_id=$CLIENT_ID&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=read_prefs"
read -p "Authorize the request on osm.org, then copy and paste the Authorization code: " CODE
ACCESS_TOKEN=$(curl --silent -X POST -d "grant_type=authorization_code&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$CODE&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob" "$TOKEN_ENDPOINT" | jq --raw-output '.access_token')
echo "Token: $ACCESS_TOKEN"
echo "Let's try to call some API endpoint:"
curl --silent -H "Authorization: Bearer $ACCESS_TOKEN" $DOMAIN/api/0.6/user/details.json
@mmd-osm
Copy link
Author

mmd-osm commented Jul 31, 2024

@DaveF63 : Yes, you need to register a new app to upload GPX traces:

image

  • Copy client secret + id that are shown after hitting "Register" and copy them into lines 4 + 5 above.
  • Change the endpoint in line 6 to https://www.openstreetmap.org
  • You can comment out line 14, then run the script once. It will display an access token. You only need to fetch this token once.
  • In your curl one liner, replace -u UName:PWord by -H "Authorization: Bearer $ACCESS_TOKEN" where access token is the same access token as returned by the previous step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment