this is the code:
import streamlit as st
import requests
import urllib
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
tenant_id = 'YOUR_TENANT_ID'
site_id = "m365x36241825.sharepoint.com,2b7d320b-e002-4e09-a83c-769c46353838,3544db06-d815-4384-aea2-2da9910245aa"
file_path = "/sites/Contoso/Shared Documents/General/datastax.txt"
# Function to get access token
def get_access_token(client_id, client_secret, tenant_id):
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://graph.microsoft.com/.default'
}
token_r = requests.post(token_url, data=token_data)
return token_r.json().get('access_token')
# Function to fetch file content
def fetch_file_content(token, site_id, file_path):
headers = {
'Authorization': f'Bearer {token}'
}
# Encode the file path
encoded_file_path = urllib.parse.quote(file_path)
url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:{encoded_file_path}:/content"
response = requests.get(url, headers=headers)
print("URL:", url)
return response
# List contents of the "General" folder
def list_folder_contents(token, site_id, folder_path):
headers = {
'Authorization': f'Bearer {token}'
}
encoded_folder_path = urllib.parse.quote(folder_path)
url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:{encoded_folder_path}:/children"
response = requests.get(url, headers=headers)
print("URL:", url)
return response
# Folder path to the "General" folder
folder_path = "/sites/Contoso/Shared Documents/General"
token = get_access_token(client_id, client_secret, tenant_id)
response = list_folder_contents(token, site_id, folder_path)
if response.status_code == 200:
items = response.json().get('value', [])
for item in items:
print(f"Name: {item['name']}, ID: {item['id']}, Type: {item['folder'] if 'folder' in item else 'file'}")
else:
print(f"Error: {response.status_code} - {response.json()}")
# Streamlit app
st.title("SharePoint File Content")
token = get_access_token(client_id, client_secret, tenant_id)
response = fetch_file_content(token, site_id, file_path)
print("Token: ")
print(token)
if response.status_code == 200:
file_content = response.content.decode('utf-8') # Assuming it's a text file
st.text(file_content)
else:
st.error(f"Error: {response.status_code} - {response.json()}")
this code is giving me response: {‘error’: {‘code’: ‘itemNotFound’, ‘message’: ‘The resource could not be found.’}}
while it is present in the sharepoint: https://m365x36241825.sharepoint.com/sites/Contoso/Shared%20Documents/Forms/AllItems.aspx?id=%2Fsites%2FContoso%2FShared%20Documents%2FGeneral%2Fdatastax%2Etxt&viewid=30ac4a84%2D14e8%2D4f6e%2D86b4%2D6da6fa8830b3&parent=%2Fsites%2FContoso%2FShared%20Documents%2FGeneral
I have added all the permission in the azure portal.
Swasti Ranjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.