简体   繁体   English

Sharepoint 文件上传与 API 使用 Python

[英]Sharepoint file upload with API using Python

What I want is uploading file to specific directory in the sharepoint via API and using python.我想要的是通过 API 并使用 python 将文件上传到 sharepoint 中的特定目录。

Here is my code:这是我的代码:

import requests
from shareplum import Office365
from config_s import config_s

# get data from config_suration
username = config_s['sp_user']
password = config_s['sp_password']
site_name = config_s['sp_site_name']
base_path = config_s['sp_base_path']
doc_library = config_s['sp_doc_library']

file_name = "/Users/Metatrons/Desktop/AutoReport_Structure/test.csv"

# 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'})#FormDigestValue
response = session.post( url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
                         data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})

# perform the actual upload
with open( file_name, 'rb') as file_input:
    try: 
        response = session.post( 
            url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='" 
            + file_name + "',overwrite=true)",
            data=file_input)
        print(response)
        print(response.headers)
        #print(response.headers['X-RequestDigest'])
    except Exception as err: 
        print("Some error occurred: " + str(err))
        print("What the  FUUUUUUUUUUUUCK")

And I got the message that我得到了这样的信息

** Response [400] ** 响应 [400]

{'Cache-Control': 'private, max-age=0', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json;odata=verbose;charset=utf-8', 'Expires': 'Mon, 27 Apr 2020 05:58:55 GMT', 'Last-Modified': 'Tue, 12 May 2020 05:58:55 GMT', 'P3P': 'CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"', 'Set-Cookie': 'rtFa=v+WfHj06vOlD59P0qVwsXStksrafJI486yruddHryI0mOTk4NkVEMTItMjc3MS00RTY2LThCNUUtQTIwODBENkIyN0U3bZhIUWlknsxj7X5UIUNUvMimHWhZd/gZoFgaZg5Dr4Wm/QbeuGwyrg5iFuo9Pt5IGannrRBEyxwiSaUwPrgHhkRrlkAnf6N6+3tS7eSnl7ebzMkkIar20aS/LCZRs8Z/UNFD/pj8ptYvwspbCDNz/m/gA5tdhlURmKCeqN94Ry16SR+7KjuAFnrfNHEMYjHsh9CotruRb4jQUEQOkVNLuHIabuq3qNJLWv4CQA/JM3LHW528rf/tR64zzKHRIqfdziTzCRnndQP9+5YzMk0jFFfslLjmjvMMO19yQ1m70+T8l/qyQk5GOMkFcw3Zw9hihPpC29SDuTts46lDBVWqOkUAAAA=; {'Cache-Control': 'private, max-age=0', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json;odata=verbose;charset=utf-8', '过期':'星期一,2020 年 4 月 27 日 05:58:55 GMT','最后修改':'星期二,2020 年 5 月 12 日 05:58:55 GMT','P3P':'CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"', 'Set-Cookie': 'rtFa=v+WfHj06vOlD59P0qVwsXStksrafJI486yruddHryI0mOTk4NkVEMTItMjc3MS00RTY2LThCNUUtQTIwODBENkIyN0U3bZhIUWlknsxj7X5UIUNUvMimHWhZd/gZoFgaZg5Dr4Wm/QbeuGwyrg5iFuo9Pt5IGannrRBEyxwiSaUwPrgHhkRrlkAnf6N6+3tS7eSnl7ebzMkkIar20aS/LCZRs8Z/UNFD/pj8ptYvwspbCDNz/m/ gA5tdhlURmKCeqN94Ry16SR+7KjuAFnrfNHEMYjHsh9CotruRb4jQUEQOkVNLuHIabuq3qNJLWv4CQA/JM3LHW528rf/tR64zzKHRIqfdziTzCRnndQP9+5YzMk0jFFfslLjmjvMMO19yQ1m70+T8l/qyQk5GOMkFcw3Zw9hihPpC29SDuTts46lDBVWqOkUAAAA=; domain=sharepoint.com;域=sharepoint.com; path=/;路径=/; SameSite=None;相同站点=无; secure;安全的; HttpOnly, FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A++; HttpOnly, FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A++; path=/;路径=/; SameSite=None;相同站点=无; secure;安全的; HttpOnly', 'X-SharePointHealthScore': '0', 'X-SP-SERVERSTATE': 'ReadOnly=0', 'DATASERVICEVERSION': '3.0', 'SPClientServiceRequestDuration': '54', 'X-AspNet-Version': '4.0.30319', 'SPRequestGuid': '3c9a519f-00ce-a000-89f2-6080347e58ff', 'request-id': '3c9a519f-00ce-a000-89f2-6080347e58ff', 'MS-CV': 'n1GaPM4AAKCJ8mCANH5Y/w.0', 'X-RequestDigest': '0x26237158F71B34858BF0EB713CDA5A9412C28A5AC95FF342B326F7B8DE04DD93AA4E233004444AC0D7BF427E2D05BDA4B526CB2BF8BB32D73F8917E9CC88F495,12 May 2020 05:58:56 -0000', 'Strict-Transport-Security': 'max-age=31536000', 'X-FRAME-OPTIONS': 'SAMEORIGIN', 'X-Powered-By': 'ASP.NET', 'MicrosoftSharePointTeamServices': '16.0.0.20029', 'X-Content-Type-Options': 'nosniff', 'X-MS-InvokeApp': '1; HttpOnly','X-SharePointHealthScore':'0','X-SP-SERVERSTATE':'ReadOnly=0','DATASERVICEVERSION':'3.0','SPClientServiceRequestDuration':'54','X-AspNet-Version' :'4.0.30319','SPRequestGuid':'3c9a519f-00ce-a000-89f2-6080347e58ff','request-id':'3c9a519f-00ce-a000-89f2-6080347e58ff','MS-CV':'n51GaPM4AAKCJ8 w.0', 'X-RequestDigest': '0x26237158F71B34858BF0EB713CDA5A9412C28A5AC95FF342B326F7B8DE04DD93AA4E233004444AC0D7BF427E2D05BDA4B526CB2BF8BB32D73F8917E9CC88F495,12 May 2020 05:58:56 -0000', 'Strict-Transport-Security': 'max-age=31536000', 'X-FRAME-OPTIONS': “SAMEORIGIN”、“X-Powered-By”:“ASP.NET”、“MicrosoftSharePointTeamServices”:“16.0.0.20029”、“X-Content-Type-Options”:“nosniff”、“X-MS-InvokeApp”: '1; RequireReadOnly', 'X-MSEdge-Ref': 'Ref A: DFB58EAF5C2D4ECB8027CD2E3348A80C Ref B: TPE30EDGE0216 Ref C: 2020-05-12T05:58:55Z', 'Date': 'Tue, 12 May 2020 05:58:55 GMT'} ** RequireReadOnly', 'X-MSEdge-Ref': 'Ref A: DFB58EAF5C2D4ECB8027CD2E3348A80C Ref B: TPE30EDGE0216 Ref C: 2020-05-12T05:58:55Z', 'Date': 'Tue, 12 May 2020 05:58:55 GMT '} **

Plz help me to figure out this problem, thx!请帮我解决这个问题,谢谢!

Please take a reference of below sample, i have tested it on my SPO environment.请参考以下示例,我已经在我的 SPO 环境中对其进行了测试。

import requests
from shareplum import Office365
from config import config

# get data from configuration
username = config['sp_user']
password = config['sp_password']
site_name = config['sp_site_name']
base_path = config['sp_base_path']
doc_library = config['sp_doc_library']

file_name = "test.html"
# 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'})

# dirty workaround.... I'm getting the X-RequestDigest from the first failed call
session.headers.update({'X-RequestDigest': 'FormDigestValue'})
response = session.post( url=base_path + "/sites/" + site_name + 
"/_api/web/GetFolderByServerRelativeUrl('" + doc_library + 
"')/Files/add(url='a.txt',overwrite=true)",
                     data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})

# perform the actual upload
with open( r'C:\Users\mengfeik\Documents\test.html', 'rb+') as file_input:
try: 
    response = session.post( 
        url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='" 
        + file_name + "',overwrite=true)",
        data=file_input)
except Exception as err: 
    print("Some error occurred: " + str(err))

print('end...')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM