0

What I want is uploading file to specific directory in the sharepoint via API and using python.

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]

{'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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3R5bTJkR2FrZGFWbHdDNHFSNllyczVOM05WTGhqalpRb3RKLzFldEpZSldlT0Q4bEpVQy8vZlpDYnE1TUVpb2ROcHo1eC92UTQ1WWZiWmg1UVU2clFpNUZzQytCUU1MUmpqdEtia1IxU090WXY2amNkZ3pQcDlZNjJsMlpHQ2piaXNzdHl2RDJMUHc9PTwvU1A+; 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; 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!

1 Answer 1

1

Please take a reference of below sample, i have tested it on my SPO environment.

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...')
Sign up to request clarification or add additional context in comments.

1 Comment

Thx, you solved my problem! And I want to ask that I got the Response 403. The error message is that : 'X-Forms_Based_Auth_Return_Url': 'https://{domain}.com/_layouts/15/error.aspx', 'X-MSDAVEXT_Error': '917656; Access+denied.+Before+opening+files+in+this+location%2c+you+must+first+browse+to+the+web+site+and+select+the+option+to+login+automatically.' I don't know how to solve this problem. I already surveyed some website for information like undocumented-features.com/2019/12/02/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.