0

I'm currently trying to access files inside a company Sharepoint (of which I'm the administrator) using Python, but I'm running into some issues.

The first thing I've done was create credentials with tenant permissions using the following tutorial: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs.

With this, I generated a pair of Client ID and Client Secret, which I would then use to connect to Sharepoint with my Python code.

However, when running my code, I'm getting the following error message:

File "...\office365\runtime\auth\providers\saml_token_provider.py", line 211, in _process_service_token_response
    raise ValueError(self.error)
ValueError: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf

Can anybody please tell me if I'm doing something wrong, and what would be the best solution for the problem?

The code I'm currently using is available below.

# Import all the libraries
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File 
import io
import pandas as pd

# Target url taken from sharepoint and credentials
url = 'https://ORGANIZATION.sharepoint.com/:x:/g/personal/USER/xxxxxxxxxxxxxxxxx'
Client_ID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
Client_Secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(Client_ID, Client_Secret):
  ctx = ClientContext(url, ctx_auth)
  web = ctx.web
  ctx.load(web)
  ctx.execute_query()
  print("Authentication successful")

response = File.open_binary(ctx, url)

# Save data to BytesIO stream
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start

# Read excel file and each sheet into pandas dataframe
df = pd.read_excel(bytes_file_obj, sheet_name=None)
Linx
  • 67
  • 7

0 Answers0