Advertisement
Guest User

Untitled

a guest
May 25th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import adal
  2. import urllib
  3. import requests
  4. import urllib2
  5.  
  6. ## set variables
  7. username = 'curtis@tenant.onmicrosoft.com'
  8. password = 'OFoarry8Oe$'
  9. authorization_url = 'https://login.windows.net/tenant.onmicrosoft.com' # Authority
  10. redirect_uri = 'https://login.microsoftonline.com/login.srf'
  11. client_id = 'dcbf844f-d2c3-42d1-8a7d-0f838f57899a' # Client id
  12.  
  13. ## use ADAL to create token response
  14. token_response = adal.acquire_token_with_username_password(
  15. authorization_url,
  16. username,
  17. password
  18. )
  19. ## endpoints discovery
  20. ## https://api.office.com/discovery/v2.0/me/allServices
  21.  
  22. ## create refresh token and save it to use later
  23. refresh_token = token_response['refreshToken']
  24. refresh_token_file = open('refresh_token.txt', 'w')
  25. refresh_token_file.write(refresh_token)
  26. refresh_token_file.close()
  27.  
  28. ## get saved refresh token and use it to get new token response
  29. refresh_token = open('refresh_token.txt', 'r').read()
  30. token_response = adal.acquire_token_with_refresh_token(authorization_url, str(refresh_token))
  31.  
  32. ## get access_token from token response
  33. access_token = token_response.get('accessToken')
  34. headers = {'Authorization':'BEARER ' + str(access_token)}
  35.  
  36. print access_token
  37.  
  38. ## download file
  39. file_url = 'https://tenant.sharepoint.com/_api/v1.0/files/root:/myfoldername/myfilename.csv:/content'
  40. r = requests.get(file_url, headers=headers)
  41. print r.text
  42.  
  43. {"error":"invalid_client","error_description":"Invalid audience Uri 'https://management.core.windows.net/'."}
  44.  
  45. https://tenant.sharepoint.com/Shared%20Documents/myfoldername/myfilename.csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement