def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id advertiser_id = str(flags.advertiser_id) list_id = flags.remarketing_list_id try: # Load the existing share info. share = service.remarketingListShares().get( profileId=profile_id, remarketingListId=list_id).execute() share['sharedAdvertiserIds'] = share.get('sharedAdvertiserIds', []) if advertiser_id not in share['sharedAdvertiserIds']: share['sharedAdvertiserIds'].append(advertiser_id) # Update the share info with the newly added advertiser ID. response = service.remarketingListShares().update( profileId=profile_id, body=share).execute() print ('Remarketing list %s is now shared to advertiser ID(s): %s.' % (list_id, ','.join(response['sharedAdvertiserIds']))) else: print ('Remarketing list %s is already shared to advertiser %s' % (list_id, advertiser_id)) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def shortener(argv, input): service, flags = sample_tools.init( argv, 'urlshortener', 'v1', __doc__, __file__, scope='https://www.googleapis.com/auth/urlshortener') try: url = service.url() # Create a shortened URL by inserting the URL into the url collection. body = {'longUrl': input} resp = url.insert(body=body).execute() # pprint.pprint(resp) short_url = resp['id'] print(short_url) # # Convert the shortened URL back into a long URL # resp = url.get(shortUrl=short_url).execute() # pprint.pprint(resp) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run' 'the application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id subaccount_id = flags.subaccount_id try: # Construct and execute the subaccount request. request = service.subaccounts().get( profileId=profile_id, id=subaccount_id) subaccount = request.execute() # Construct the user role permissions request. request = service.userRolePermissions().list( profileId=profile_id, ids=subaccount['availablePermissionIds']) # Execute request and print response. result = request.execute() for permission in result['userRolePermissions']: print ('Found user role permission with ID %s and name "%s".' % (permission['id'], permission['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id report_id = flags.report_id try: # Construct a get request for the specified report. request = service.reports().files().list( profileId=profile_id, reportId=report_id) while True: # Execute request and print response. response = request.execute() for report_file in response['items']: print ('Report file with ID %s and file name "%s" has status %s.' % (report_file['id'], report_file['fileName'], report_file['status'])) if response['items'] and response['nextPageToken']: request = service.reports().files().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id try: # Limit the fields returned fields = 'nextPageToken,userRoles(accountId,id,name,subaccountId)' # Construct the request. request = service.userRoles().list(profileId=profile_id, fields=fields) while True: # Execute request and print response. response = request.execute() for user_role in response['userRoles']: print ('Found user role with ID %s and name "%s".' % (user_role['id'], user_role['name'])) if response['userRoles'] and response['nextPageToken']: request = service.userRoles().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'analytics', 'v3', __doc__, __file__, scope='https://www.googleapis.com/auth/analytics.readonly') today = date.today() amo = today - relativedelta(days=+31) # Try to make a request to the API. Print the results or handle errors. try: first_profile_id = get_first_profile_id(service) if not first_profile_id: print 'Could not find a valid profile for this user.' else: results = get_top_keywords(service, first_profile_id, amo, today) print_results(results) except TypeError, error: # Handle errors in constructing a query. print('There was an error in constructing your query : %s' % error)
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id advertiser_id = flags.advertiser_id try: # Construct the request. request = service.floodlightActivities().list( profileId=profile_id, advertiserId=advertiser_id) while True: # Execute request and print response. response = request.execute() for activity in response['floodlightActivities']: print ('Found floodlight activity with ID %s and name "%s".' % (activity['id'], activity['name'])) if response['floodlightActivities'] and response['nextPageToken']: request = service.floodlightActivities().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, _ = sample_tools.init( argv, 'adexchangeseller', 'v2.0', __doc__, __file__, parents=[], scope='https://www.googleapis.com/auth/adexchange.seller.readonly') try: # Retrieve preferred deals list in pages and display data as we receive it. request = service.accounts().preferreddeals().list(accountId='myaccount') if request: result = request.execute() if 'items' in result: deals = result['items'] for deal in deals: output = 'Deal id "%s" ' % deal['id'] if 'advertiserName' in deal: output += 'for advertiser "%s" ' % deal['advertiserName'] if 'buyerNetworkName' in deal: output += 'on network "%s" ' % deal['buyerNetworkName'] output += 'was found.' print output else: print 'No preferred deals found!' except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=[ 'https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking' ]) profile_id = flags.profile_id height = flags.height width = flags.width try: # Construct the request. request = service.sizes().list(profileId=profile_id, height=height, width=width) # Execute request and print response. result = request.execute() for size in result['sizes']: print 'Found size with ID %s.' % (size['id']) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'content', 'v2', __doc__, __file__, parents=[argparser]) merchant_id = flags.merchant_id product_id = flags.product_id new_status = { 'availability': 'out of stock', 'price': {'value': 3.00, 'currency': 'USD'}} request = service.inventory().set( merchantId=merchant_id, storeCode=product_id.split(':')[0], productId=product_id, body=new_status) try: unused_result = request.execute() print 'Product with ID "%s" was updated.' % (product_id,) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id advertiser_id = flags.advertiser_id try: # Construct the request. request = service.changeLogs().list( profileId=profile_id, objectIds=[advertiser_id], objectType='OBJECT_ADVERTISER') while True: # Execute request and print response. response = request.execute() for change_log in response['changeLogs']: print( '%s: Field "%s" from "%s" to "%s".' % (change_log['action'], change_log['fieldName'], change_log['oldValue'], change_log['newValue'])) if response['changeLogs'] and response['nextPageToken']: request = service.changeLogs().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, unused_flags = sample_tools.init( argv, 'adsense', 'v1.4', __doc__, __file__, parents=[], scope='https://www.googleapis.com/auth/adsense.readonly') try: # Let the user pick account if more than one. account_id = get_account_id(service) # Retrieve payments list in pages and display data as we receive it. request = service.accounts().payments().list(accountId=account_id) if request is not None: result = request.execute() if 'items' in result: payments = result['items'] for payment in payments: print ('Payment with id "%s" of %s %s and date %s was found. ' % (str(payment['id']), payment['paymentAmount'], payment['paymentAmountCurrencyCode'], payment.get('paymentDate', 'unknown'))) else: print 'No payments found!' except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser]) ad_client_id = flags.ad_client_id account_id = flags.account_id try: # Retrieve ad unit list in pages and display data as we receive it. request = service.accounts().adunits().list(adClientId=ad_client_id, accountId=account_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if 'items' in result: ad_units = result['items'] for ad_unit in ad_units: print ('Ad unit with ID "%s", code "%s", name "%s" and status "%s" ' 'was found.' % (ad_unit['id'], ad_unit['code'], ad_unit['name'], ad_unit['status'])) request = service.accounts().adunits().list_next(request, result) else: print 'No ad units were found.' break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id field_id = flags.field_id try: # Construct the request. request = service.creativeFieldValues().list( profileId=profile_id, creativeFieldId=field_id) while True: # Execute request and print response. response = request.execute() for value in response['creativeFieldValues']: print ('Found creative field value with ID %s and value "%s".' % (value['id'], value['value'])) if response['creativeFieldValues'] and response['nextPageToken']: request = service.creativeFieldValues().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=[ 'https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking' ]) profile_id = flags.profile_id advertiser_id = flags.advertiser_id try: # Construct the request. request = service.floodlightActivityGroups().list( profileId=profile_id, advertiserId=advertiser_id) # Execute request and print response. response = request.execute() for group in response['floodlightActivityGroups']: print('Found floodlight activity group with ID %s and name "%s".' % (group['id'], group['name'])) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=[ 'https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking' ]) profile_id = flags.profile_id activity_id = flags.activity_id try: # Construct the request. request = service.floodlightActivities().generatetag( profileId=profile_id, floodlightActivityId=activity_id) # Execute request and print response. response = request.execute() print response['floodlightActivityTag'] except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id advertiser_id = flags.advertiser_id try: # Construct the basic creative structure. creative = { 'advertiserId': advertiser_id, 'name': 'Test tracking creative', 'type': 'TRACKING_TEXT' } request = service.creatives().insert(profileId=profile_id, body=creative) # Execute request and print response. response = request.execute() print ('Created tracking creative with ID %s and name "%s".' % (response['id'], response['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init(argv, 'content', 'v2', __doc__, __file__, parents=[argparser]) merchant_id = flags.merchant_id adwords_id = flags.adwords_id try: # First we need to retrieve the existing set of users. response = service.accounts().get(merchantId=merchant_id, accountId=merchant_id, fields='adwordsLinks').execute() account = response # Add new user to existing user list. adwords_link = {'adwordsId': adwords_id, 'status': 'active'} account.setdefault('adwordsLinks', []).append(adwords_link) # Patch account with new user list. response = service.accounts().patch(merchantId=merchant_id, accountId=merchant_id, body=account).execute() print 'AdWords ID %s was added to merchant ID %s' % (adwords_id, merchant_id) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'plus', 'v1', __doc__, __file__, scope='https://www.googleapis.com/auth/plus.me') try: person = service.people().get(userId='me').execute() print 'Got your ID: %s' % person['displayName'] print print '%-040s -> %s' % ('[Activitity ID]', '[Content]') # Don't execute the request until we reach the paging loop below. request = service.activities().list( userId=person['id'], collection='public') # Loop over every activity and print the ID and a short snippet of content. while request is not None: activities_doc = request.execute() for item in activities_doc.get('items', []): print '%-040s -> %s' % (item['id'], item['object']['content'][:30]) request = service.activities().list_next(request, activities_doc) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run' 'the application to re-authorize.')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.3', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id field_id = flags.field_id try: # Construct and save creative field value field_value = { 'value': 'Test Creative Field Value' } request = service.creativeFieldValues().insert( profileId=profile_id, creativeFieldId=field_id, body=field_value) # Execute request and print response. response = request.execute() print ('Created creative field value with ID %s and value "%s".' % (response['id'], response['value'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id campaign_id = flags.campaign_id try: # Construct and save event tag. event_tag = { 'campaignId': campaign_id, 'name': 'Test Campaign Event Tag', 'status': 'ENABLED', 'type': 'CLICK_THROUGH_EVENT_TAG', 'url': 'https://www.google.com' } request = service.eventTags().insert(profileId=profile_id, body=event_tag) # Execute request and print response. response = request.execute() print ('Created campaign event tag with ID %s and name "%s".' % (response['id'], response['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init(argv, 'content', 'v2', __doc__, __file__, parents=[argparser]) merchant_id = flags.merchant_id try: request = service.accounts().list(merchantId=merchant_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() if 'resources' in result: accounts = result['resources'] for account in accounts: print('Account "%s" with name "%s" was found.' % (account['id'], account['name'])) request = service.accounts().list_next(request, result) else: print 'No accounts were found.' break except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id try: # Construct a get request for the specified profile. request = service.files().list(profileId=profile_id) while True: # Execute request and print response. response = request.execute() for report_file in response['items']: print ('File with ID %s and file name "%s" has status "%s".' % (report_file['id'], report_file['fileName'], report_file['status'])) if response['items'] and response['nextPageToken']: request = service.files().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init(argv, 'content', 'v2', __doc__, __file__, parents=[argparser]) merchant_id = flags.merchant_id product_id = flags.product_id new_status = { 'availability': 'out of stock', 'price': { 'value': 3.00, 'currency': 'USD' } } request = service.inventory().set(merchantId=merchant_id, storeCode=product_id.split(':')[0], productId=product_id, body=new_status) try: unused_result = request.execute() print 'Product with ID "%s" was updated.' % (product_id, ) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id try: # Construct and save advertiser. advertiser_group = { 'name': 'Test Advertiser Group' } request = service.advertiserGroups().insert( profileId=profile_id, body=advertiser_group) # Execute request and print response. response = request.execute() print ('Created advertiser group with ID %s and name "%s".' % (response['id'], response['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=[ 'https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking' ]) report_id = flags.report_id file_id = flags.file_id try: # Construct the request. request = service.files().get_media(reportId=report_id, fileId=file_id) # Execute request and print the file contents print request.execute() except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[], scope='https://www.googleapis.com/auth/adexchange.seller.readonly') try: # Retrieve ad client list in pages and display data as we receive it. request = service.adclients().list(maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() ad_clients = result['items'] for ad_client in ad_clients: print('Ad client for product "%s" with ID "%s" was found. ' % (ad_client['productCode'], ad_client['id'])) print('\tSupports reporting: %s' % (ad_client['supportsReporting'] and 'Yes' or 'No')) request = service.adclients().list_next(request, result) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=[ 'https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking' ]) profile_id = flags.profile_id report_id = flags.report_id try: # Construct the request. request = service.reports().delete(profileId=profile_id, reportId=report_id) # Execute request and print response. request.execute() print 'Successfully deleted report with ID %s.' % report_id except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # If you previously ran this app with an earlier version of the API # or if you change the list of scopes below, revoke your app's permission # here: https://accounts.google.com/IssuedAuthSubTokens # Then re-run the app to re-authorize it. service, flags = sample_tools.init( argv, 'prediction', 'v1.6', __doc__, __file__, parents=[argparser], scope=('https://www.googleapis.com/auth/prediction', 'https://www.googleapis.com/auth/devstorage.read_only')) try: # Get access to the Prediction API. papi = service.trainedmodels() calls = { "list": listModels, "train": trainThenWait, "describe": describeModel, "predict": makePredictions, "delete": deleteModel, "everything": everything } calls[flags.api_call](papi, flags) except client.AccessTokenRefreshError: print('The credentials have been revoked or expired, please re-run ' 'the application to re-authorize.')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, "adexchangeseller", "v1.1", __doc__, __file__, parents=[], scope="https://www.googleapis.com/auth/adexchange.seller.readonly", ) try: # Retrieve ad client list in pages and display data as we receive it. request = service.reports().saved().list(maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() saved_reports = result["items"] for saved_report in saved_reports: print('Saved report with ID "%s" and name "%s" was found.' % (saved_report["id"], saved_report["name"])) request = service.reports().saved().list_next(request, result) except client.AccessTokenRefreshError: print("The credentials have been revoked or expired, please re-run the " "application to re-authorize")
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'androidpublisher', 'v3', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/androidpublisher') # Process flags and read their values. package_name = flags.package_name try: edit_request = service.edits().insert(body={}, packageName=package_name) result = edit_request.execute() edit_id = result['id'] apks_result = service.edits().apks().list( editId=edit_id, packageName=package_name).execute() for apk in apks_result['apks']: print('versionCode: %s, binary.sha1: %s' % (apk['versionCode'], apk['binary']['sha1'])) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init(argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser]) account_id = flags.account_id ad_client_id = flags.ad_client_id ad_unit_id = flags.ad_unit_id try: ad_unit = {'customStyle': {'colors': {'text': 'ff0000'}}} # Update ad unit text color. request = service.accounts().adunits().patch(accountId=account_id, adClientId=ad_client_id, adUnitId=ad_unit_id, body=ad_unit) result = request.execute() print('Ad unit with ID "%s" was updated with text color "%s".' % (result['id'], result['customStyle']['colors']['text'])) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id advertiser_id = flags.advertiser_id try: # Construct and save creative field creative_field = { 'advertiserId': advertiser_id, 'name': 'Test Creative Field' } request = service.creativeFields().insert( profileId=profile_id, body=creative_field) # Execute request and print response. response = request.execute() print ('Created creative field with ID %s and name "%s".' % (response['id'], response['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): dbconfig_redflags, dbconfig_accusations, dbconfig_messages = db_conn() READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose service, flags = sample_tools.init( argv, 'prediction', 'v1.6', __doc__, __file__, parents=[argparser], scope=('https://www.googleapis.com/auth/prediction', 'https://www.googleapis.com/auth/devstorage.read_only')) if slack_client.rtm_connect(): print("FowlerBot connected and running!") while True: user, userid, text, channel = parse_slack_output( slack_client.rtm_read()) if channel == 'D4D3GBG0Z' or channel == 'D4DSX1674' or channel == 'D4EHJ93UN': sexual_harassment_complaint(text, userid, dbconfig_accusations) elif user and text: handle_command(user, userid, text, service, flags, channel, dbconfig_redflags, dbconfig_messages) time.sleep(READ_WEBSOCKET_DELAY) else: print("Connection failed. Invalid Slack token or bot ID?")
def main(argv): # Authenticate and construct service. service, _ = sample_tools.init( argv, 'adexchangeseller', 'v2.0', __doc__, __file__, parents=[], scope='https://www.googleapis.com/auth/adexchange.seller.readonly') try: # Retrieve ad client list in pages and display data as we receive it. request = service.accounts().reports().saved().list( accountId='myaccount', maxResults=MAX_PAGE_SIZE) while request: result = request.execute() if 'items' in result: saved_reports = result['items'] for saved_report in saved_reports: print( 'Saved report with ID "%s" and name "%s" was found.' % (saved_report['id'], saved_report['name'])) request = service.accounts().reports().saved().list_next( request, result) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id report_id = flags.report_id try: # Create a report resource with the fields to patch report = { 'criteria': { 'dateRange': {'relativeDateRange': 'YESTERDAY'} } } # Construct the request. request = service.reports().patch( profileId=profile_id, reportId=report_id, body=report) # Execute request and print response. result = request.execute() print ('Successfully patched %s report with ID %s.' % (result['type'], result['id'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id account_id = flags.account_id parent_role_id = flags.parent_role_id try: # Construct the basic user role structure. user_role = { 'name': 'Test User Role', 'accountId': account_id, 'parentUserRoleId': parent_role_id } request = service.userRoles().insert(profileId=profile_id, body=user_role) # Execute request and print response. response = request.execute() print ('Created user role with ID %s and name "%s".' % (response['id'], response['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/dfareporting') profile_id = flags.profile_id try: # Create a new report resource to insert report = { 'name': 'Example Standard Report', 'type': 'STANDARD', 'criteria': { 'dateRange': {'relativeDateRange': 'YESTERDAY'}, 'dimensions': [{'name': 'dfa:campaign'}], 'metricNames': ['dfa:clicks'] } } # Construct the request. request = service.reports().insert(profileId=profile_id, body=report) # Execute request and print response. pprint.pprint(request.execute()) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init(argv, 'content', 'v2', __doc__, __file__, parents=[argparser]) merchant_id = flags.merchant_id datafeed_id = flags.datafeed_id try: # Changing the scheduled fetch time to 7:00. request = service.datafeeds().patch( merchantId=merchant_id, datafeedId=datafeed_id, body={'fetchSchedule': { 'hour': 7 }}) result = request.execute() print('Datafeed with ID "%s" and fetchSchedule %s was updated.' % (result['id'], str(result['fetchSchedule']))) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, unused_flags = sample_tools.init( argv, 'adsense', 'v1.4', __doc__, __file__, parents=[], scope='https://www.googleapis.com/auth/adsense.readonly') try: account_id = get_account_id(service) # Retrieve ad client list in pages and display data as we receive it. request = service.accounts().adclients().list(accountId=account_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() ad_clients = result['items'] for ad_client in ad_clients: print ('Ad client for product "%s" with ID "%s" was found. ' % (ad_client['productCode'], ad_client['id'])) print ('\tSupports reporting: %s' % (ad_client['supportsReporting'] and 'Yes' or 'No')) request = service.accounts().adclients().list_next(request, result) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, "dfareporting", "v2.3", __doc__, __file__, parents=[argparser], scope=["https://www.googleapis.com/auth/dfareporting", "https://www.googleapis.com/auth/dfatrafficking"], ) profile_id = flags.profile_id try: # Construct the request. request = service.campaigns().list(profileId=profile_id) while True: # Execute request and print response. response = request.execute() for campaign in response["campaigns"]: print('Found campaign with ID %s and name "%s".' % (campaign["id"], campaign["name"])) if response["campaigns"] and response["nextPageToken"]: request = service.campaigns().list_next(request, response) else: break except client.AccessTokenRefreshError: print("The credentials have been revoked or expired, please re-run the " "application to re-authorize")
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adexchangeseller', 'v1.1', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/adexchange.seller.readonly') ad_client_id = flags.ad_client_id try: # Retrieve URL channel list in pages and display data as we receive it. request = service.urlchannels().list(adClientId=ad_client_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() url_channels = result['items'] for url_channel in url_channels: print ('URL channel with URL pattern "%s" was found.' % url_channel['urlPattern']) request = service.customchannels().list_next(request, result) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/dfareporting') profile_id = flags.profile_id report_id = flags.report_id file_id = flags.file_id try: # Construct a get request for the specified report. request = service.reports().files().get(fileId=file_id, profileId=profile_id, reportId=report_id) # Execute request and print response. pprint.pprint(request.execute()) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=[ 'https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking' ]) profile_id = flags.profile_id campaign_id = flags.campaign_id placement_id = flags.placement_id try: # Construct the request. request = service.placements().generatetags( profileId=profile_id, campaignId=campaign_id, placementIds=[placement_id]) # Execute request and print response. response = request.execute() for placement_tag in response['placementTags']: print_placement_tag(placement_tag) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id ad_id = flags.ad_id try: # Retrieve the ad. ad = service.ads().get(profileId=profile_id, id=ad_id).execute() # Retrieve a single targetable remarketing list for the ad. lists = service.targetableRemarketingLists().list( profileId=profile_id, advertiserId=ad['advertiserId'], maxResults=1).execute() if lists['targetableRemarketingLists']: list = lists['targetableRemarketingLists'][0] # Update the ad with a list targeting expression ad['remarketing_list_expression'] = { 'expression': list['id'] } response = service.ads().update(profileId=profile_id, body=ad).execute() print ('Ad %s updated to use remarketing list expression: "%s".' % (response['id'], response['remarketing_list_expression']['expression'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adsense', 'v1.3', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/adsense.readonly') ad_client_id = flags.ad_client_id try: # Retrieve ad unit list in pages and display data as we receive it. request = service.adunits().list(adClientId=ad_client_id, maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() ad_units = result['items'] for ad_unit in ad_units: print ('Ad unit with code "%s", name "%s" and status "%s" was found. ' % (ad_unit['code'], ad_unit['name'], ad_unit['status'])) request = service.adunits().list_next(request, result) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id advertiser_id = flags.advertiser_id try: # Construct and save campaign. campaign = { 'name': 'Test Campaign', 'advertiserId': advertiser_id, 'archived': 'false', 'startDate': '2014-01-01', 'endDate': '2020-01-01' } request = service.campaigns().insert( profileId=profile_id, defaultLandingPageName='Test Landing Page', defaultLandingPageUrl='http://www.google.com', body=campaign) # Execute request and print response. response = request.execute() print ('Created campaign with ID %s and name "%s".' % (response['id'], response['name'])) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/dfareporting') profile_id = flags.profile_id report_id = flags.report_id try: # Create a report resource with the fields to patch report = { 'criteria': { 'dateRange': { 'relativeDateRange': 'YESTERDAY' } } } # Construct the request. request = service.reports().patch(profileId=profile_id, reportId=report_id, body=report) # Execute request and print response. pprint.pprint(request.execute()) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id campaign_id = flags.campaign_id creative_id = flags.creative_id try: # Construct the request. association = { 'creativeId': creative_id } request = service.campaignCreativeAssociations().insert( profileId=profile_id, campaignId=campaign_id, body=association) # Execute request and print response. response = request.execute() print ('Creative with ID %s is now associated with campaign %s.' % (response['creativeId'], campaign_id)) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init(argv, 'content', 'v2', __doc__, __file__, parents=[argparser]) merchant_id = flags.merchant_id try: name = 'account%s' % shopping_common.get_unique_id() account = { 'name': name, 'websiteUrl': 'https://%s.example.com/' % (name, ) } # Add account. request = service.accounts().insert(merchantId=merchant_id, body=account) result = request.execute() print('Created account ID "%s" for MCA "%s".' % (result['id'], merchant_id)) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'analytics', 'v3', __doc__, __file__, scope='https://www.googleapis.com/auth/analytics.readonly') # Try to make a request to the API. Print the results or handle errors. try: profile_id = profile_ids[profile] if not profile_id: print 'No valid profile for user.' else: for start_date, end_date in date_ranges: limit = ga_query(service, profile_id, 0, start_date, end_date).get('totalResults') for pag_index in xrange(0, limit, 1000000): results = ga_query(service, profile_id, pag_index, start_date, end_date) #if results.get('containsSampledData'): # raise SampledDataError # if you dont mind sampled data, put a #in from of the raise sampleldata print_results(results, pag_index, start_date, end_date) except TypeError, error: # Handle errors in constructing a query. print ('Error constructing query : %s' % error)
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.0', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id report_id = flags.report_id try: # Retrieve the specified report resource report = service.reports().get( profileId=profile_id, reportId=report_id).execute() compatible_fields_type = get_compatible_fields_type(report['type']) # Construct the request request = service.reports().compatibleFields().query( profileId=profile_id, body=report) # Execute the request and print response. response = request.execute() compatible_fields = response[compatible_fields_type] print_fields('Dimensions', compatible_fields['dimensions']) print_fields('Metrics', compatible_fields['metrics']) print_fields('Dimension Filters', compatible_fields['dimensionFilters']) print_fields('Pivoted Activity Metrics', compatible_fields['pivotedActivityMetrics']) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adsense', 'v1.2', __doc__, __file__, parents=[], scope='https://www.googleapis.com/auth/adsense.readonly') try: # Retrieve account list in pages and display data as we receive it. request = service.accounts().list(maxResults=MAX_PAGE_SIZE) while request is not None: result = request.execute() accounts = result['items'] for account in accounts: print('Account with ID "%s" and name "%s" was found. ' % (account['id'], account['name'])) request = service.accounts().list_next(request, result) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.2', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id try: # Construct the request. request = service.placements().list(profileId=profile_id) while True: # Execute request and print response. response = request.execute() for placement in response['placements']: print ('Found placement with ID %s and name "%s" for campaign "%s".' % (placement['id'], placement['name'], placement['campaignId'])) if response['placements'] and response['nextPageToken']: request = service.placements().list_next(request, response) else: break except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'dfareporting', 'v2.1', __doc__, __file__, parents=[argparser], scope=['https://www.googleapis.com/auth/dfareporting', 'https://www.googleapis.com/auth/dfatrafficking']) profile_id = flags.profile_id campaign_id = flags.campaign_id placement_id = flags.placement_id try: # Construct the request. request = service.placements().generatetags( profileId=profile_id, campaignId=campaign_id, placementIds=[placement_id]) # Execute request and print response. response = request.execute() for placement_tag in response['placementTags']: print_placement_tag(placement_tag) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'audit', 'v1', __doc__, __file__, scope='https://www.googleapis.com/auth/apps/reporting/audit.readonly') service = build('audit', 'v1', http=http) try: activities = service.activities() # Retrieve the first two activities print 'Retrieving the first 2 activities...' activity_list = activities.list( applicationId='207535951991', customerId='C01rv1wm7', maxResults='2', actorEmail='*****@*****.**').execute() pprint.pprint(activity_list) # Now retrieve the next 2 events match = re.search('(?<=continuationToken=).+$', activity_list['next']) if match is not None: next_token = match.group(0) print '\nRetrieving the next 2 activities...' activity_list = activities.list( applicationId='207535951991', customerId='C01rv1wm7', maxResults='2', actorEmail='*****@*****.**', continuationToken=next_token).execute() pprint.pprint(activity_list) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run' 'the application to re-authorize')