Connecting to SharePoint to Write/Read Files through App Sign in Method (Client ID, Secret & Tenant ID) but wouldn't be able to get authenticated.
I have tried the code below:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.auth.authentication_context import AuthenticationContext
context = ClientContext(SITE_URL).with_credentials(
ClientCredential(CLIENT_ID, CLIENT_SECRET))
cert_credentials = {
"tenant": TENANT_ID,
"client_id": CLIENT_ID,
"thumbprint": test_cert_thumbprint,
"cert_path": "{0}/../selfsignkey.pem".format(os.path.dirname(__file__)),
}
ctx = ClientContext(test_site_url).with_client_certificate(**cert_credentials)
current_web = ctx.web.get().execute_query()
print("{0}".format(current_web.url))
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
class SharepointService:
app_principal = {'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET}
context_auth = AuthenticationContext(url=site_url)
token = context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
print(token)
ctx = ClientContext(site_url, context_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))
You could refer to following code to write or read files