Hello everyone, I have been struggling for weeks to connect to sharepoint and automate some of my ETL tasks using python.
So basically I have followed the following instructions:
Step 1: Register an Application in Azure Active Directory
- Log in to Azure Portal:
- Navigate to App Registrations:
- In the Azure AD menu, select "App registrations".
- Click "New registration".
- Fill in the App Registration Details:
- Name: Enter a name for your application (e.g., "SharePointETLApp").
- Supported Account Types: Choose "Accounts in this organizational directory only" if you're targeting your organization.
- Redirect URI: You can leave this blank for now (it's used for interactive logins, not for service-to-service apps).
- Obtain the Client ID:
- Once registered, go to the app's overview page.
- Copy the Application (client) ID. This is your SHAREPOINT_CLIENT_ID.
Step 2: Create a Client Secret
- In your registered application, navigate to the "Certificates & Secrets" tab under Manage on the left bar.
- Under Client Secrets, click "New client secret".
- Provide a description (e.g., "SharePointAccessSecret").
- Set an expiration period (e.g., 6 months, 1 year, or custom).
- Click "Add".
- Copy the generated secret value immediately. This is your SHAREPOINT_CLIENT_SECRET. (You won’t be able to see it again later.)
Step 3: Grant Permissions to the App
- Go to the "API Permissions" tab in your application.
- Click "Add a permission".
- Select "Microsoft Graph" and then "Delegated permissions" or "Application permissions" based on your needs:
- For accessing SharePoint files, add:
- Sites.FullControl.All
After all, I went to this link on sharepoint https://[tenant].sharepoint.com/sites/tasks/_layouts/15/AppRegNew.aspx
And registered my app and clicked on Trust button
Later I went to this address /_layouts/15/AppPrincipals.aspx to check if the app is there, and I cannot find my app that I registered in the previous step
this is my python code:
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
site_url = """https://[tenant].sharepoint.com/sites/tasks/"""
client_id = """client_id"""
client_secret = """client_secret"""
def test_connection(site_url, client_id, client_secret):
credentials = ClientCredential(client_id, client_secret)
ctx = ClientContext(site_url).with_credentials(credentials)
try:
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(f"Successfully connected to SharePoint site: {web.properties['Title']}")
except Exception as e:
print(f"An error occurred: {e}")
test_connection(site_url, client_id, client_secret)
And here is the output:
An error occurred: (None, None, '401 Client Error: Unauthorized for url: https://[tenant].sharepoint.com/sites/tasks/_api/Web')
I checked so many sources but I am getting really confused.
I would appreciate if anyone that has exprienced this, help me figure out how I can solve my question and finally be able to access sharepoint using python