Python MySQL 365 Rest API -get_folder_by_server_relative_url .add函数出错

92vpleto  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(75)

我有一个脚本,只要我能记得,它工作得很好,然而,最近它出现了以下错误:

TypeError   Traceback (most recent call last)
Cell In[19], line 53
     50 info.url = target_file
     51 info.overwrite = True
---> 53 result_file = context.web.get_folder_by_server_relative_url(target_folder).files.add(info)
     54 context.execute_query()
     55 print('File {0} has been uploaded successfully'.format(result_file.properties['ServerRelativeUrl']))

TypeError: add() missing 1 required positional argument: 'content'

我不知道如何解决这个问题,也不知道哪个位置论证可能被遗漏了。任何帮助是高度赞赏,因为我一直试图弄清楚这一点一整天都无济于事:(

import logging
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from office365.sharepoint.files.file_creation_information import FileCreationInformation
import os

###### Logging setup ####################################################
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
#########################################################################

# Site settings that won't change
site_root = '/Sites/SampleSite'
site_url = 'https://REDACTED.sharepoint.com' + site_root

# Specify the file to upload
path = "./SampleFile1.xlsx"
target_folder = "Shared%20Documents/UserFiles"
target_file = "SampleFile1.xlsx"

# SharePoint app-only principal - credentials    
client_id     = 'REDACTED'
client_secret = 'REDACTED'


context_auth = AuthenticationContext(site_url)
print('check')
if context_auth.acquire_token_for_app(client_id, client_secret):

        print('check')
        # Looks like we have a valid access token.  Download a file
        context = ClientContext(site_url, context_auth)
        #print(context)
        with open(path, 'rb') as content_file:
             file_content = content_file.read()

        info = FileCreationInformation()
        info.content = file_content
        info.url = target_file
        info.overwrite = True

        result_file = context.web.get_folder_by_server_relative_url(target_folder).files.add(info)
        context.execute_query()
        print('File {0} has been uploaded successfully'.format(result_file.properties['ServerRelativeUrl']))

else:
    # No token or error was returned
    logger.critical("Authentication failed")
    try:
        logger.critical(context_auth.get_last_error())
    except:
        logger.critical("Reason for failure is unknown")
zqry0prt

zqry0prt1#

您正在阅读文件内容并在info.content属性中设置它,但是如果库希望您将内容作为参数传递给add方法,

result_file = context.web.get_folder_by_server_relative_url(target_folder).files.add(target_file, file_content, True)

相关问题