Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import requests
  2.  
  3. site = "WEBSITE"
  4. app_id = 'YOUR APP ID'
  5. app_secret = 'YOUR APP SECRET'
  6. username = 'YOUR USERNAME'
  7. password = 'YOUR PASSWORD'
  8.  
  9. # Send a POST request to /oauth/token with the username and password
  10. payload = {
  11. 'client_id': app_id,
  12. 'client_secret': app_secret,
  13. 'grant_type': "password",
  14. 'username': username,
  15. 'password': password
  16. }
  17. print "POST %s/oauth/token, payload: %s" % (site, payload)
  18. response = requests.post(("%s/oauth/token" % site), payload)
  19. print "RESPONSE"
  20. print 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:
  33. token = response.json()["access_token"]
  34. headers = {"Authorization": "Bearer %s" % token}
  35.  
  36. # Make subsequent requests and include headers containing current token
  37. print "GET %s/users/edit.json, headers: %s" % (site, headers)
  38. print "RESPONSE"
  39. print requests.get(("%s/users/edit.json" % site), headers=headers).content
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement