Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

'str' object is not callable for `ClientContext for execute_query()

Open StanChan03 opened this issue 1 year ago • 3 comments

Whenever I try to run this I get the str object is not callable for ClientContext in which execute_query is not able to run. I even passed the acquire token as a callback function, but I still get the same error. Any` help is much appreciated.

`def acquire_token(): client_id = 'xxxx-xxxx-xxxx' secret_id = 'xxxx-xxxx-xxxx' tenent_id = 'xxxx-xxxx-xxxx authority_url = f'https://login.microsoftonline.com/{tenent_id}' scope = ['https://graph.microsoft.com/.default'] app = msal.ConfidentialClientApplication(client_id, authority=authority_url, client_credential=secret_id) result = app.acquire_token_silent(scopes=scope, account=None) if not result: result = app.acquire_token_for_client(scopes=scope) if 'access_token' in result: return result['access_token'] else: logging.error("Authentication failed.") raise Exception("Authentication failed")

def upload_sharepoint(pdf_content, location_name, new_start_date, new_end_date): sharepoint_url = 'https://teslamotorsinc.sharepoint.com/sites/my_site' folder = '/sites/my_site/Shared Documents/some_folder' archive_folder = folder + '/Archive' try: # Authenticate with SharePoint using access token ctx = ClientContext(sharepoint_url).with_access_token(acquire_token()) target_folder = ctx.web.get_folder_by_server_relative_url(folder) archive_folder_obj = ctx.web.get_folder_by_server_relative_url(archive_folder) # List and move files older than a month to the archive folder try: files_list = target_folder.files ctx.load(files_list) print('Loading files...') ctx.execute_query() print('Files successfully loaded') one_month_ago = datetime.now() - timedelta(days=30) for file_item in files_list: file_name = file_item.properties["Name"] file_time_created = file_item.properties["TimeCreated"] print(f"File item: {file_item.properties}") # Parse the TimeCreated string to datetime file_time_created = datetime.strptime(file_time_created[:19], "%Y-%m-%dT%H:%M:%S") if file_time_created < one_month_ago: file_item.moveto(archive_folder + '/' + file_name, 1) print('file is moved') ctx.execute_query() print('Failed to move') logging.info(f'File {file_name} has been moved to archive folder.') except Exception as e: logging.error(f"Failed to move old files: {e}") # Upload new PDF file try: print('Uploading new PDF file...') target_folder.upload_file(f'somefile_name_{new_start_date}to{new_end_date}.pdf', pdf_content) print('Added query')
ctx.execute_query() print('failed_to upload')
logging.info(f'PDF has been uploaded to {folder}') except Exception as e: logging.error(f"Failed to upload new PDF: {e}") except Exception as e: logging.error(f'Failed_Authentication: {e}')`

StanChan03 avatar Jul 13 '24 08:07 StanChan03

Looks like a duplicate of https://github.com/vgrem/Office365-REST-Python-Client/issues/877

taatuut avatar Aug 05 '24 17:08 taatuut

Did you ever find a solution for this? I'm having the same issue.

YourMayor avatar Aug 15 '24 14:08 YourMayor

having the same issue

jpmzaty avatar Dec 20 '24 10:12 jpmzaty