#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name DownTo = itsm.getParameter('DownTo') #Here mention the path where the application to download client_id = itsm.getParameter('client_id') #Please provide the AD client ID client_secret = itsm.getParameter('client_secret') #Please provide the AD secret key office_url = itsm.getParameter('office_url') #Please provide your office 365 domain eg. xxxxx.onmicrosoft.com userPrincipalName = itsm.getParameter('User_Mail_Id') #Please provide the user mail id eg. xxxxx@xxxxx.onmicrosoft.com onedrive_file_name = itsm.getParameter('file_name') #Please provide the sharepoint file name eg. xxxxx.doc import ctypes class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) import subprocess with disable_file_system_redirection(): import urllib import os def downloadFile(DownTo, fromURL, Filename): try: fileName = Filename DownTo = os.path.join(DownTo, fileName) with open(DownTo, 'wb') as f: f.write(urllib.urlopen(fromURL).read()) if os.path.isfile(DownTo): return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1000) except: return 'Please Check URL or Download Path!' import json import urllib2 def get_access_token(): try: import urllib params = urllib.urlencode({ 'client_id': client_id, 'client_secret': client_secret,'resource': 'https://graph.microsoft.com','grant_type': 'client_credentials'}) f = urllib.urlopen("https://login.microsoftonline.com/%s/oauth2/token"%(office_url), params) json_data = json.loads(f.read()) data = json_data.get('access_token') return data except Exception as err : print err def get_user_id(): try: from urllib2 import urlopen, Request access_token = get_access_token() request = Request("https://graph.microsoft.com/v1.0/users") request.add_header('Authorization', 'Bearer %s' %(access_token)) request.add_header('accept', 'application/json') request.add_header('grant_type', 'client_credentials') response = urlopen(request) json_data = json.loads(response.read()) user_id = '' for rec in json_data.get('value'): if rec.get('userPrincipalName') == userPrincipalName: user_id = rec.get('id') data = user_id return {'user_id' : data, 'access_token' : access_token} except Exception as err : print err def check_file_or_folder(): try: from urllib2 import urlopen, Request get_acc_user_id = get_user_id() request = Request("https://graph.microsoft.com/v1.0/users/%s/drive/root:/%s"%(get_acc_user_id.get('user_id'), onedrive_file_name)) request.add_header('Authorization', 'Bearer %s' % (get_acc_user_id.get('access_token'))) request.add_header('accept', 'application/json') request.add_header('grant_type', 'client_credentials') response = urlopen(request) json_data = json.loads(response.read()) #for record in json_data.get('value'): if json_data.get('@microsoft.graph.downloadUrl'): downloadFile(DownTo,json_data.get('@microsoft.graph.downloadUrl'), json_data.get('name')) print "File Downloaded" except Exception as err : print err check_file_or_folder()