Initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
load_dotenv(find_dotenv())
|
||||
import os
|
||||
import modules.logger_tool as logger
|
||||
log_name = 'api_modules_msgraph_config'
|
||||
log_dir = os.getenv("LOG_PATH", "/logs") # Default path as fallback
|
||||
logging = logger.get_logger(
|
||||
name=log_name,
|
||||
log_level=os.getenv("LOG_LEVEL", "DEBUG"),
|
||||
log_path=log_dir,
|
||||
log_file=log_name,
|
||||
runtime=True,
|
||||
log_format='default'
|
||||
)
|
||||
from msal import ConfidentialClientApplication
|
||||
|
||||
CLIENT_ID = os.getenv("VITE_MICROSOFT_CLIENT_ID")
|
||||
CLIENT_SECRET = os.getenv("VITE_MICROSOFT_CLIENT_SECRET")
|
||||
TENANT_ID = os.getenv("VITE_MICROSOFT_TENANT_ID")
|
||||
AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}"
|
||||
SCOPE = ["https://graph.microsoft.com/.default"]
|
||||
|
||||
# Create an MSAL confidential client application
|
||||
def get_ms_access_token():
|
||||
app = ConfidentialClientApplication(
|
||||
client_id=CLIENT_ID,
|
||||
client_credential=CLIENT_SECRET,
|
||||
authority=AUTHORITY,
|
||||
)
|
||||
|
||||
# For a confidential client application, we don't use user-specific accounts
|
||||
# Instead, we directly acquire a token for the application
|
||||
result = app.acquire_token_for_client(scopes=SCOPE)
|
||||
|
||||
if 'access_token' in result:
|
||||
logging.info("Token acquired successfully")
|
||||
return result['access_token']
|
||||
else:
|
||||
error_message = f"Failed to acquire token: {result.get('error')}, {result.get('error_description')}"
|
||||
logging.error(error_message)
|
||||
raise Exception(error_message)
|
||||
@@ -0,0 +1,40 @@
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
load_dotenv(find_dotenv())
|
||||
import os
|
||||
import modules.logger_tool as logger
|
||||
log_name = 'api_modules_msgraph_client'
|
||||
log_dir = os.getenv("LOG_PATH", "/logs") # Default path as fallback
|
||||
logging = logger.get_logger(
|
||||
name=log_name,
|
||||
log_level=os.getenv("LOG_LEVEL", "DEBUG"),
|
||||
log_path=log_dir,
|
||||
log_file=log_name,
|
||||
runtime=True,
|
||||
log_format='default'
|
||||
)
|
||||
import requests
|
||||
|
||||
GRAPH_API_ENDPOINT = "https://graph.microsoft.com/v1.0"
|
||||
|
||||
class MSGraphClient:
|
||||
def __init__(self, access_token: str):
|
||||
self.access_token = access_token
|
||||
|
||||
def get_headers(self):
|
||||
return {
|
||||
"Authorization": f"Bearer {self.access_token}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
def get_onenote_notebooks(self):
|
||||
url = f"{GRAPH_API_ENDPOINT}/me/onenote/notebooks"
|
||||
response = requests.get(url, headers=self.get_headers())
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json().get('value', [])
|
||||
else:
|
||||
raise Exception(f"Error fetching notebooks: {response.status_code}, {response.text}")
|
||||
|
||||
# Function to initialize the MSGraph client
|
||||
def get_msgraph_client(access_token: str):
|
||||
return MSGraphClient(access_token)
|
||||
Reference in New Issue
Block a user