Forum Discussion
Rohitb
1 year agoHelper I
Fabric Notebook Help - SharePoint
Hi, In most of the code that I am scanning across to upload file into sharepoint via service principal, sites.readwriteAll application permission is being provided to the app. But that is the h...
wardy912
1 year agoSuper User
import requests
from msal import ConfidentialClientApplication
client_id = 'YOUR-CLIENT-ID'
client_secret = 'YOUR-CLIENT-SECRET'
tenant_id = 'YOUR-TENANT-ID'
authority = f'https://login.microsoftonline.com/{tenant_id}'
scope = ['https://graph.microsoft.com/.default']
app = ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
token = app.acquire_token_for_client(scopes=scope)
headers = {
'Authorization': f"Bearer {token['access_token']}",
'Content-Type': 'text/plain'
}
file_content = 'Hello SharePoint!'
upload_url = 'https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/Folder/File.txt:/content'
response = requests.put(upload_url, headers=headers, data=file_content)
print(response.status_code, response.json())Rohitb
1 year agoHelper I
wardy912 : Thankyou for your response, Can you please let me know what API permission is needed for Service Principal for above code ?
- v-prasare1 year agoCommunity Support
The above requires Microsoft Graph > Application Permission: Sites.Selected.
This permission restricts the app to only the sites it is explicitly granted access to.
After assigning Sites.Selected, you must use SharePoint PowerShell (Set-SPOSite) to allow the app write access to the specific site.