Skip to content

Instantly share code, notes, and snippets.

@kueda
Created July 31, 2015 22:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kueda/53ef93159c64de96ddc2 to your computer and use it in GitHub Desktop.
Save kueda/53ef93159c64de96ddc2 to your computer and use it in GitHub Desktop.
iNaturalist API Resource Owner Password Credentials Flow Example (Python)
import requests
site = "https://www.inaturalist.org"
app_id = 'YOUR APP ID'
app_secret = 'YOUR APP SECRET'
username = 'YOUR USERNAME'
password = 'YOUR PASSWORD'
# Send a POST request to /oauth/token with the username and password
payload = {
'client_id': app_id,
'client_secret': app_secret,
'grant_type': "password",
'username': username,
'password': password
}
print "POST %s/oauth/token, payload: %s" % (site, payload)
response = requests.post(("%s/oauth/token" % site), payload)
print "RESPONSE"
print response.content
print
# response will be a chunk of JSON looking like
# {
# "access_token":"xxx",
# "token_type":"bearer",
# "expires_in":null,
# "refresh_token":null,
# "scope":"write"
# }
# Store the token (access_token) in your app. You can now use it to make authorized
# requests on behalf of the user, like retrieving profile data:
token = response.json()["access_token"]
headers = {"Authorization": "Bearer %s" % token}
print "GET %s/users/edit.json, headers: %s" % (site, headers)
print "RESPONSE"
print requests.get(("%s/users/edit.json" % site), headers=headers).content
print
@abubelinha
Copy link

abubelinha commented Jul 31, 2022

@kueda thanks for this example.
I think this is not working anymore.
If you wish to update it, see some comments at pyinaturalist issue #403

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