0

I was trying to use python3 to read an excel file located on sharepoint with company's account. Here is the code I copied/modified from Dan's amazing answer in this post

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File 

url = 'https://company.sharepointsite.com/sites/documentsite'
username = 'username'
password = 'password'
relative_url = '/sites/documentsite/Documents/filename.xlsx'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
  ctx = ClientContext(url, ctx_auth)
  web = ctx.web
  ctx.load(web)
  ctx.execute_query()
  print "Web title: {0}".format(web.properties['Title'])

else:
  print ctx_auth.get_last_error()

Here is the error I got : enter image description here

Does that mean the company is blocking the token thingy? and I need to talk to the company admin to do something ?

Any help would be greatly appreciated !!! thanks ! !

yingnan liu
  • 409
  • 1
  • 8
  • 18
  • Can you manually access that file? – Trenton McKinney Jul 11 '20 at 01:36
  • @TrentonMcKinney Hi Trenton! yes I can, I created that excel on sharepoint for testing, which is stored in a "team folder" in company's sharepoint, if that helps, thanks !! – yingnan liu Jul 11 '20 at 01:40
  • https://stackoverflow.com/questions/53671547/python-download-files-from-sharepoint-site alternate solution, but could be a policy issue. – Trenton McKinney Jul 11 '20 at 01:50
  • @TrentonMcKinney awesome thanks! I will take a look. and just another quick question on "URL" that I used in my code, is that the "FULL" url that I copied directly from the excel file in sharepoint? or need to be the first couple characters? e.g: https://mycompany.sharepoint.com ? (after "sharepoint.com", my url still has tons of characters...) thanks again !! – yingnan liu Jul 11 '20 at 02:09
  • I don't know. I just found that answer with Google. – Trenton McKinney Jul 11 '20 at 02:13
  • strange, did you try using the `Graph API` instead ? I've pulled data down via excel straight into a dataframe using the graph api, there is a library called `o365` that handles most of the code for you. I'd look into that – Umar.H Jul 11 '20 at 11:13
  • @TrentonMcKinney no problem, thanks for the help ! – yingnan liu Jul 11 '20 at 13:15
  • @Datanovice nope, i am fairly new to python, but will give graph api a try, thanks for pointing it out – yingnan liu Jul 11 '20 at 13:16
  • hello @yingnanliu I'm in the same situation, were you able to solve it? – Roger Nieto Oct 01 '20 at 01:43

2 Answers2

0

You have to set the permission in the code correctly in order to execute the function you want to accomplish here.

This is Collection Permissions, Site Permissions, Webpart permissions, List permissions and then folder permissions in SharePoint and Windows permissions.

Makyen
  • 31,849
  • 12
  • 86
  • 121
  • 2
    Posts on Stack Overflow shouldn't contain a "signature" within the question or answer. There's a user-card below every post, which serves the purpose of a signature. If a visitor wants to know more about you, they can either hover the mouse over your user card and see the first part of your profile text, or click through to your profile page and see much more information, including whatever you've written in your profile "about me" section. If you do include an additional signature in your posts, many people tend to react to such signatures as if they were spam (i.e. negatively). – Makyen Jul 11 '20 at 05:35
  • thanks Randy !! I am fairly new to python and i am searching those permissions you mentioned, meanwhile, any chance you could help me on which permissions i am missing ? – yingnan liu Jul 11 '20 at 13:14
  • You want to enable direct authentication and password sync. – RandyDosanjh Jul 11 '20 at 16:11
0

Yingnan

What i would do in this case is either create a user with the permissions required for your code to run or, create a group for users to use this code. if you group them, you will need to see how much they need to use, let say "site permissions" for your group or maybe "collection level permissions".