def getManagedCustomer(client_customer_id, refresh_token): oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, refresh_token) try: adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) except: print('Oauth error for %s' % client_customer_id) traceback.print_exc() return None criterion_service = adwords_client.GetService('AdGroupCriterionService', version=ADWORDS_API_VERSION) selector = { 'fields': ['KeywordText', 'Id', 'KeywordMatchType', 'CriteriaType'], 'predicates': [{ 'field': 'AdGroupId', 'operator': 'IN', 'values': [21902709882] }, {'field': 'CriteriaType', 'operator': 'NOT_IN', 'values': ['USER_LIST','USER_INTEREST','PRODUCT_PARTITION' ]} ] } result = criterion_service.get(selector) print(result)
def feedItems(client_customer_id, refresh_token): oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, refresh_token) try: adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) except: print('Oauth error for %s' % client_customer_id) traceback.print_exc() return None # feed_mapping_service = adwords_client.GetService('FeedMappingService', version='v201806') # feed_item_service = adwords_client.GetService('FeedItemService', version='v201710') campaign_feed_service = adwords_client.GetService('CampaignFeedService', 'v201806') PAGE_SIZE = 1 campaign_feeds = [] more_pages = True selector = { 'fields': ['PlaceholderTypes', 'Status', 'MatchingFunction'], 'predicates': [{ 'field': 'Status', 'operator': 'EQUALS', 'values': ['ENABLED'] }, { 'field': 'FeedId', 'operator': 'IN', 'values': [75151834] }, { 'field': 'PlaceholderTypes', 'operator': 'CONTAINS_ANY', 'values': [2] }], 'paging': { 'startIndex': 0, 'numberResults': PAGE_SIZE } } while more_pages: page = campaign_feed_service.get(selector) print(page['totalNumEntries']) if 'entries' in page: campaign_feeds.extend(page['entries']) selector['paging']['startIndex'] += PAGE_SIZE more_pages = selector['paging']['startIndex'] < int( page['totalNumEntries']) # for entry in campaign_feeds: # print(entry) print(campaign_feeds)
def check_config(self, logger: AirbyteLogger, config_path: str, config: json) -> AirbyteConnectionStatus: # singer catalog that attempts to pull a stream ("accounts") that should always exists, though it may be empty. try: customer_ids = config["customer_ids"].split(",") for customer_id in customer_ids: oauth2_client = oauth2.GoogleRefreshTokenClient( config["oauth_client_id"], config["oauth_client_secret"], config["refresh_token"]) sdk_client = adwords.AdWordsClient( config["developer_token"], oauth2_client, user_agent=config["user_agent"], client_customer_id=customer_id) selector = { "fields": [ "Name", "CanManageClients", "CustomerId", "TestAccount", "DateTimeZone", "CurrencyCode" ], } accounts = self._get_accounts(logger, sdk_client, selector) if not accounts: err = f"No accounts associated with customer id {customer_id}" error_msg = f"Unable to connect with the provided credentials. Error: {err}" return AirbyteConnectionStatus(status=Status.FAILED, message=error_msg) return AirbyteConnectionStatus(status=Status.SUCCEEDED) except Exception as e: return AirbyteConnectionStatus(status=Status.FAILED, message=f"{str(e)}")
def get_adwords_client(mcc): oauth2_client = oauth2.GoogleRefreshTokenClient(client_id=adwords_mccess.get(mcc).client_id, client_secret=adwords_mccess.get(mcc).client_secret, refresh_token=adwords_mccess.get(mcc).refresh_token) adwords_client = adwords.AdWordsClient(adwords_mccess.get(mcc).developer_token, oauth2_client, adwords_mccess.get(mcc).user_agent, client_customer_id=adwords_mccess.get(mcc).client_customer_id) return adwords_client
def feedItems(client_customer_id, refresh_token): oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, refresh_token) try: adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) except: print('Oauth error for %s' % client_customer_id) traceback.print_exc() return None # feed_mapping_service = adwords_client.GetService('FeedMappingService', version='v201710') # feed_item_service = adwords_client.GetService('FeedItemService', version='v201710') conversion_tracker_service = adwords_client.GetService( 'ConversionTrackerService', 'v201806') # NOTE: paging might not be working for this service so make sure you're pulling them all at once PAGE_SIZE = 10000 conversions = [] more_pages = True selector = { 'fields': ['Id', 'Name', 'Status'], 'predicates': [{ 'field': 'Name', 'operator': 'CONTAINS', 'values': ['z-att'] }, { 'field': 'Status', 'operator': 'IN', 'values': ['ENABLED'] }], 'paging': { 'startIndex': 0, 'numberResults': PAGE_SIZE } } while more_pages: page = conversion_tracker_service.get(selector) print(page['totalNumEntries']) if 'entries' in page: for entry in page['entries']: # puts the conversions into the format needed to insert (almost) conversions.append( [client_customer_id, entry['id'], entry['name']]) selector['paging']['startIndex'] += PAGE_SIZE more_pages = selector['paging']['startIndex'] < int( page['totalNumEntries']) print(conversions)
def __init__(self): # Initialize client object. oauth2_client = oauth2.GoogleRefreshTokenClient( CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN) adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=CLIENT_CUSTOMER_ID) self.ins=adwords_client print("Client got")
def _get_oauth_client(self) -> GoogleOAuth2Client: """ Create an OAuth refresh-token client for all google adwords and manager api Returns: GoogleOAuth2Client """ return oauth2.GoogleRefreshTokenClient( client_id=self.__settings.client_id, client_secret=self.__settings.secrets_manager.client_secret, refresh_token=self.__settings.secrets_manager.refresh_token)
def feedItems(client_customer_id, refresh_token): oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, refresh_token) try: adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) except: print('Oauth error for %s' % client_customer_id) traceback.print_exc() return None campaign_extension_service = adwords_client.GetService( 'AdGroupExtensionSettingService', 'v201806') PAGE_SIZE = 1 campaign_extensions = [] more_pages = True selector = { 'fields': ['AdGroupId', 'ExtensionType', 'Extensions'], # 'predicates': [ # # { # # 'field': 'Status', # # 'operator': 'EQUALS', # # 'values': ['ENABLED'] # # }, 1419349875 # # { # # 'field': 'CampaignId', # # 'operator': 'IN', # # 'values': [1418802235] # # } # ], 'paging': { 'startIndex': 0, 'numberResults': PAGE_SIZE } } while more_pages: page = campaign_extension_service.get(selector) print(page['totalNumEntries']) if 'entries' in page: campaign_extensions.extend(page['entries']) selector['paging']['startIndex'] += PAGE_SIZE more_pages = selector['paging']['startIndex'] < int( page['totalNumEntries']) # for entry in campaign_feeds: # print(entry) print(campaign_extensions)
def __init__(self): self.client = super(AdWordsApiClient, self).__init__( developer_token=config.developer_token(), oauth2_client=oauth2.GoogleRefreshTokenClient( client_id=config.oauth2_client_id(), client_secret=config.oauth2_client_secret(), refresh_token=config.oauth2_refresh_token()), client_customer_id=config.client_customer_id()) self.client_customers = self._fetch_client_customers()
def _create_sdk_client(self): oauth2_client = oauth2.GoogleRefreshTokenClient(self.__client_id, \ self.__client_secret, \ self.__refresh_token) sdk_client = adwords.AdWordsClient(self.__developer_token, oauth2_client, \ user_agent=self.__user_agent, \ client_customer_id=self.__customer_id) return sdk_client
def create_sdk_client(customer_id): oauth2_client = oauth2.GoogleRefreshTokenClient( CONFIG['oauth_client_id'], \ CONFIG['oauth_client_secret'], \ CONFIG['refresh_token']) sdk_client = adwords.AdWordsClient(CONFIG['developer_token'], \ oauth2_client, user_agent=CONFIG['user_agent'], \ client_customer_id=customer_id) return sdk_client
def main(client_id, client_secret, refresh_token, developer_token, user_agent, client_customer_id): oauth2_client = oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token) adwords_client = adwords.AdWordsClient(developer_token, oauth2_client, user_agent, client_customer_id) customer = adwords_client.GetService('CustomerService').get() print 'You are logged in as customer: %s' % customer['customerId']
def connect_api(request): oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN) adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, client_customer_id=CLIENT_CUSTOMER_ID) add_campaign(adwords_client) context = {'adwords_client': adwords_client} return render(request, 'index.html', context)
def createClient(client_id, client_secret, refresh_token, developer_token, client_customer_id): oauth2_client = oauth2.GoogleRefreshTokenClient( client_id, client_secret, refresh_token, access_token=os.getenv('ACCESS_TOKEN')) adwords_client = adwords.AdWordsClient( developer_token, oauth2_client, client_customer_id=client_customer_id) return adwords_client
def get_client(self): access_token = oauth2.GoogleRefreshTokenClient(self.client_ID, self.client_secret, self.refresh_token) adwords_client = adwords.AdWordsClient( self.developer_token, access_token, client_customer_id=self.client_customer_id, cache=googleads.common.ZeepServiceProxy.NO_CACHE) return adwords_client
def MyInitAdwordsClient(client_id, client_secret, refresh_token, developer_token, user_agent, client_customer_id): oauth2_client = oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token) adwords_client = adwords.AdWordsClient( developer_token, oauth2_client, user_agent, client_customer_id=client_customer_id, cache=ZeepServiceProxy.NO_CACHE) return adwords_client
def main(client_id, client_secret, refresh_token, application_name): oauth2_client = oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token) dfp_client = dfp.DfpClient(oauth2_client, application_name) networks = dfp_client.GetService('NetworkService').getAllNetworks() for network in networks: print(( 'Network with network code \'%s\' and display name \'%s\' was found.' % (network['networkCode'], network['displayName'])))
def main(client_id, client_secret, refresh_token, application_name): oauth2_client = oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token) ad_manager_client = ad_manager.AdManagerClient(oauth2_client, application_name) networks = ad_manager_client.GetService('NetworkService').getAllNetworks() for network in networks: print( 'Network with network code "%s" and display name "%s" was found.' % (network['networkCode'], network['displayName']))
def get_adwords_client(): oauth2_tokens = session[AUTH_TOKEN_KEY] oauth2_client = oauth2.GoogleRefreshTokenClient( CLIENT_ID, CLIENT_SECRET, oauth2_tokens['refresh_token'] ) adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, 'Celebration Saunas', client_customer_id=CLIENT_CUSTOMER_ID ) return adwords_client
def get_ads_service(service_name, version, oauth_credentials, developer_token, customer_id): from googleads import adwords from googleads import oauth2 oauth2_client = oauth2.GoogleRefreshTokenClient( oauth_credentials.get_client_id(), oauth_credentials.get_client_secret(), oauth_credentials.get_refresh_token()) client = adwords.AdWordsClient(developer_token, oauth2_client, 'MegaList Dataflow', client_customer_id=customer_id) return client.GetService(service_name, version=version)
def MyInitAdwordsClient(client_customer_id): CLIENT_ID = 'xxxxxxxx' CLIENT_SECRET = 'xxxxxxxx' REFRESH_TOKEN = 'xxxxxxxx' oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN) DEVELOPER_TOKEN = 'xxxxxxxx' USER_AGENT = 'BG' adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) return adwords_client
def get_adwords_client(refresh_token, client_customer_id=None): client_id = settings.ADWORDS_CLIENT_ID client_secret = settings.ADWORDS_SECRET_KEY developer_token = settings.ADWORDS_DEVELOPER_TOKEN user_agent = 'Captivise' oauth2_client = oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token) return adwords.AdWordsClient( developer_token, oauth2_client, user_agent, client_customer_id=client_customer_id, )
def main(client_id, client_secret, refresh_token, user_profile_name, application_name): oauth2_client = oauth2.GoogleRefreshTokenClient( client_id, client_secret, refresh_token) dfa_client = dfa.DfaClient(user_profile_name, oauth2_client, application_name) campaign_service = dfa_client.GetService( 'campaign', server='https://advertisersapitest.doubleclick.net') results = campaign_service.getCampaignsByCriteria({}) if results['records']: for campaign in results['records']: print ('Campaign with name \'%s\' and ID \'%s\' was found.' % (campaign['name'], campaign['id']))
def get_adwords_client(account_id, use_delivery_hero=False): token_client = oauth2.GoogleRefreshTokenClient( client_id=os.getenv('CLIENT_ID'), client_secret=os.getenv('CLIENT_SECRET'), refresh_token=_get_refresh_token(use_delivery_hero)) adwords_client = AdWordsClient( developer_token=os.getenv('ADWORDS_DEVELOPER_TOKEN'), oauth2_client=token_client, user_agent='rocketads-script') adwords_client.SetClientCustomerId(account_id) return adwords_client
def authenticate_client(self) -> AdWordsClient: """Configures an oAuth Adwords client""" oauth2_client = oauth2.GoogleRefreshTokenClient( client_id=self._oauth2_connector.config.client_id, client_secret=self._oauth2_connector.config.client_secret.get_secret_value(), refresh_token=self.get_refresh_token(), ) # Configure an AdWordsClient client = AdWordsClient( developer_token=self.developer_token, oauth2_client=oauth2_client, user_agent=USER_AGENT, client_customer_id=self.client_customer_id, ) return client
def getCMCDReport(client_customer_id, refresh_token): print("dowloading yesterday's CMCD Report") oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, refresh_token) try: adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) except: print('Oauth error for %s' % client_customer_id) traceback.print_exc() return None report_downloader = adwords_client.GetReportDownloader( version=ADWORDS_API_VERSION) print('past report downloader') report = { 'reportName': 'CALL METRICS CALL DETAILS REPORT', 'dateRangeType': 'TODAY', 'reportType': 'CALL_METRICS_CALL_DETAILS_REPORT', 'downloadFormat': 'XML', 'selector': { 'fields': [ 'AccountDescriptiveName', 'AccountTimeZone', 'AdGroupId', 'AdGroupName', 'CampaignId', 'CampaignName', 'CallDuration', 'CallStartTime', 'CallEndTime', 'CallerNationalDesignatedCode', 'CallType', ] } } report_downloader.DownloadReport(report, sys.stdout, skip_report_header=False, skip_column_header=False, skip_report_summary=False)
def _check_internal(self, logger: AirbyteLogger, streams: List, config: json): # checking if REPORT syncing will be called for manager account # https://developers.google.com/adwords/api/docs/common-errors#ReportDefinitionError.CUSTOMER_SERVING_TYPE_REPORT_MISMATCH try: customer_ids = config["customer_ids"].split(",") oauth2_client = oauth2.GoogleRefreshTokenClient( config["oauth_client_id"], config["oauth_client_secret"], config["refresh_token"]) for customer_id in customer_ids: sdk_client = adwords.AdWordsClient( config["developer_token"], oauth2_client, user_agent=config["user_agent"], client_customer_id=customer_id) selector = { "fields": [ "Name", "CanManageClients", "CustomerId", "TestAccount", "DateTimeZone", "CurrencyCode" ], "predicates": [{ "field": "CustomerId", "operator": "IN", "values": [ customer_id, ], }], } accounts = self._get_accounts(logger, sdk_client, selector) if accounts: account = accounts[0] is_manager = account.canManageClients for stream in streams: if stream.endswith("REPORT") and is_manager: logger.log_by_prefix( f"Unable to sync {stream} with the manager account {customer_id}", "ERROR") sys.exit(1) else: err = f"No accounts associated with customer id {customer_id}" logger.log_by_prefix( f"Unable to sync with the provided credentials. Error: {err}", "ERROR") sys.exit(1) except Exception as err: logger.log_by_prefix(f"Unable to sync. Error: {err}", "ERROR") sys.exit(1)
def main(client_id, client_secret, refresh_token, developer_token, user_agent, client_customer_id): oauth2_client = oauth2.GoogleRefreshTokenClient( client_id, client_secret, refresh_token) adwords_client = adwords.AdWordsClient( developer_token, oauth2_client, user_agent, client_customer_id=client_customer_id) customer_service = adwords_client.GetService('CustomerService', version='v201702') customers = customer_service.getCustomers() print('You are logged in as a user with access to the following customers:') for customer in customers: print(('\t%s' % customer['customerId']))
def getById(self, str_acc_id): account_credentials = Account.objects.get(account_id=str_acc_id).mcc client_id = account_credentials.client_id client_secret = account_credentials.client_secret refresh_token = account_credentials.refresh_token developer_token = account_credentials.dev_token user_agent = account_credentials.user_agent oauth2_client = oauth2.GoogleRefreshTokenClient( client_id, client_secret, refresh_token) adwords_client = adwords.AdWordsClient(developer_token, oauth2_client, user_agent, client_customer_id=str_acc_id) return adwords_client
def getManagedCustomer(client_customer_id, refresh_token): oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, refresh_token) try: adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT, client_customer_id=client_customer_id) except: print('Oauth error for %s' % client_customer_id) traceback.print_exc() return None customer_service = adwords_client.GetService('CustomerService', version=ADWORDS_API_VERSION) managed_customer_service = adwords_client.GetService( 'ManagedCustomerService', version=ADWORDS_API_VERSION) selector = { 'fields': ['CustomerId', 'CanManageClients'], } customers = customer_service.getCustomers() print('from customer service') print(customers) for customer in customers: if customer['customerId'] == int(client_customer_id): print('hi alex') else: print(type(customer['customerId'])) print(customer['customerId']) print('vs') print(type(client_customer_id)) print(client_customer_id) # print customer # print '\n' print('from managed customer service') page = managed_customer_service.get(selector) print(page)