1414MSADS_MANAGE = 'msads.manage'
1515ADS_MANAGE = 'ads.manage'
1616BINGADS_MANAGE = 'bingads.manage'
17+ MSA_PROD = 'msa.prod'
1718
1819class AuthorizationData :
1920 """ Represents a user who intends to access the corresponding customer and account.
@@ -216,11 +217,11 @@ def __init__(self, access_token=None, access_token_expires_in_seconds=None, refr
216217 self ._refresh_token = refresh_token
217218 self ._response_json = response_json
218219 self ._access_token_received_datetime = datetime .utcnow ()
219-
220+
220221 @property
221222 def access_token_received_datetime (self ):
222223 """ The datetime when access token was received
223-
224+
224225 :rtype: datetime
225226 """
226227 return self ._access_token_received_datetime
@@ -264,7 +265,7 @@ def refresh_token(self):
264265 """
265266
266267 return self ._refresh_token
267-
268+
268269 @property
269270 def response_json (self ):
270271 """ OAuth whole attribute that got along with access token.
@@ -288,7 +289,7 @@ class OAuthAuthorization(Authentication):
288289 * :class:`.OAuthWebAuthCodeGrant`
289290 """
290291
291- def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' ):
292+ def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' , use_msa_prod = True ):
292293 """ Initializes a new instance of the OAuthAuthorization class.
293294
294295 :param client_id: The client identifier corresponding to your registered application.
@@ -303,10 +304,10 @@ def __init__(self, client_id, oauth_tokens=None, env=PRODUCTION, oauth_scope=MSA
303304 self ._client_id = client_id
304305 self ._oauth_tokens = oauth_tokens
305306 self ._state = None
306- self .environment = env
307- self ._oauth_scope = oauth_scope
307+ self .environment = env
308+ self ._oauth_scope = MSA_PROD if env == SANDBOX and use_msa_prod else oauth_scope
308309 self ._tenant = tenant
309-
310+
310311 @property
311312 def tenant (self ):
312313 """ tenant
@@ -387,7 +388,7 @@ class OAuthWithAuthorizationCode(OAuthAuthorization):
387388 For more information about registering a Bing Ads application, see http://go.microsoft.com/fwlink/?LinkID=511607.
388389 """
389390
390- def __init__ (self , client_id , client_secret , redirection_uri , token_refreshed_callback = None , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = "common" ):
391+ def __init__ (self , client_id , client_secret , redirection_uri , token_refreshed_callback = None , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = "common" , use_msa_prod = True ):
391392 """ Initialize a new instance of this class.
392393
393394 :param client_id: The client identifier corresponding to your registered application.
@@ -404,7 +405,7 @@ def __init__(self, client_id, client_secret, redirection_uri, token_refreshed_ca
404405 :return:
405406 """
406407
407- super (OAuthWithAuthorizationCode , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , oauth_scope = oauth_scope , tenant = tenant )
408+ super (OAuthWithAuthorizationCode , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , oauth_scope = oauth_scope , tenant = tenant , use_msa_prod = use_msa_prod )
408409 self ._client_secret = client_secret
409410 self ._redirection_uri = redirection_uri
410411 self ._token_refreshed_callback = token_refreshed_callback
@@ -418,14 +419,14 @@ def get_authorization_endpoint(self):
418419 endpoint_url = _UriOAuthService .AUTHORIZE_URI [(self .environment , self ._oauth_scope )]
419420 if self .environment == PRODUCTION and (self ._oauth_scope == MSADS_MANAGE or self ._oauth_scope == ADS_MANAGE ):
420421 endpoint_url = endpoint_url .replace ('common' , self .tenant );
421-
422+
422423 endpoint = str .format (
423424 endpoint_url ,
424425 self ._client_id ,
425426 'code' ,
426427 quote_plus (self ._redirection_uri )
427428 )
428-
429+
429430 return endpoint if self .state is None else endpoint + '&state=' + self .state
430431
431432 def request_oauth_tokens_by_response_uri (self , response_uri , ** kwargs ):
@@ -546,7 +547,7 @@ class OAuthDesktopMobileAuthCodeGrant(OAuthWithAuthorizationCode):
546547 For more information about registering a Bing Ads application, see http://go.microsoft.com/fwlink/?LinkID=511607.
547548 """
548549
549- def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' ):
550+ def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' , use_msa_prod = True ):
550551 """ Initializes a new instance of the this class with the specified client id.
551552
552553 :param client_id: The client identifier corresponding to your registered application.
@@ -555,14 +556,16 @@ def __init__(self, client_id, oauth_tokens=None, env=PRODUCTION, oauth_scope=MSA
555556 :type oauth_tokens: OAuthTokens
556557 """
557558
559+ effective_scope = MSA_PROD if env == SANDBOX and use_msa_prod else oauth_scope
558560 super (OAuthDesktopMobileAuthCodeGrant , self ).__init__ (
559561 client_id ,
560562 None ,
561- _UriOAuthService .REDIRECTION_URI [(env , oauth_scope )],
563+ _UriOAuthService .REDIRECTION_URI [(env , effective_scope )],
562564 oauth_tokens = oauth_tokens ,
563565 env = env ,
564- oauth_scope = oauth_scope ,
565- tenant = tenant
566+ oauth_scope = effective_scope ,
567+ tenant = tenant ,
568+ use_msa_prod = use_msa_prod
566569 )
567570
568571
@@ -594,9 +597,9 @@ class OAuthDesktopMobileImplicitGrant(OAuthAuthorization):
594597 Authorization Code Grant section of the OAuth 2.0 spec at https://tools.ietf.org/html/rfc6749#section-4.1.
595598 For more information about registering a Bing Ads application, see http://go.microsoft.com/fwlink/?LinkID=511607.
596599 """
597-
598600
599- def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' ):
601+
602+ def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' , use_msa_prod = True ):
600603 """ Initializes a new instance of the this class with the specified client id.
601604
602605 :param client_id: The client identifier corresponding to your registered application.
@@ -605,9 +608,10 @@ def __init__(self, client_id, oauth_tokens=None, env=PRODUCTION, oauth_scope=MSA
605608 :type oauth_tokens: OAuthTokens
606609 """
607610
608- super (OAuthDesktopMobileImplicitGrant , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , oauth_scope = oauth_scope , tenant = tenant )
609-
610-
611+ effective_scope = MSA_PROD if env == SANDBOX and use_msa_prod else oauth_scope
612+ super (OAuthDesktopMobileImplicitGrant , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , oauth_scope = effective_scope , tenant = tenant )
613+
614+
611615
612616 def get_authorization_endpoint (self ):
613617 """ Gets the Microsoft Account authorization endpoint where the user should be navigated to give his or her consent.
@@ -625,7 +629,7 @@ def get_authorization_endpoint(self):
625629 'token' ,
626630 _UriOAuthService .REDIRECTION_URI [(self .environment , self ._oauth_scope )],
627631 )
628-
632+
629633 return endpoint if self .state is None else endpoint + '&state=' + self .state
630634
631635 def extract_access_token_from_uri (self , redirection_uri ):
@@ -666,25 +670,29 @@ def __init__(self):
666670 (PRODUCTION , MSADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/nativeclient' ,
667671 (PRODUCTION , ADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/nativeclient' ,
668672 (PRODUCTION , BINGADS_MANAGE ): 'https://login.live.com/oauth20_desktop.srf' ,
669- (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/common/oauth2/nativeclient'
673+ (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/common/oauth2/nativeclient' ,
674+ (SANDBOX , MSA_PROD ): 'https://login.microsoftonline.com/common/oauth2/nativeclient'
670675 }
671676 AUTH_TOKEN_URI = {
672677 (PRODUCTION , MSADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/token' ,
673678 (PRODUCTION , ADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/token' ,
674679 (PRODUCTION , BINGADS_MANAGE ): 'https://login.live.com/oauth20_token.srf' ,
675- (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/consumers/oauth2/v2.0/token'
680+ (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/consumers/oauth2/v2.0/token' ,
681+ (SANDBOX , MSA_PROD ): 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
676682 }
677683 AUTHORIZE_URI = {
678- (PRODUCTION , MSADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={0}&scope=https%3A%2F%2Fads.microsoft.com%2Fmsads.manage%20offline_access&response_type={1}&redirect_uri={2}' ,
684+ (PRODUCTION , MSADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={0}&scope=https%3A%2F%2Fads.microsoft.com%2Fmsads.manage%20offline_access&response_type={1}&redirect_uri={2}' ,
679685 (PRODUCTION , ADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={0}&scope=https%3A%2F%2Fads.microsoft.com%2Fads.manage%20offline_access&response_type={1}&redirect_uri={2}' ,
680686 (PRODUCTION , BINGADS_MANAGE ): 'https://login.live.com/oauth20_authorize.srf?client_id={0}&scope=bingads.manage&response_type={1}&redirect_uri={2}' ,
681- (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/consumers/oauth2/v2.0/authorize?client_id={0}&scope=https://api.ads.microsoft.com/msads.manage%20offline_access&response_type={1}&redirect_uri={2}&prompt=login'
687+ (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/consumers/oauth2/v2.0/authorize?client_id={0}&scope=https://api.ads.microsoft.com/msads.manage%20offline_access&response_type={1}&redirect_uri={2}&prompt=login' ,
688+ (SANDBOX , MSA_PROD ): 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={0}&scope=https%3A%2F%2Fsi.ads.microsoft.com%2Fmsads.manage%20offline_access&response_type={1}&redirect_uri={2}'
682689 }
683690 SCOPE = {
684- (PRODUCTION , MSADS_MANAGE ): 'https://ads.microsoft.com/msads.manage offline_access' ,
685- (PRODUCTION , ADS_MANAGE ): 'https://ads.microsoft.com/ads.manage offline_access' ,
691+ (PRODUCTION , MSADS_MANAGE ): 'https://ads.microsoft.com/msads.manage offline_access' ,
692+ (PRODUCTION , ADS_MANAGE ): 'https://ads.microsoft.com/ads.manage offline_access' ,
686693 (PRODUCTION , BINGADS_MANAGE ): 'bingads.manage' ,
687- (SANDBOX , MSADS_MANAGE ): 'https://api.ads.microsoft.com/msads.manage offline_access'
694+ (SANDBOX , MSADS_MANAGE ): 'https://api.ads.microsoft.com/msads.manage offline_access' ,
695+ (SANDBOX , MSA_PROD ): 'https://si.ads.microsoft.com'
688696 }
689697
690698 @staticmethod
@@ -699,12 +707,12 @@ def get_access_token(**kwargs):
699707
700708 if 'client_secret' in kwargs and kwargs ['client_secret' ] is None :
701709 del kwargs ['client_secret' ]
702-
710+
703711 if 'oauth_scope' in kwargs and kwargs ['oauth_scope' ] == 'bingads.manage' :
704712 del kwargs ['tenant' ]
705-
713+
706714 auth_token_url = _UriOAuthService .AUTH_TOKEN_URI [(kwargs ['environment' ], kwargs ['oauth_scope' ])]
707-
715+
708716 if 'tenant' in kwargs and kwargs ['tenant' ] is not None :
709717 auth_token_url = auth_token_url .replace ('common' , kwargs ['tenant' ])
710718
0 commit comments