def get_token(self): """ Acquire token for the site given the app client credentials. """ ## Use MSAL: # authority_url = self._LOGIN_URL + f"/{self._tenant_id}" # app = msal.ConfidentialClientApplication( # authority=authority_url, # client_id=self._client_id, # client_credential=self._client_secret, # validate_authority=False, # ) # token = app.acquire_token_for_client( # scopes=[self._client_id + f"/{self._SCOPE}"] # ) # if self._runtime.debug: # self._logger.debug(token) # return token if self._spconfig.subsite: self._url = self._spconfig.site + "/" + self._spconfig.subsite else: self._url = self._spconfig.site if self._runtime.debug: self._logger.debug(self._url) self._context_auth = AuthenticationContext(url=self._url) self._context_auth.acquire_token_for_app( client_id=self._spconfig.client, client_secret=self._spconfig.secret)
def connect_with_certificate(base_url, client_id, thumbprint, cert_path): """Creates authenticated SharePoint context via certificate credentials :param str cert_path: Path to A PEM encoded certificate private key. :param str thumbprint: Hex encoded thumbprint of the certificate. :param str client_id: The OAuth client id of the calling application. :param str base_url: Url to Site or Web """ def acquire_token(): tenant_info = get_tenant_info(base_url) authority_url = 'https://login.microsoftonline.com/{0}'.format(tenant_info['name']) auth_ctx = adal.AuthenticationContext(authority_url) resource = tenant_info['base_url'] with open(cert_path, 'r') as file: key = file.read() json_token = auth_ctx.acquire_token_with_client_certificate( resource, client_id, key, thumbprint) return TokenResponse(**json_token) ctx_auth = AuthenticationContext(url=base_url) ctx_auth.set_token(acquire_token()) ctx = ClientContext(base_url, ctx_auth) return ctx
def alterarGeradas(ID, sumGeradas): # Definindo parametros de conexão # ID e Senha criados pelo portal do sharepoint app_settings = { 'url': 'https://usinacoruripe.sharepoint.com/sites/FaturamentoTorta', 'client_id': 'c74022f1-d1b5-47e3-913f-84d7a98cf032', 'client_secret': 'qfHtOWl6YieOhGAAavzuzUDvuf9pl2ZvD/0JSqvZhsQ=' } # Chamando conexão com API Rest context_auth = AuthenticationContext(url=app_settings['url']) context_auth.acquire_token_for_app( client_id=app_settings['client_id'], client_secret=app_settings['client_secret']) ctx = ClientContext(app_settings['url'], context_auth) # Puxando valores da lista listaShare = ctx.web.lists.get_by_title("Autorizações") items = listaShare.get_items() ctx.load(items) ctx.execute_query() item = listaShare.get_item_by_id(ID) item.set_property('OData__x006d_kv6', sumGeradas) item.update() ctx.execute_query()
def __upload_target_reports(adm_sp_value: ADMSharepointValue) -> None: site_url = adm_sp_value.sharepoint_value.site_url + 'sites/' + adm_sp_value.sharepoint_value.site_id context_auth = AuthenticationContext(url=site_url) context_auth.acquire_token_for_app( client_id=adm_sp_value.sharepoint_value.client_id, client_secret=adm_sp_value.sharepoint_value.client_secret) ctx = ClientContext(site_url, context_auth) target_report_values = adm_sp_value.target_report_values # Create folder structure new_path = __create_folder(ctx, adm_sp_value) # Upload reports without segment by configuration if target_report_values is not None: for target_report_value in target_report_values: file_path = target_report_value.target_report_location if file_path is not None: files = glob.glob(file_path) __upload_file(files, new_path, ctx) # Upload reports with segment by configuration target_report_segment_values = target_report_value.target_report_segment_values if target_report_segment_values is not None: for target_report_segment_value in target_report_segment_values: segment_by_value = target_report_segment_value.target_report_segment_by_field_value segment_file_path = target_report_segment_value.target_report_segment_report_location segment_files = glob.glob(segment_file_path) __upload_file(segment_files, new_path, ctx, segment_by_value)
def __init__(self, host, port=0, auth=None, username=None, password=None, protocol='http', path=None): if not port: port = 443 if protocol == 'https' else 80 self.path = path or '' if not self.path.endswith('/'): self.path = '%s/' % self.path # oneDrive: need to split /site/ and path # in our config site is /personal/UF_OCX_msf_geneva_msf_org/ # path is /Documents/Tests/ self.baseurl = '{0}://{1}:{2}{3}/'.format( protocol, host, port, '/'.join(self.path.split('/')[0:3])) ctx_auth = AuthenticationContext(self.baseurl) #if len(self.path.split('/')) < 5: # self.path = '%sDocuments/' % self.path if ctx_auth.acquire_token_for_user(username, cgi.escape(password)): self.request = ClientRequest(ctx_auth) self.request.context = ClientContext(self.baseurl, ctx_auth) if not ctx_auth.provider.FedAuth or not ctx_auth.provider.rtFa: raise ConnectionFailed(ctx_auth.get_last_error()) else: raise ConnectionFailed(ctx_auth.get_last_error())
def execute(self, container, args): l = self.logger try: try: ctx_auth = AuthenticationContext(url=args.sharepointurl) if ctx_auth.acquire_token_for_user( username=args.sharepointusername, password=args.sharepointpass): ctx = ClientContext(args.sharepointurl, ctx_auth) filename = args.output path = args.sharepointfpath checkin_type = args.uplshr_checkintype return upload_data_to_sharepoint(path, filename, ctx, checkin_type, l) else: print(ctx_auth.get_last_error()) return except Exception as e: l.error("Exception happened boards search " + str(e)) except Exception as e: l.error("Exception happened during connection establishment " + str(e))
def setUpClass(cls): ctx_auth = AuthenticationContext(url=settings['url']) # ctx_auth.acquire_token_for_user(username=settings['user_credentials']['username'], # password=settings['user_credentials']['password']) ctx_auth.acquire_token_for_app(client_id=settings['client_credentials']['client_id'], client_secret=settings['client_credentials']['client_secret']) cls.context = ClientContext(settings['url'], ctx_auth)
def setUpClass(cls): super(TestCommunicationSite, cls).setUpClass() ctx_auth = AuthenticationContext(url=settings['url']) ctx_auth.acquire_token_for_user(username=settings['user_credentials']['username'], password=settings['user_credentials']['password']) cls.client = ClientContext(settings['url'], ctx_auth) cls.site_manager = SPSiteManager(cls.client)
def FetchMSPData(auth_creds, endpoint, entrylist): print("Fetching data for: ", endpoint) MSP_ROOT_URL = auth_creds[0] MSP_USERNAME = auth_creds[1] MSP_PASS = auth_creds[2] ctx_auth = AuthenticationContext(MSP_ROOT_URL) if ctx_auth.acquire_token_for_user(MSP_USERNAME, MSP_PASS): request = ClientRequest(ctx_auth) dataList = GetAllData( auth_creds, request, "/sites/pwa/_api/projectdata/{0}".format(endpoint) ) d = {} if len(entrylist) == 1 and entrylist[0] == "all": ParseAndAddAllToDict(dataList, d) else: for entry in entrylist: ParseAndAddToDict(dataList, entry, d) data_df = pd.DataFrame(d) print("Success data fetching for: ", endpoint) return data_df else: print(ctx_auth.get_last_error())
def test2_connect_with_app_principal_alt(self): context_auth = AuthenticationContext(url=test_site_url) context_auth.acquire_token_for_app( client_id=settings.get('client_credentials', 'client_id'), client_secret=settings.get('client_credentials', 'client_secret')) ctx = ClientContext(test_site_url, context_auth) self.assertIsInstance(ctx.authentication_context._provider, ACSTokenProvider)
def connect(self): self.ctx_auth = AuthenticationContext(self.site_url) self.connected = self.ctx_auth.acquire_token_for_user( self.username, self.password) self.request = ClientRequest(self.ctx_auth) print('Authentication was {}successful'.format( 'not ' if not self.connected else '')) return self.connected
def upload_one_file(base_url, folder_url, file_name, user_name, pwd): ctx_auth = AuthenticationContext(url=base_url) if not ctx_auth.acquire_token_for_user(username=user_name, password=pwd): print(ctx_auth.get_last_error()) exit() upload_binary_file(file_name, base_url, folder_url, ctx_auth) checkin_file(file_name, base_url, folder_url, ctx_auth)
def test6_read_site(self): url = "https://mediadev8.sharepoint.com/sites/team" ctx_auth = AuthenticationContext(url) ctx_auth.acquire_token_for_user( username=settings['user_credentials']['username'], password=settings['user_credentials']['password']) client = ClientContext(url, ctx_auth) client.load(client.web) client.execute_query()
def __init__(self, url, username, password, log): """ SharePoint object initialization. :param url: SharePoint destination URL (See DEFAULT_SP_URL above for example) :param username: Username to access SharePoint (See DEFAULT_SP_USERNAME above for example) :param log: where to send log messages :type log: logging.Logger """ self._url = url self._username = username self._password = password self.log = log # # Authenticate for this run # log.debug( "SharePoint: Acquiring AuthenticationContext {} for {}".format( url, username)) self._ctx_auth = AuthenticationContext(url=url) # # Looks like, at the time of this writing, the Office365-REST_Python-Client # library exception handling leaves a lot to be desired as their internal # method calls don't test the results of lower-level method calls in order to # percolate up problems they encounter. # # NOTE: Errors will actually go to the log file but not standard output for some # unknown reason # log.debug("SharePoint: Acquiring token from {} for {}".format( url, username)) self._ctx_auth.acquire_token_for_user(username=username, password=password) # # Kludgy test to see if a problem occurred # Admittedly this test will break if a different authentication provider is used # if self._ctx_auth.provider.token is None: _msg = "SharePoint: Unable to get SharePoint authorization. Error: {}".format( self._ctx_auth.get_last_error()) log.critical(_msg) raise ValueError(_msg) log.debug( "SharePoint: Authentication token obtained from {} for {}".format( url, username)) # # Get cookies # log.debug("SharePoint: Getting cookies from {} for {}".format( url, username)) self._ctx = ClientContext(self.url, self._ctx_auth) log.debug("SharePoint: Obtained ClientContext from {} for {}".format( url, username))
def __init__(self, settings_path): super().__init__(__version__, r"/home/downloader_settings", settings_path) context_auth = AuthenticationContext(url=site_url) context_auth.acquire_token_for_app( client_id=app_principal['client_id'], client_secret=app_principal['client_secret']) self.ctx = ClientContext(site_url, context_auth)
def setUpClass(cls): # Due to Outlook REST API v1.0 BasicAuth Deprecation # (refer https://developer.microsoft.com/en-us/office/blogs/outlook-rest-api-v1-0-basicauth-deprecation/) # NetworkCredentialContext class should be no longer utilized # ctx_auth = NetworkCredentialContext(username=settings['user_credentials']['username'], # password=settings['user_credentials']['password']) ctx_auth = AuthenticationContext(url=settings['tenant']) ctx_auth.acquire_token_password_grant(client_credentials=settings['client_credentials'], user_credentials=settings['user_credentials']) cls.client = OutlookClient(ctx_auth)
def connectToSharepoint(): server_url = "https://spitindia.sharepoint.com" site_url = server_url + "/sites/Darshan" username = "******" password = "******" ctx_auth = AuthenticationContext(site_url) ctx_auth.acquire_token_for_user(username, password) ctx = ClientContext(site_url, ctx_auth) saveFilesToElasticSearch(ctx, server_url)
def login(self): ctx_auth = AuthenticationContext(self.baseurl) if ctx_auth.acquire_token_for_user(self.username, cgi.escape(self.password)): self.request = ClientRequest(ctx_auth) self.request.context = ClientContext(self.baseurl, ctx_auth) if not ctx_auth.provider.FedAuth or not ctx_auth.provider.rtFa: raise ConnectionFailed(ctx_auth.get_last_error()) else: raise ConnectionFailed(ctx_auth.get_last_error())
def main(): ctx_auth = AuthenticationContext(webUrl) try: if ctx_auth.acquire_token_for_user(username, password): runRestQuery(ctx_auth, "web") else: print(ctx_auth.get_last_error()) showEndpoints(uniquEndpoints) except Exception as e: print(e)
def download(tenant_url, folder, file): file_path = "%s%s" % (folder, file) ctx_auth = AuthenticationContext(tenant_url) ctx_auth.acquire_token_for_user(username, password) # ctx_auth.with_client_certificate(tenant, client_id, thumbprint, cert_path) # ctx_auth.acquire_token_for_app(client_id, client_secret) ctx = ClientContext(tenant_url, ctx_auth) target = "%s%s%s" % (os.path.dirname( os.path.abspath(__file__)), "/import/", file) response = File.open_binary(ctx, file_path) with open(target, "wb") as local_file: local_file.write(response.content)
def authSharpoint() -> ClientContext: ''' Authenticates with sharepoint ''' ctx_auth = AuthenticationContext(env.sp_url) if ctx_auth.acquire_token_for_user(env.sp_username, env.sp_password): ctx = ClientContext(env.sp_url, ctx_auth) web = ctx.web ctx.load(web) ctx.execute_query() return(ctx) else: print(ctx_auth.get_last_error()) return(False)
def get_excel(): # Authentication ctx_auth = AuthenticationContext(r"https://Domain.sharepoint.com/") # Get Access Token ctx_auth.acquire_token_for_user("*****@*****.**", "Password") ctx = ClientContext(r'Exact URL of the file you want to download,ctx_auth) # Initiate Client Request Using Authentication request = ClientRequest(ctx_auth) # Create Options and create Headers options = RequestOptions(ctx.web.resource_url) options.set_header('Accept', 'application/json') options.set_header('Content-Type', 'application/json') # Start Request data = request.execute_request_direct(options) # get Result content in Json String Format myjsondump = json.dumps(data.content.decode('utf-8')) myjsonload = json.loads(myjsondump) curr_str = "" for load in myjsonload: curr_str = curr_str + load # extract "File Get URL" from json string start_index = curr_str.find(r'"FileGetUrl":"') + len(r'"FileGetUrl":"') url_text_dump = curr_str[start_index:] url_end_Index = url_text_dump.find(r'",') # File URL my_url = url_text_dump[:url_end_Index] my_url = my_url.strip(' \n\t') print(my_url) # get replace encoded characters qurl = my_url.replace(r"\u0026", "&") # get request resp = requests.get(url=qurl) url_data = requests.get(url=qurl) # Open an Write in Excel file VIP_Excel_File = open("Filename which you want to download.xlsx", mode="wb") VIP_Excel_File.write(url_data.content) print("Excel Sheet from Sharepoint Extracted Successfully")
def FetchMSPAvailableFields(auth_creds): MSP_ROOT_URL = auth_creds[0] MSP_USERNAME = auth_creds[1] MSP_PASS = auth_creds[2] ctx_auth = AuthenticationContext(MSP_ROOT_URL) if ctx_auth.acquire_token_for_user(MSP_USERNAME, MSP_PASS): request = ClientRequest(ctx_auth) options = RequestOptions(MSP_ROOT_URL + "/sites/pwa/_api/projectdata") data = request.execute_request_direct(options) root = ET.fromstring(data.content) l = set() for elem in root.iter(): if elem.text: if not (elem.text == "None" or elem.text == "Default"): l.add(elem.text) return l
def getDigest(url, username, password, site=None): # url = 'https://xebialabs.sharepoint.com/sites/TestCommunicationSite' if site: url = "%s/sites/%s" % (url, site) ctx_auth = AuthenticationContext(url) if ctx_auth.acquire_token_for_user(username, password): request = ClientRequest(ctx_auth) options = RequestOptions("{0}/_api/contextinfo".format(url)) options.method = 'POST' options.set_header('Accept', 'application/json') options.set_header('Content-Type', 'application/json') data = request.execute_request_direct(options) digest = json.loads(data.content)['FormDigestValue'] return digest else: return "Failed to acquire authentication token"
class SharePoint(): # Generating Auth Access auth = AuthenticationContext(sharepoint_base_url) auth.acquire_token_for_user(sharepoint_user, sharepoint_password) ctx = ClientContext(sharepoint_base_url, auth) web = ctx.web ctx.load(web) ctx.execute_query() print('Connected to SharePoint: ', web.properties['Title']) # Collecting files names from the folder def folder_details(ctx, folder_in_sharepoint): folder = ctx.web.get_folder_by_server_relative_url( folder_in_sharepoint) folder_names = [] sub_folders = folder.files ctx.load(sub_folders) ctx.execute_query() for each_folder in sub_folders: folder_names.append(each_folder.properties["Name"]) return folder_names # Passing auth ctx and folder path file_list = folder_details(ctx, folder_in_sharepoint) # Reading File from SharePoint Folder and saving it in local. print("Downloading 3P & Subledger file from sharepoint") for each_file in file_list: sharepoint_file = folder_in_sharepoint + each_file file_response = File.open_binary(ctx, sharepoint_file) print(file_response) with open(each_file, 'wb') as output_file: output_file.write(file_response.content) # Downloading the UTC Key file from the sharepoint print("Downloading UTC Key.xlsx file from sharepoint") sharepoint_file = utc_key_file_in_sharepoint + "UTC Key.xlsx" file_response = File.open_binary(ctx, sharepoint_file) print(file_response) with open("UTC Key.xlsx", 'wb') as output_file: output_file.write(file_response.content) # Processing the files and generating the output file. print("Processing the files and generating the output file") import automation # Uploading the output file in the output folder of sharepoint. with open(output_xlsx, 'rb') as content_file: print(output_xlsx) file_content = content_file.read() target_folder = ctx.web.get_folder_by_server_relative_url( '/CP/Shared%20Documents/Automation/Subledger%20Automation/Output/') print( "Uploading the output file in the output folder of the sharepoint." ) target_folder.upload_file(output_xlsx, file_content) ctx.execute_query()
def with_credentials(self, credentials): """ Assigns credentials :type credentials: UserCredential or ClientCredential """ self._auth_context = AuthenticationContext(url=self._base_url, credentials=credentials) return self
def __init__(self, base_url, auth_context=None): """ :param str base_url: Absolute Web or Site Url :param AuthenticationContext or None auth_context: Authentication context """ if base_url.endswith("/"): base_url = base_url[:len(base_url) - 1] if auth_context is None: self._auth_context = AuthenticationContext(url=base_url) else: self._auth_context = auth_context super(ClientContext, self).__init__() self.__web = None self.__site = None self._base_url = base_url self.__ctx_web_info = None self.__pending_request = None
def make_context(self): ctx_auth = AuthenticationContext(url=self.site_url) ctx_auth.acquire_token_for_user( username=self.authentication_dict['username'], password=self.authentication_dict['password']) #ctx_auth.acquire_token_for_user(username='******', password='******') self.context = ClientContext(self.site_url, ctx_auth) if self.context.auth_context.provider.error is not '': #the the login didnt work! returning_variable = False else: #the login worked! returning_variable = True return returning_variable
def __init__(self, base_url, auth_context=None): """ :type base_url: str :type auth_context: AuthenticationContext or None """ if base_url.endswith("/"): base_url = base_url[:len(base_url) - 1] if auth_context is None: self._auth_context = AuthenticationContext(url=base_url) else: self._auth_context = auth_context super(ClientContext, self).__init__() self.__web = None self.__site = None self._base_url = base_url self._contextWebInformation = None self._pendingRequest = ODataRequest(self, JsonLightFormat(ODataMetadataLevel.Verbose)) self._pendingRequest.beforeExecute += self._build_modification_query
def get_digest(self): # logger.info("Executing get_digest()") print("Executing get_digest()") # logger.info("url %s" % self.url) # logger.info("username %s" % self.username) # logger.info("password %s" % self.password) ctx_auth = AuthenticationContext(self.tokenServer, self.url) if ctx_auth.acquire_token_for_user(self.username, self.password): request = ClientRequest(ctx_auth) options = RequestOptions("{0}/_api/contextinfo".format(self.url)) options.method = 'POST' options.set_header('Accept', 'application/json') options.set_header('Content-Type', 'application/json') data = request.execute_request_direct(options) if "odata.error" in data.content: self.throw_error(data.content['odata.error']['message']['value']) else: return json.loads(data.content)['FormDigestValue'] else: self.throw_error("Failed to acquire authentication token for %s" % self.url)