I want to connect to a Sharepoint directory to overwrite a file there on a schedule. First I need to connect to the site, but I’m getting an unauthorized_client error and a message stating there might be an issue with the tenant. My code is below, based on examples I’ve found online. I didn’t grok how to construct the site_url, but I’ve tried many values for that variable. Do you know how I can connect to my Sharepoint site using a client_id, client_secret, and tenant_id?
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.authentication_context import AuthenticationContext
# SharePoint Credentials
client_id = ""
client_secret = ""
tenant_id = ""
# SharePoint site URL, but I'm not sure this is constructed properly
site_url = f"https://companyName.sharepoint.com/{tenant_id}/oauth2/v2.0/token"
# Authentication context using client_id and client_secret
context_auth = AuthenticationContext(site_url)
# Acquire token for the application
try:
if context_auth.acquire_token_for_app(
client_id=client_id, client_secret=client_secret
):
# Create SharePoint client context
ctx = ClientContext(site_url, context_auth)
print(ctx)
web=ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))
except Exception as e:
# Print error message if an exception occurs during SharePoint context establishment
print(f"Error: {type(e).__name__} {e}")
Here is the error returned.
Error states:
Error: ValueError {"error":"unauthorized_client","error_description":"AADSTS7000 16: Application with identifier '('',)' was not found in the directory ''. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant.