Skip to content

Instantly share code, notes, and snippets.

@KyMidd
Created July 9, 2025 03:31
Show Gist options
  • Select an option

  • Save KyMidd/fbb0d96e38c18e92d2222763f88488d9 to your computer and use it in GitHub Desktop.

Select an option

Save KyMidd/fbb0d96e38c18e92d2222763f88488d9 to your computer and use it in GitHub Desktop.
def exchange_code_for_token(auth_code, tenant_id, client_id, client_secret):
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
data = {
"client_id": client_id,
"client_secret": client_secret,
"scope": "https://graph.microsoft.com/.default",
"code": auth_code,
"redirect_uri": f"{redirect_function_url}/callback",
"grant_type": "authorization_code",
}
response = requests.post(token_url, data=data)
response.raise_for_status()
return response.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment