|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright 2012 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""This example adds a new ad unit to a publisher ad client. |
| 18 | +
|
| 19 | +To get ad clients, run get_all_ad_clients_for_publisher.py. |
| 20 | +
|
| 21 | +Tags: accounts.adunits.insert |
| 22 | +""" |
| 23 | + |
| 24 | +__author__ = 'jalc@google.com (Jose Alcerreca)' |
| 25 | + |
| 26 | +import argparse |
| 27 | +import sys |
| 28 | + |
| 29 | +from apiclient import sample_tools |
| 30 | +from oauth2client import client |
| 31 | +from sample_utils import GetUniqueName |
| 32 | + |
| 33 | +# Declare command-line flags. |
| 34 | +argparser = argparse.ArgumentParser(add_help=False) |
| 35 | +argparser.add_argument( |
| 36 | + 'account_id', |
| 37 | + help='The ID of the pub account on which to create the ad unit') |
| 38 | +argparser.add_argument( |
| 39 | + 'ad_client_id', |
| 40 | + help='The ID of the ad client on which to create the ad unit') |
| 41 | + |
| 42 | + |
| 43 | +def main(argv): |
| 44 | + # Authenticate and construct service. |
| 45 | + service, flags = sample_tools.init( |
| 46 | + argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser]) |
| 47 | + account_id = flags.account_id |
| 48 | + ad_client_id = flags.ad_client_id |
| 49 | + |
| 50 | + try: |
| 51 | + ad_unit = { |
| 52 | + 'name': 'Ad Unit #%s' % GetUniqueName(), |
| 53 | + 'contentAdsSettings': { |
| 54 | + 'backupOption': { |
| 55 | + 'type': 'COLOR', |
| 56 | + 'color': 'ffffff' |
| 57 | + }, |
| 58 | + 'size': 'SIZE_200_200', |
| 59 | + 'type': 'TEXT' |
| 60 | + }, |
| 61 | + 'customStyle': { |
| 62 | + 'colors': { |
| 63 | + 'background': 'ffffff', |
| 64 | + 'border': '000000', |
| 65 | + 'text': '000000', |
| 66 | + 'title': '000000', |
| 67 | + 'url': '0000ff' |
| 68 | + }, |
| 69 | + 'corners': 'SQUARE', |
| 70 | + 'font': { |
| 71 | + 'family': 'ACCOUNT_DEFAULT_FAMILY', |
| 72 | + 'size': 'ACCOUNT_DEFAULT_SIZE' |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + # Create ad unit. |
| 78 | + request = service.accounts().adunits().insert(adClientId=ad_client_id, |
| 79 | + accountId=account_id, |
| 80 | + body=ad_unit) |
| 81 | + |
| 82 | + result = request.execute() |
| 83 | + print ('Ad unit of type "%s", name "%s" and status "%s" was created.' % |
| 84 | + (result['contentAdsSettings']['type'], result['name'], |
| 85 | + result['status'])) |
| 86 | + |
| 87 | + except client.AccessTokenRefreshError: |
| 88 | + print ('The credentials have been revoked or expired, please re-run the ' |
| 89 | + 'application to re-authorize') |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + main(sys.argv) |
0 commit comments