29 lines
1021 B
Python
29 lines
1021 B
Python
import requests
|
|
|
|
from bot.msteams.token import Token
|
|
|
|
class Auth:
|
|
def __init__(self, client_id: str, client_secret: str, tenant_id: str) -> None:
|
|
self.client_id = client_id
|
|
self.client_secret = client_secret
|
|
self.tenant_id = tenant_id
|
|
self.__token = Token(self.__get_token_data())
|
|
|
|
def __str__(self) -> str:
|
|
if self.__token.is_expired:
|
|
self.__token = Token(self.__get_token_data())
|
|
return self.__token.token
|
|
|
|
def __get_token_data(self) -> dict:
|
|
url = f"https://login.microsoftonline.com/{self.tenant_id}/oauth2/v2.0/token"
|
|
headers = {
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
}
|
|
data = {
|
|
"grant_type": "client_credentials",
|
|
"client_id": self.client_id,
|
|
"client_secret": self.client_secret,
|
|
"scope": "https://api.botframework.com/.default"
|
|
}
|
|
response = requests.post(url, headers=headers, data=data)
|
|
return response.json() |