|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright 2013 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 illustrates how to get all dimension values for a dimension. |
| 18 | +
|
| 19 | +Tags: dimensionValues.query |
| 20 | +""" |
| 21 | + |
| 22 | +__author__ = ( '[email protected] (Jonathon Imperiosi)') |
| 23 | + |
| 24 | +import argparse |
| 25 | +import pprint |
| 26 | +import sys |
| 27 | + |
| 28 | +from apiclient import sample_tools |
| 29 | +from oauth2client import client |
| 30 | + |
| 31 | +# Declare command-line flags. |
| 32 | +argparser = argparse.ArgumentParser(add_help=False) |
| 33 | +argparser.add_argument('profile_id', type=int, |
| 34 | + help='The ID of the profile to get a report for') |
| 35 | + |
| 36 | +def main(argv): |
| 37 | + # Authenticate and construct service. |
| 38 | + service, flags = sample_tools.init( |
| 39 | + argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser], |
| 40 | + scope='https://www.googleapis.com/auth/dfareporting') |
| 41 | + |
| 42 | + profile_id = flags.profile_id |
| 43 | + |
| 44 | + try: |
| 45 | + # Create the dimension to query |
| 46 | + dimension = { |
| 47 | + 'dimensionName': 'dfa:advertiser', |
| 48 | + 'startDate': '2013-01-01', |
| 49 | + 'endDate': '2013-12-31' |
| 50 | + } |
| 51 | + |
| 52 | + # Construct the request. |
| 53 | + request = service.dimensionValues().query(profileId=profile_id, |
| 54 | + body=dimension) |
| 55 | + |
| 56 | + while request is not None: |
| 57 | + # Execute request and print response. |
| 58 | + response = request.execute() |
| 59 | + pprint.pprint(response) |
| 60 | + |
| 61 | + nextPageToken = response.get('nextPageToken') |
| 62 | + |
| 63 | + if nextPageToken and nextPageToken != '0': |
| 64 | + request = service.dimensionValues().query_next(request, response) |
| 65 | + else: |
| 66 | + request = None |
| 67 | + except client.AccessTokenRefreshError: |
| 68 | + print ('The credentials have been revoked or expired, please re-run the ' |
| 69 | + 'application to re-authorize') |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + main(sys.argv) |
0 commit comments