使用Python通过API上载Sharepoint文件

2024-04-27 02:36:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要的是通过API和python将文件上传到sharepoint中的特定目录

这是我的代码:

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")

我得到的信息是

** 答复[400]

{'Cache-Control':'private,max age=0', “传输编码”:“分块”, “内容类型”:“应用程序/json;odata=verbose;字符集=utf-8”, “到期”:“2020年4月27日星期一05:58:55 GMT”, “上次修改”:“2020年5月12日星期二05:58:55 GMT”, “P3P”:“CP=”所有工业数字信号处理器相关行政控制当前CUSo IVAo IVDo PSA PSD TAI TELo我们的SAMo CNT COM INT NAV ONL PHY PRE PUR UNI”, “设定Cookie”如下:“RTF=v+WfHHHH6 6 6 6 8 8 8 8 8 8 8 8 8 8 8 8中国移动通信技术MJ3 3 0 0 RTY2 10 10 10中国移动通信技术MJ3 3 3 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 A/JM3LHW528rf/tR64zzKHRIqfdziTzCRnndQP9+5YzMk0jFFfslLjmjvMMO19yQ1m70+T8l/QYQK5OMKFCW3ZW9HIHPPC29SDutts46LDBVWQOKUAAA=;domain=sharepoint.com;path=/;SameSite=None;安全;仅HttpOnly,FedAuth=77u/PD94BwgWgdmVYC2LVBj0IMS4WIBLBMNzGZGLUZZZZZZZWIDZIZUZZZKZZZZZZZZZZZZZZZZZZLZLW2FY2ZY2VZLW9V+VZGLWZZZZZZZLWZZZLWZZZZLWZZZLWZZZZZZZZZZZBSWxMzMzZZZZNjZNcZNdAwmdAwm2MzMzMzMzMzMzMzMzMdOdg1NdAzMzMzMzM4NmVkMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMy2LzMy2LzMzMy2LzMy2LzMy2LzMy2LzMy2LzMy2LzMy2LzMy2Lc2LzNzNzNzNzMy2LzNzMy2LzNzMy2Lc2LzNzNzNzNzNzNzNzNzMy2LzNzNzMy2LzNzNzNzNzNzNzMy2LzM2.2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 u090WXY2amNkZ3pQcDlZNjJsMlpHQ2piaXNzdHl2RDJMUHc9PTwvU1A+;路径=/;SameSite=无;安全;HttpOnly', “X-SharePointHealthScore”:“0”, “X-SP-SERVERSTATE”:“只读=0”, “DATASERVICEVERSION”:“3.0”, “SPClientServiceRequestDuration”:“54”, “X-AspNet-Version”:“4.0.30319”, “SPRequestGuid”:“3c9a519f-00ce-a000-89f2-6080347e58ff”, “请求id”:“3c9a519f-00ce-a000-89f2-6080347e58ff”, “MS-CV”:“n1GaPM4AAKCJ8mCANH5Y/w.0”, “X-RequestDigest”:“0x26237158F71B34858BF0EB713CDA5A9412C28A5AC95FF342B326F7B8DE04DD93AA4E23300444AC0D7BF427E2D05BDA4 B526CB2BF8BB32D73F8917E9CC88F495,2020年5月12日05:58:56-0000”, “严格的运输安全”:“最大年龄=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”:“参考A:DFB58EAF5C2D4ECB8027CD2E3348A80C参考B:TPE30EDGE0216参考C:2020-05-12T05:58:55Z”, “日期”:“2020年5月12日星期二05:58:55 GMT” **

请帮我解决这个问题,谢谢


Tags: pathnameconfigbasedocresponsesessionlibrary
1条回答
网友
1楼 · 发布于 2024-04-27 02:36:46

请参考下面的示例,我已经在我的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...')

相关问题 更多 >