Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import adal import urllib import requests
  2. ## set variables
  3. username = ''
  4. password = ''
  5. authorization_url = 'h
  6.  
  7. # aka Authority
  8. redirect_uri = 'https://login.microsoftonline.com/login.srf'
  9. # from Azure AD application
  10. client_id = '1ec02d31-55b8-47b8-bf19-5ce082ba121c'
  11. # from Azure AD application
  12. file_url = 'https://contoso.sharepoint.com/_api/v2.0/drive/root:/myfoldername/myfilename.csv:/content'
  13.  
  14. ## use ADAL to create token response
  15. token_response = adal.acquire_token_with_username_password(
  16. authorization_url,
  17. username,
  18. password
  19. )
  20.  
  21. ## Use ADAL to create refresh token and save as text file to reuse
  22. refresh_token = token_response['refreshToken']
  23. refresh_token_file = open('refresh_token.txt', 'w')
  24. refresh_token_file.write(refresh_token) refresh_token_file.close()
  25.  
  26. ## Get saved refresh token and use it to get new token response
  27. refresh_token = open('refresh_token.txt', 'r').read()
  28. token_response =
  29. adal.acquire_token_with_refresh_token(
  30. authorization_url, str(refresh_token))
  31.  
  32. ## get access_token from token response JSON string
  33. access_token = token_response.get('accessToken')
  34. ## create http header to send access token to authenticate
  35. headers = {'Authorization':'BEARER ' + str(access_token)}
  36. ## example to upload file
  37. upload_file = requests.put(file_url,
  38. data = open('myfilename.csv', 'rb'), headers=headers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement