Skip to content

Instantly share code, notes, and snippets.

@saul-data
Last active August 1, 2022 23:14
Show Gist options
  • Save saul-data/feabced59a82e45859f3da3db463c222 to your computer and use it in GitHub Desktop.
Save saul-data/feabced59a82e45859f3da3db463c222 to your computer and use it in GitHub Desktop.
Microsoft Graph Sharepoint Rest API with Python
import requests
import os
# Instructions for Dataplane data pipelines - https://dataplane.app
# References:
# https://docs.microsoft.com/en-us/sharepoint/dev/apis/sharepoint-rest-graph
# https://docs.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0
# Connect to Sharepoint via the Microsoft Graph API
# 1. Authenticate to the Microsoft Graph API
# Get Authentication details from Dataplane Secrets
office_365_secret_id = os.getenv("secret_dp_office_365_id")
office_365_secret = os.getenv("secret_dp_office_365_secret")
office_365_tenant_id = os.getenv("secret_dp_office_365_tenant_id")
office_365_application_id = os.getenv("secret_dp_office_365_application_id")
url = "https://login.microsoftonline.com/" + office_365_tenant_id + "/oauth2/v2.0/token"
payload=f"""
client_id={office_365_application_id}
&scope=https://graph.microsoft.com/.default
&client_secret= {office_365_secret}
&grant_type=client_credentials
"""
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment