首页 > 解决方案 > 我正在尝试使用 python 脚本将文件写入我的公司共享点,但我无法让它工作

问题描述

所以我试图让我的 python 脚本写入我的共享点,但我似乎无法让它工作。我要做的是将特定日期的数据集保存到共享点。这部分代码可以保存在我的本地机器上。现在我正试图让它在我需要它的特定文件夹中写入我的共享点这是代码:

 # Set Login Info
    username = 'xxx'
    password = 'xxx'
    site_name = 'xxx'
    base_path = 'xxx.sharepoint.com'
    nested_folder = 'Shared%20Documents/General/Performance/Dashboard' 
    file_name = final_path #when your file in the same directory

    # Obtain auth cookie
    authcookie = Office365(base_path, username=username, password=password).GetCookies()
    session = requests.Session()
    session.cookies = authcookie
    session.headers.update({'user-agent': 'python_bite/v1'})
    session.headers.update({'accept': 'application/json;odata=verbose'})
    
    session.headers.update({'X-RequestDigest': 'FormDigestValue'})
    response = session.post(url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + nested_folder + "')/Files/add(url='a.txt',overwrite=true)",
                         data="")
    session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})

    # Upload file
    with open(file_name, 'rb') as file_input:
        try:
            response = session.post(
                url=base_path + "/sites/" + site_name + f"/_api/web/GetFolderByServerRelativeUrl('" + nested_folder + "')/Files/add(url='"
                + file_name + "',overwrite=true)",

                data=file_input)
            print("response: ", response.status_code) #it returns 200
            if response.status_code == '200':
                print("File uploaded successfully")
        except Exception as err:
            print("Something went wrong: " + str(err))

    print('File Uploaded Successfully')

This is the error code I get:

> <pi>Exception: ('Error authenticating against Office 365. Was not able to find an error code. Here is the SOAP response from Office 365', b'<?xml version="1.0" encoding="utf-8"?><S:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header><wsa:Action S:mustUnderstand="1" wsu:Id="Action">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</wsa:Action><wsa:To S:mustUnderstand="1" wsu:Id="To">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security S:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Created>2021-07-13T16:11:53.3294256Z</wsu:Created><wsu:Expires>2021-07-13T16:16:53.3294256Z</wsu:Expires></wsu:Timestamp></wsse:Security></S:Header><S:Body xmlns:S="http://www.w3.org/2003/05/soap-envelope"><wst:RequestSecurityTokenResponse xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust"><wsp:AppliesTo><wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Address>https://tmobileusa.sharepoint.com</wsa:Address></wsa:EndpointReference></wsp:AppliesTo><psf:pp xmlns:psf="http://schemas.microsoft.com/Passport/SoapServices/SOAPFault"><psf:reqstatus>0x8004882c</psf:reqstatus><psf:errorstatus>0x80045b00</psf:errorstatus></psf:pp></wst:RequestSecurityTokenResponse></S:Body></S:Envelope>')
</pi>
Details:
    DataSourceKind=Python
    DataSourcePath=Python
    Message=Python script error.
<pi>Exception: ('Error authenticating against Office 365. Was not able to find an error code. Here is the SOAP response from Office 365', b'<?xml version="1.0" encoding="utf-8"?><S:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header><wsa:Action S:mustUnderstand="1" wsu:Id="Action">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</wsa:Action><wsa:To S:mustUnderstand="1" wsu:Id="To">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security S:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Creat...
    ErrorCode=-2147467259

标签: pythonxmlweb-servicessharepointsoap

解决方案


推荐阅读