I want to upload file to Sharepoint via python.

%3CLINGO-SUB%20id%3D%22lingo-sub-1442380%22%20slang%3D%22en-US%22%3EI%20want%20to%20upload%20file%20to%20Sharepoint%20via%20python.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1442380%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20faced%20a%20problem.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EResponse%26lt%3B403%26gt%3B%3CBR%20%2F%3E'X-MSDAVEXT_Error'%3A%20'917656%3B%20Access%2Bdenied.%2BBefore%2Bopening%2Bfiles%2Bin%2Bthis%2Blocation%252c%2Byou%2Bmust%2Bfirst%2Bbrowse%2Bto%2Bthe%2Bweb%2Bsite%2Band%2Bselect%2Bthe%2Boption%2Bto%2Blogin%2Bautomatically.'%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EHere%20is%20my%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%60%60%60%3C%2FP%3E%3CP%3Eimport%20requests%3CBR%20%2F%3Efrom%20shareplum%20import%20Office365%3CBR%20%2F%3Efrom%20config_s%20import%20config_s%3CBR%20%2F%3Efrom%20office365.runtime.auth.authentication_context%20import%20AuthenticationContext%3CBR%20%2F%3Efrom%20office365.sharepoint.client_context%20import%20ClientContext%3C%2FP%3E%3CP%3E%23%20get%20data%20from%20configuration%3CBR%20%2F%3Eusername%20%3D%20config_s%5B'sp_user'%5D%3CBR%20%2F%3Epassword%20%3D%20config_s%5B'sp_password'%5D%3CBR%20%2F%3Esite_name%20%3D%20config_s%5B'sp_site_name'%5D%3CBR%20%2F%3Ebase_path%20%3D%20config_s%5B'sp_base_path'%5D%3CBR%20%2F%3Edoc_library%20%3D%20config_s%5B'sp_doc_library'%5D%3C%2FP%3E%3CP%3Eurl_me%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fvpon365.sharepoint.com%2Fsites%2Fmsteams_ffbb68%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fvpon365.sharepoint.com%2Fsites%2Fmsteams_ffbb68%2F%3C%2FA%3E%22%3CBR%20%2F%3Eclient_id%20%3D%20config_s%5B'client_id'%5D%3CBR%20%2F%3Eclient_secret%20%3D%20config_s%5B'client_secret'%5D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Efile_name%20%3D%20%22test.csv%22%3CBR%20%2F%3E%23%20Obtain%20auth%20cookie%3CBR%20%2F%3Eauthcookie%20%3D%20Office365(base_path%2C%20username%3Dusername%2C%20password%3Dpassword).GetCookies()%3CBR%20%2F%3Esession%20%3D%20requests.Session()%3CBR%20%2F%3Esession.cookies%20%3D%20authcookie%3CBR%20%2F%3Esession.headers.update(%7B'user-agent'%3A%20'python_bite%2Fv1'%7D)%3CBR%20%2F%3Esession.headers.update(%7B'accept'%3A%20'application%2Fjson%3Bodata%3Dverbose'%7D)%3C%2FP%3E%3CP%3E%23%20ctx_auth%20%3D%20AuthenticationContext(url_me)%3CBR%20%2F%3E%23%20if%20ctx_auth.acquire_token_for_app(client_id%2C%20client_secret)%3A%3CBR%20%2F%3E%23%20ctx%20%3D%20ClientContext(url_me%2C%20ctx_auth)%3CBR%20%2F%3E%23%20web%20%3D%20ctx.web%3CBR%20%2F%3E%23%20ctx.load(web)%3CBR%20%2F%3E%23%20ctx.execute_query()%3CBR%20%2F%3E%23%20print%20(%22Web%20title%3A%20%7B0%7D%22.format(web.properties%5B'Title'%5D))%3C%2FP%3E%3CP%3E%23%20dirty%20workaround....%20I'm%20getting%20the%20X-RequestDigest%20from%20the%20first%20failed%20call%3CBR%20%2F%3Esession.headers.update(%7B'X-RequestDigest'%3A%20'FormDigestValue'%7D)%3CBR%20%2F%3Eresponse%20%3D%20session.post(%20url%3Dbase_path%20%2B%20%22%2Fsites%2F%22%20%2B%20site_name%20%2B%3CBR%20%2F%3E%22%2F_api%2Fweb%2FGetFolderByServerRelativeUrl('%22%20%2B%20doc_library%20%2B%3CBR%20%2F%3E%22')%2FFiles%2Fadd(url%3D'a.txt'%2Coverwrite%3Dtrue)%22%2C%3CBR%20%2F%3Edata%3D%22%22)%3CBR%20%2F%3Esession.headers.update(%7B'X-RequestDigest'%3A%20response.headers%5B'X-RequestDigest'%5D%7D)%3CBR%20%2F%3Eprint(response)%3CBR%20%2F%3Eprint(response.headers)%3C%2FP%3E%3CP%3E%23%20perform%20the%20actual%20upload%3C%2FP%3E%3CP%3Ewith%20open(%20r'%2FUsers%2FMetatrons%2FDesktop%2FAutoReport_Structure%2Ftest.csv'%2C%20'rb%2B')%20as%20file_input%3A%3CBR%20%2F%3Etry%3A%3CBR%20%2F%3Eresponse%20%3D%20session.post(%3CBR%20%2F%3Eurl%3Dbase_path%20%2B%20%22%2Fsites%2F%22%20%2B%20site_name%20%2B%20%22%2F_api%2Fweb%2FGetFolderByServerRelativeUrl('%22%20%2B%20doc_library%20%2B%20%22')%2FFiles%2Fadd(url%3D'%22%3CBR%20%2F%3E%2B%20file_name%20%2B%20%22'%2Coverwrite%3Dtrue)%22%2C%3CBR%20%2F%3Edata%3Dfile_input)%3CBR%20%2F%3Eprint(response)%3CBR%20%2F%3Eprint(response.headers)%3CBR%20%2F%3Eprint()%3CBR%20%2F%3Eexcept%20Exception%20as%20err%3A%3CBR%20%2F%3Eprint(%22Some%20error%20occurred%3A%20%22%20%2B%20str(err))%3C%2FP%3E%3CP%3Eprint('end...')%3C%2FP%3E%3CP%3E%60%60%60%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlz%20help%20me%20to%20solve%20this%20problem%2C%20thank%20you%20sincerely.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

I faced a problem.

Response<403>
'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.'

Here is my code.

 

```

import requests
from shareplum import Office365
from config_s import config_s
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

# get data from configuration
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']

url_me = "https://vpon365.sharepoint.com/sites/msteams_ffbb68/"
client_id = config_s['client_id']
client_secret = config_s['client_secret']


file_name = "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'})

# ctx_auth = AuthenticationContext(url_me)
# if ctx_auth.acquire_token_for_app(client_id, client_secret):
# ctx = ClientContext(url_me, ctx_auth)
# web = ctx.web
# ctx.load(web)
# ctx.execute_query()
# print ("Web title: {0}".format(web.properties['Title']))

# 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']})
print(response)
print(response.headers)

# perform the actual upload

with open( r'/Users/Metatrons/Desktop/AutoReport_Structure/test.csv', '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()
except Exception as err:
print("Some error occurred: " + str(err))

print('end...')

```

 

Plz help me to solve this problem, thank you sincerely.

0 Replies