11import json
2- import msal
32import time
43import urllib
54import random
98from typing import List
109from typing import Dict
1110
11+ import msal
12+
1213from ms_graph .users import Users
1314from ms_graph .drives import Drives
1415from ms_graph .groups import Groups
2223
2324class MicrosoftGraphClient ():
2425
26+ """
27+ ### Overview:
28+ ----
29+ Used as the main entry point for the Microsoft Graph
30+ API Service.
31+ """
32+
2533 RESOURCE = 'https://graph.microsoft.com/'
2634
2735 AUTHORITY_URL = 'https://login.microsoftonline.com/'
@@ -32,9 +40,16 @@ class MicrosoftGraphClient():
3240 OFFICE365_AUTH_ENDPOINT = '/oauth20_authorize.srf?'
3341 OFFICE365_TOKEN_ENDPOINT = '/oauth20_token.srf'
3442
35- def __init__ (self , client_id : str , client_secret : str , redirect_uri : str ,
36- scope : List [str ], account_type : str = 'consumers' ,
37- office365 : bool = False , credentials : str = None ):
43+ def __init__ (
44+ self ,
45+ client_id : str ,
46+ client_secret : str ,
47+ redirect_uri : str ,
48+ scope : List [str ],
49+ account_type : str = 'consumers' ,
50+ office365 : bool = False ,
51+ credentials : str = None
52+ ):
3853 """Initializes the Graph Client.
3954
4055 ### Parameters
@@ -86,6 +101,7 @@ def __init__(self, client_id: str, client_secret: str, redirect_uri: str,
86101 self .office_url = self .OFFICE365_AUTHORITY_URL + self .OFFICE365_AUTH_ENDPOINT
87102 self .graph_url = self .AUTHORITY_URL + self .account_type + self .AUTH_ENDPOINT
88103 self .office365 = office365
104+ self ._redirect_code = None
89105
90106 # Initialize the Credential App.
91107 self .client_app = msal .ConfidentialClientApplication (
@@ -121,7 +137,7 @@ def _state(self, action: str, token_dict: dict = None) -> bool:
121137 if does_exists and action == 'load' :
122138
123139 # Load the file.
124- with open (file = self .credentials , mode = 'r' ) as state_file :
140+ with open (file = self .credentials , mode = 'r' , encoding = 'utf-8' ) as state_file :
125141 credentials = json .load (fp = state_file )
126142
127143 # Grab the Token if it exists.
@@ -150,7 +166,7 @@ def _state(self, action: str, token_dict: dict = None) -> bool:
150166 self .id_token = token_dict ['id_token' ]
151167 self .token_dict = token_dict
152168
153- with open (file = self .credentials , mode = 'w+' ) as state_file :
169+ with open (file = self .credentials , mode = 'w+' , encoding = 'utf-8' ) as state_file :
154170 json .dump (obj = token_dict , fp = state_file , indent = 2 )
155171
156172 def _token_seconds (self , token_type : str = 'access_token' ) -> int :
@@ -249,10 +265,12 @@ def login(self) -> None:
249265 # Build the URL.
250266 url = self .authorization_url ()
251267
252- # aks the user to go to the URL provided, they will be prompted to authenticate themsevles.
253- print ('Please go to URL provided authorize your account: {}' .format (url ))
268+ # aks the user to go to the URL provided, they will be prompted
269+ # to authenticate themsevles.
270+ print (f'Please go to URL provided authorize your account: { url } ' )
254271
255- # ask the user to take the final URL after authentication and paste here so we can parse.
272+ # ask the user to take the final URL after authentication and
273+ # paste here so we can parse.
256274 my_response = input ('Paste the full URL redirect here: ' )
257275
258276 # store the redirect URL
0 commit comments