1111
1212PRODUCTION = 'production'
1313SANDBOX = 'sandbox'
14+ MSADS_MANAGE = 'msads.manage'
15+ ADS_MANAGE = 'ads.manage'
16+ BINGADS_MANAGE = 'bingads.manage'
1417
1518class AuthorizationData :
1619 """ Represents a user who intends to access the corresponding customer and account.
@@ -285,7 +288,7 @@ class OAuthAuthorization(Authentication):
285288 * :class:`.OAuthWebAuthCodeGrant`
286289 """
287290
288- def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , require_live_connect = False , tenant = 'common' ):
291+ def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' ):
289292 """ Initializes a new instance of the OAuthAuthorization class.
290293
291294 :param client_id: The client identifier corresponding to your registered application.
@@ -301,7 +304,7 @@ def __init__(self, client_id, oauth_tokens=None, env=PRODUCTION, require_live_co
301304 self ._oauth_tokens = oauth_tokens
302305 self ._state = None
303306 self .environment = env
304- self ._require_live_connect = require_live_connect
307+ self ._oauth_scope = oauth_scope
305308 self ._tenant = tenant
306309
307310 @property
@@ -384,7 +387,7 @@ class OAuthWithAuthorizationCode(OAuthAuthorization):
384387 For more information about registering a Bing Ads application, see http://go.microsoft.com/fwlink/?LinkID=511607.
385388 """
386389
387- def __init__ (self , client_id , client_secret , redirection_uri , token_refreshed_callback = None , oauth_tokens = None , env = PRODUCTION , require_live_connect = False , tenant = "common" ):
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" ):
388391 """ Initialize a new instance of this class.
389392
390393 :param client_id: The client identifier corresponding to your registered application.
@@ -401,7 +404,7 @@ def __init__(self, client_id, client_secret, redirection_uri, token_refreshed_ca
401404 :return:
402405 """
403406
404- super (OAuthWithAuthorizationCode , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , require_live_connect = require_live_connect , tenant = tenant )
407+ super (OAuthWithAuthorizationCode , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , oauth_scope = oauth_scope , tenant = tenant )
405408 self ._client_secret = client_secret
406409 self ._redirection_uri = redirection_uri
407410 self ._token_refreshed_callback = token_refreshed_callback
@@ -412,8 +415,8 @@ def get_authorization_endpoint(self):
412415 :return: The Microsoft Account authorization endpoint.
413416 :rtype: str
414417 """
415- endpoint_url = _UriOAuthService .AUTHORIZE_URI [(self .environment , self ._require_live_connect )]
416- if self .environment == PRODUCTION and self ._require_live_connect == False :
418+ endpoint_url = _UriOAuthService .AUTHORIZE_URI [(self .environment , self ._oauth_scope )]
419+ if self .environment == PRODUCTION and ( self ._oauth_scope == MSADS_MANAGE or self . _oauth_scope == ADS_MANAGE ) :
417420 endpoint_url = endpoint_url .replace ('common' , self .tenant );
418421
419422 endpoint = str .format (
@@ -453,7 +456,7 @@ def request_oauth_tokens_by_response_uri(self, response_uri, **kwargs):
453456 grant_type = 'authorization_code' ,
454457 environment = self .environment ,
455458 code = code ,
456- requireliveconnect = self ._require_live_connect ,
459+ oauth_scope = self ._oauth_scope ,
457460 tenant = self .tenant ,
458461 ** kwargs
459462 )
@@ -480,8 +483,8 @@ def request_oauth_tokens_by_refresh_token(self, refresh_token):
480483 grant_type = 'refresh_token' ,
481484 refresh_token = refresh_token ,
482485 environment = self .environment ,
483- scope = _UriOAuthService .SCOPE [(self .environment , self ._require_live_connect )],
484- requireliveconnect = self ._require_live_connect ,
486+ scope = _UriOAuthService .SCOPE [(self .environment , self ._oauth_scope )],
487+ oauth_scope = self ._oauth_scope ,
485488 tenant = self .tenant
486489 )
487490 if self .token_refreshed_callback is not None :
@@ -543,7 +546,7 @@ class OAuthDesktopMobileAuthCodeGrant(OAuthWithAuthorizationCode):
543546 For more information about registering a Bing Ads application, see http://go.microsoft.com/fwlink/?LinkID=511607.
544547 """
545548
546- def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , require_live_connect = False , tenant = 'common' ):
549+ def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' ):
547550 """ Initializes a new instance of the this class with the specified client id.
548551
549552 :param client_id: The client identifier corresponding to your registered application.
@@ -555,10 +558,10 @@ def __init__(self, client_id, oauth_tokens=None, env=PRODUCTION, require_live_co
555558 super (OAuthDesktopMobileAuthCodeGrant , self ).__init__ (
556559 client_id ,
557560 None ,
558- _UriOAuthService .REDIRECTION_URI [(env , require_live_connect )],
561+ _UriOAuthService .REDIRECTION_URI [(env , oauth_scope )],
559562 oauth_tokens = oauth_tokens ,
560563 env = env ,
561- require_live_connect = require_live_connect ,
564+ oauth_scope = oauth_scope ,
562565 tenant = tenant
563566 )
564567
@@ -593,7 +596,7 @@ class OAuthDesktopMobileImplicitGrant(OAuthAuthorization):
593596 """
594597
595598
596- def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , require_live_connect = False , tenant = 'common' ):
599+ def __init__ (self , client_id , oauth_tokens = None , env = PRODUCTION , oauth_scope = MSADS_MANAGE , tenant = 'common' ):
597600 """ Initializes a new instance of the this class with the specified client id.
598601
599602 :param client_id: The client identifier corresponding to your registered application.
@@ -602,7 +605,7 @@ def __init__(self, client_id, oauth_tokens=None, env=PRODUCTION, require_live_co
602605 :type oauth_tokens: OAuthTokens
603606 """
604607
605- super (OAuthDesktopMobileImplicitGrant , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , require_live_connect = require_live_connect , tenant = tenant )
608+ super (OAuthDesktopMobileImplicitGrant , self ).__init__ (client_id , oauth_tokens = oauth_tokens , env = env , oauth_scope = oauth_scope , tenant = tenant )
606609
607610
608611
@@ -613,14 +616,14 @@ def get_authorization_endpoint(self):
613616 :rtype: str
614617 """
615618
616- endpoint_url = _UriOAuthService .AUTHORIZE_URI [(self .environment , self ._require_live_connect )]
617- if self .environment == PRODUCTION and self ._require_live_connect == False :
619+ endpoint_url = _UriOAuthService .AUTHORIZE_URI [(self .environment , self ._oauth_scope )]
620+ if self .environment == PRODUCTION and ( self ._oauth_scope == MSADS_MANAGE or self . _oauth_scope == ADS_MANAGE ) :
618621 endpoint_url = endpoint_url .replace ('common' , self .tenant );
619622 endpoint = str .format (
620623 endpoint_url ,
621624 self .client_id ,
622625 'token' ,
623- _UriOAuthService .REDIRECTION_URI [(self .environment , self ._require_live_connect )],
626+ _UriOAuthService .REDIRECTION_URI [(self .environment , self ._oauth_scope )],
624627 )
625628
626629 return endpoint if self .state is None else endpoint + '&state=' + self .state
@@ -650,7 +653,7 @@ def extract_access_token_from_uri(self, redirection_uri):
650653
651654 @property
652655 def redirection_uri (self ):
653- return _UriOAuthService .REDIRECTION_URI [(self .environment , self ._require_live_connect )]
656+ return _UriOAuthService .REDIRECTION_URI [(self .environment , self ._oauth_scope )]
654657
655658
656659class _UriOAuthService :
@@ -659,21 +662,29 @@ class _UriOAuthService:
659662 def __init__ (self ):
660663 pass
661664
662- REDIRECTION_URI = {(PRODUCTION , True ):'https://login.live.com/oauth20_desktop.srf' ,
663- (PRODUCTION , False ):'https://login.microsoftonline.com/common/oauth2/nativeclient' ,
664- (SANDBOX , False ):'https://login.live-int.com/oauth20_desktop.srf'
665+ REDIRECTION_URI = {
666+ (PRODUCTION , MSADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/nativeclient' ,
667+ (PRODUCTION , ADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/nativeclient' ,
668+ (PRODUCTION , BINGADS_MANAGE ): 'https://login.live.com/oauth20_desktop.srf' ,
669+ (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/common/oauth2/nativeclient'
665670 }
666- AUTH_TOKEN_URI = {(PRODUCTION , True ):'https://login.live.com/oauth20_token.srf' ,
667- (PRODUCTION , False ):'https://login.microsoftonline.com/common/oauth2/v2.0/token' ,
668- (SANDBOX , False ):'https://login.live-int.com/oauth20_token.srf'
671+ AUTH_TOKEN_URI = {
672+ (PRODUCTION , MSADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/token' ,
673+ (PRODUCTION , ADS_MANAGE ): 'https://login.microsoftonline.com/common/oauth2/v2.0/token' ,
674+ (PRODUCTION , BINGADS_MANAGE ): 'https://login.live.com/oauth20_token.srf' ,
675+ (SANDBOX , MSADS_MANAGE ): 'https://login.windows-ppe.net/consumers/oauth2/v2.0/token'
669676 }
670- AUTHORIZE_URI = {(PRODUCTION , True ):'https://login.live.com/oauth20_authorize.srf?client_id={0}&scope=bingads.manage&response_type={1}&redirect_uri={2}' ,
671- (PRODUCTION , False ):'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}' ,
672- (SANDBOX , False ):'https://login.live-int.com/oauth20_authorize.srf?client_id={0}&scope=bingads.manage&response_type={1}&redirect_uri={2}&prompt=login'
677+ 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}' ,
679+ (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}' ,
680+ (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'
673682 }
674- SCOPE = {(PRODUCTION , True ):'bingads.manage' ,
675- (PRODUCTION , False ):'https://ads.microsoft.com/ads.manage offline_access' ,
676- (SANDBOX , False ):'bingads.manage'
683+ 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' ,
686+ (PRODUCTION , BINGADS_MANAGE ): 'bingads.manage' ,
687+ (SANDBOX , MSADS_MANAGE ): 'https://api.ads.microsoft.com/msads.manage offline_access'
677688 }
678689
679690 @staticmethod
@@ -689,10 +700,10 @@ def get_access_token(**kwargs):
689700 if 'client_secret' in kwargs and kwargs ['client_secret' ] is None :
690701 del kwargs ['client_secret' ]
691702
692- if 'requireliveconnect ' in kwargs and kwargs ['requireliveconnect ' ] == True :
703+ if 'oauth_scope ' in kwargs and kwargs ['oauth_scope ' ] == 'bingads.manage' :
693704 del kwargs ['tenant' ]
694705
695- auth_token_url = _UriOAuthService .AUTH_TOKEN_URI [(kwargs ['environment' ], kwargs ['requireliveconnect ' ])]
706+ auth_token_url = _UriOAuthService .AUTH_TOKEN_URI [(kwargs ['environment' ], kwargs ['oauth_scope ' ])]
696707
697708 if 'tenant' in kwargs and kwargs ['tenant' ] is not None :
698709 auth_token_url = auth_token_url .replace ('common' , kwargs ['tenant' ])
0 commit comments