Spaces:
Sleeping
Sleeping
import os | |
import msal | |
import requests | |
from fastapi import FastAPI, HTTPException, Request | |
from fastapi.responses import HTMLResponse, RedirectResponse | |
from dotenv import load_dotenv | |
load_dotenv() | |
# Configuration | |
CLIENT_ID = os.getenv('CLIENT_ID_2') | |
CLIENT_SECRET = os.getenv('CLIENT_SECRET_2') | |
TENANT_ID = os.getenv('TENANT_ID') | |
AUTHORITY = f"https://login.microsoftonline.com/common" | |
REDIRECT_URI = 'https://omkar008-micrososft-authentication.hf.space/getAToken' | |
SCOPES = ['Mail.Read'] | |
app = FastAPI() | |
def read_root(request: Request): | |
# Generate the authorization URL | |
app_instance = msal.ConfidentialClientApplication( | |
CLIENT_ID, | |
CLIENT_SECRET, | |
authority=AUTHORITY | |
) | |
auth_url = app_instance.get_authorization_request_url(SCOPES, redirect_uri=REDIRECT_URI) | |
return {"auth_url":auth_url} | |
def get_a_token(request: Request, code: str): | |
app_instance = msal.ConfidentialClientApplication( | |
CLIENT_ID, | |
CLIENT_SECRET, | |
authority=AUTHORITY | |
) | |
# Exchange the authorization code for an access token | |
result = app_instance.acquire_token_by_authorization_code(code, SCOPES, redirect_uri=REDIRECT_URI) | |
print(result) | |
if "access_token" in result: | |
access_token = result['access_token'] | |
print(access_token) | |
if not access_token: | |
return {"Access Token Not found !!"} | |
return {"access_token":access_token} |