Created
January 25, 2019 20:24
-
-
Save jpda/7aff128f77a3e1b69ad0fd2a7fb3f7c1 to your computer and use it in GitHub Desktop.
Microsoft Graph from Python only using requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
import json | |
def main(): | |
client_id = "<CLIENT ID>" | |
client_secret = "<CLIENT SECRET>" | |
resource_id = "https://graph.microsoft.com" | |
tenant = "<TENANT NAME OR GUID> (like microsoft.com or jpd.ms)" | |
graph_query = "<GRAPH PATH> - example: users/john@jpd.ms" | |
# setup our Azure AD request | |
postData = {"client_id": client_id, "client_secret": client_secret, "grant_type": "client_credentials", "resource": resource_id} | |
token_request = requests.post("https://login.microsoftonline.com/{}/oauth2/token".format(tenant), data=postData) | |
print(token_request.status_code) | |
# parse the token response | |
token_response = json.loads(token_request.content) | |
# get the access token - there is other stuff in here too | |
access_token = token_response["access_token"] | |
# you can take the access token to jwt.io to decode and check it out | |
print(access_token) | |
# take the token into the request to the graph | |
graph_request = requests.get("https://graph.microsoft.com/v1.0/{}".format(graph_query), headers={'Authorization': 'Bearer {}'.format(access_token)}) | |
print(graph_request.content) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment