· 9 years ago · Nov 02, 2016, 01:02 AM
1import requests
2
3site = "WEBSITE"
4app_id = 'YOUR APP ID'
5app_secret = 'YOUR APP SECRET'
6username = 'YOUR USERNAME'
7password = 'YOUR PASSWORD'
8
9# Send a POST request to /oauth/token with the username and password
10payload = {
11 'client_id': app_id,
12 'client_secret': app_secret,
13 'grant_type': "password",
14 'username': username,
15 'password': password
16}
17print "POST %s/oauth/token, payload: %s" % (site, payload)
18response = requests.post(("%s/oauth/token" % site), payload)
19print "RESPONSE"
20print response.content
21
22# response will be a chunk of JSON looking like
23# {
24# "access_token":"xxx",
25# "token_type":"bearer",
26# "expires_in":null,
27# "refresh_token":null,
28# "scope":"write"
29# }
30
31# Store the token (access_token) in your app. You can now use it to make authorized
32# requests on behalf of the user, like retrieving profile data:
33token = response.json()["access_token"]
34headers = {"Authorization": "Bearer %s" % token}
35
36# Make subsequent requests and include headers containing current token
37print "GET %s/users/edit.json, headers: %s" % (site, headers)
38print "RESPONSE"
39print requests.get(("%s/users/edit.json" % site), headers=headers).content