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 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 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 _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 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 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 main(access_token, token_expiry, client_customer_id, developer_token, user_agent): oauth2_client = oauth2.GoogleAccessTokenClient(access_token, token_expiry) adwords_client = adwords.AdWordsClient( developer_token, oauth2_client, user_agent, client_customer_id=client_customer_id) customer = adwords_client.GetService('CustomerService').getCustomers()[0] print 'You are logged in as customer: %s' % customer['customerId']
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 main(service_account_email, key_file, service_account_user, developer_token, user_agent, client_customer_id): oauth2_client = oauth2.GoogleServiceAccountClient( oauth2.GetAPIScope('adwords'), service_account_email, key_file, sub=service_account_user) 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 getClient(user_id=None, account_id=None, client_customer_id=None): """Returns the adwords client Manager level if a user_id is provided Client (account) level if account_id and client_customer_id are provided """ helpers = Helpers() settings = Settings() if user_id is not None: refresh_token = helpers.getRefreshTokenFromUserId(user_id) else: refresh_token = helpers.getRefreshToken(account_id) if not refresh_token: Log("info", "Can't determine refresh_token for user %s " % (user_id), '', account_id) return yaml_data = helpers.getYamlData()["adwords"] client_id = yaml_data["client_id"] client_secret = yaml_data["client_secret"] developer_token = yaml_data["developer_token"] oauth_client = oauth2.GoogleRefreshTokenClient( client_id=client_id, client_secret=client_secret, refresh_token=refresh_token) if client_customer_id is not None: adwords_client = adwords.AdWordsClient( developer_token, oauth_client, client_customer_id=client_customer_id) else: adwords_client = adwords.AdWordsClient(developer_token, oauth_client) 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 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 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 from_config_parameters(cls, developer_token, client_id, client_secret, client_customer_id, refresh_token, user_agent='unknown', timeout=3600): oauth2_client = GoogleRefreshTokenClient(client_id=client_id, client_secret=client_secret, refresh_token=refresh_token) client = adwords.AdWordsClient(developer_token=developer_token, oauth2_client=oauth2_client, user_agent=user_agent, timeout=timeout, client_customer_id=client_customer_id) return cls(adwords_client=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=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 main(service_account_email, key_file, service_account_user, developer_token, user_agent, client_customer_id): oauth2_client = oauth2.GoogleServiceAccountClient( oauth2.GetAPIScope('adwords'), service_account_email, key_file, sub=service_account_user) 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 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)
def fetch_and_upload_to_s3(self, tenant, feed, connection): # credentialsName = tenant + connection['connectionId'] # kms = boto3.client('kms') # secret = credstash.getSecret( # name=credentialsName, # table='kilimanjaro-latest-credentials', # kms=kms, # context={ 'access': tenant} # ) print('beginning work') refreshToken = '1//04fBw5VSJ7oF5CgYIARAAGAQSNwF-L9IrCE53bDpnv8652TDVTEFHhhkx7mNBC-nXOfSA6I_Qp4pJqAkfDMboUlIekks9UjCiMOQ' clientId = '60914815831-2t42edddggbns2s6g7i9iurc4t9a96i6.apps.googleusercontent.com' clientSecret = 'oGP-uoA1Oc9HVtGeF8vtIYTr' developerToken = '0qPN98uVQ0jU6ZWPcfhIdw' clientCustomerId = '311-101-7656' oauth2_client = oauth2.GoogleRefreshTokenClient(clientId, clientSecret, refreshToken) adwords_client = adwords.AdWordsClient(developerToken, oauth2_client, '', client_customer_id=clientCustomerId) report_downloader = adwords_client.GetReportDownloader(version='v201809') report = { 'reportName': feed['metadata']['tableName'], 'dateRangeType': 'LAST_7_DAYS', # feed['metadata']['dateFilter'], 'reportType': 'AD_PERFORMANCE_REPORT', # TODO: from mapping 'downloadFormat': 'CSV', 'selector': { 'fields': list(map(lambda col: col['columnName'], feed['schema'])) } } print(report['reportName']) stream_data = report_downloader.DownloadReportAsStream( report, skip_report_header=False, skip_column_header=False, skip_report_summary=False, include_zero_impressions=True) # s3 = boto3.client('s3') # s3.upload_fileobj(stream_data, tenant + '/raw', feed['feedId']) # stream_data.close() print('streaming done')
def __create_client(context): client_id = context.get('GOOGLE_ADS_CLIENT_ID') client_secret = context.get('GOOGLE_ADS_CLIENT_SECRET') refresh_token = context.get('GOOGLE_ADS_REFRESH_TOKEN') developer_token = context.get('GOOGLE_ADS_DEVELOPER_TOKEN') user_agent = context.get('GOOGLE_ADS_USER_AGENT') customer_id = context.get('GOOGLE_ADS_CUSTOMER_ID') oauth2_client = oauth2.GoogleRefreshTokenClient( client_id, client_secret, refresh_token) ads_client = adwords.AdWordsClient( developer_token, oauth2_client, user_agent, client_customer_id=customer_id) ads_client.cache = common.ZeepServiceProxy.NO_CACHE return ads_client