0

I'm using Office365-REST-Python-Client to access some files from Sharepoint through a Python script (using App-only method)

a code example would be this:

credentials = ClientCredential(x, y)
ctx = ClientContext(z).with_credentials(credentials)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))

response = File.open_binary(ctx, relative_url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0)

print(bytes_file_obj.getvalue())

My problem is that I can get the web.properties (it shows the title in this example) but when I try to do anything related to the file I get an error saying the file does not exist:

b'{"error":{"code":"-2130575338, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The file /PATH/TO/FILE/ does not exist."}}}'

I try creating different files on different locations but the error is the same, some of those would be

z = 'https://tenant.sharepoint.com/sites/test/' 
z = 'https://tenant.sharepoint.com/sites/test'
relative_url = '/sites/test/Shared%20Documents/TestBook.xlsx'
relative_url = '/sites/test/Shared Documents/TestBook.xlsx'
relative_url = '/sites/test/SharedDocuments/TestBook.xlsx'

z = 'https://tenant.sharepoint.com/' 
z = 'https://tenant.sharepoint.com' 
relative_url = '/Shared%20Documents/Book544.xlsx'
relative_url = '/Shared%20Documents/TestTest/TestBook222.xlsx'
relative_url = '/Shared Documents/TestTest/TestBook222.xlsx'

For reference, the paths taken from Sharepoint file properties would be like this: https://tenant.sharepoint.com/Shared%20Documents/Book544.xlsx

https://tenant.sharepoint.com/Shared%20Documents/TestTest/TestBook222.xlsx

https://tenant.sharepoint.com/sites/test/Shared%20Documents/TestBook.xlsx

For this test the xml permissions on /_layouts/15/appinv.aspx are like this (on tenant-admin.sharepoint.com and tenant.sharepoint.com/sites/test respectively):

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>

and

<AppPermissionRequests AllowAppOnlyPolicy="true">
 <AppPermissionRequest Scope="http://sharepoint/content/tenant/sites/test" Right="FullControl" />
</AppPermissionRequests>

I run out of ideas on how to solve this. How can I access the Sharepoint file using the Rest API after I already successfully pass the authentication part?

2 Answers 2

1

You web.config should have below correct entry.

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>

Your CSOM code should be as follows:

credentials = ClientCredential(x, y)
ctx = ClientContext(z).with_credentials(credentials)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))

File file = ctx.Web.GetFileByServerRelativeUrl("/sites/test/Shared Documents/testbook.xlsx");

clientContext.Load(file);
clientContext.ExecuteQuery();

You'll get your file data in file object

0

The problem was I wrote the name of the SharePoint tenant on

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>

When I use literally "http://sharepoint/content/tenant" the rest of the script works fine as it is.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.