@@ -7,7 +7,7 @@ class Client:
77 header = {"Accept" : "application/json, */*" , "content-type" : "application/json; charset=utf-8" ,
88 'OData-MaxVersion' : '4.0' , 'OData-Version' : '4.0' }
99
10- def __init__ (self , client_id , client_secret , token = None ):
10+ def __init__ (self , client_id = None , client_secret = None , token = None ):
1111 self .client_id = client_id
1212 self .client_secret = client_secret
1313 self .token = token
@@ -116,32 +116,42 @@ def parse_response(self, response):
116116 return response .json ()
117117
118118 def url_petition (self , redirect_uri , resource ):
119- url = "https://login.microsoftonline.com/{0}/oauth2/authorize?client_id={1}&response_type={2}&redirect_uri={3}&response_mode={4}&resource={5}" .format (
120- "common" , self .client_id , "code" , redirect_uri , "query" , resource )
119+ if self .client_id is not None and redirect_uri is not None and resource is not None :
120+ url = "https://login.microsoftonline.com/{0}/oauth2/authorize?client_id={1}&response_type={2}&redirect_uri={3}&response_mode={4}&resource={5}" .format (
121+ "common" , self .client_id , "code" , redirect_uri , "query" , resource )
122+
123+ # this part needs an administrator autorization
124+ # url = "https://login.microsoftonline.com/common/adminconsent?client_id={0}&redirect_uri={1}".format(
125+ # client_id, redirect_uri)
126+ return url
127+ else :
128+ raise Exception ("The attributes necessary to get the url were not obtained." )
121129
122- # this part needs an administrator autorization
123- # url = "https://login.microsoftonline.com/common/adminconsent?client_id={0}&redirect_uri={1}".format(
124- # client_id, redirect_uri)
125- return url
126130
127131 def exchange_code (self , redirect_uri , code ):
128- url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
129- args = {
130- 'client_id' : self .client_id ,
131- 'redirect_uri' : redirect_uri ,
132- 'client_secret' : self .client_secret ,
133- 'code' : code ,
134- 'grant_type' : 'authorization_code' ,
135- }
136- response = requests .post (url , data = args )
137- return self .parse_response (response )
132+ if self .client_id is not None and self .client_secret is not None and redirect_uri is not None and code is not None :
133+ url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
134+ args = {
135+ 'client_id' : self .client_id ,
136+ 'redirect_uri' : redirect_uri ,
137+ 'client_secret' : self .client_secret ,
138+ 'code' : code ,
139+ 'grant_type' : 'authorization_code' ,
140+ }
141+ response = requests .post (url , data = args )
142+ return self .parse_response (response )
143+ else :
144+ raise Exception ("The attributes necessary to exchange the code were not obtained." )
138145
139146 def refresh_token (self , refresh_token , redirect_uri , resource ):
140- url = "https://login.microsoftonline.com/common/oauth2/token"
141- args = {"client_id" : self .client_id , "grant_type" : "refresh_token" , "refresh_token" : refresh_token ,
142- "redirect_uri" : redirect_uri , "client_secret" : self .client_secret , "resource" : resource }
143- response = requests .post (url , data = args )
144- return self .parse_response (response )
147+ if self .client_id is not None and self .client_secret is not None and refresh_token is not None and redirect_uri is not None and resource is not None :
148+ url = "https://login.microsoftonline.com/common/oauth2/token"
149+ args = {"client_id" : self .client_id , "grant_type" : "refresh_token" , "refresh_token" : refresh_token ,
150+ "redirect_uri" : redirect_uri , "client_secret" : self .client_secret , "resource" : resource }
151+ response = requests .post (url , data = args )
152+ return self .parse_response (response )
153+ else :
154+ raise Exception ("The attributes necessary to refresh the token were not obtained." )
145155
146156 def set_token (self , token ):
147157 """
0 commit comments