2
votes

I want to retrieve all clientID from my MCC account. I'm using this code

              AdWordsUser user = new AdWordsUser(adwordsPropertyService.getEmail(), adwordsPropertyService.getPassword(),
                      null, adwordsPropertyService.getUseragent(), adwordsPropertyService.getDeveloperToken(),
                      adwordsPropertyService.getUseSandbox());

              InfoServiceInterface infoService = user.getService(AdWordsService.V201109.INFO_SERVICE);


              InfoSelector selector = new InfoSelector();
              selector.setApiUsageType(ApiUsageType.UNIT_COUNT_FOR_CLIENTS);
              String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
              selector.setDateRange(new DateRange(today, today));
              selector.setIncludeSubAccounts(true);

              ApiUsageInfo apiUsageInfo = infoService.get(selector);
              for (ApiUsageRecord record : apiUsageInfo.getApiUsageRecords()) {
                      ......

But apiUsageInfo.getApiUsageRecords return my only some clientId. Have you any suggests?

2

2 Answers

3
votes

My Answer will be helpful for PHP Developers

I am using v201502(php), You will get all account details from ManagedCustomerService api. Please refer the following URL https://developers.google.com/adwords/api/docs/reference/v201502/ManagedCustomerService

This is the sample code i used,

    function DisplayAccountTree($account, $link, $accounts, $links, $depth) {
      print str_repeat('-', $depth * 2);
      printf("%s, %s\n", $account->customerId, $account->name);
      if (array_key_exists($account->customerId, $links)) {
        foreach ($links[$account->customerId] as $childLink) {
          $childAccount = $accounts[$childLink->clientCustomerId];
          DisplayAccountTree($childAccount, $childLink, $accounts, $links,
              $depth +1);
        }
      }
    }
    function GetAccountHierarchyExample(AdWordsUser $user) {
      // Get the service, which loads the required classes.

      $user->SetClientCustomerId('xxx-xxx-xxxx');
      $managedCustomerService =
          $user->GetService('ManagedCustomerService');

      // Create selector.
      $selector = new Selector();
      // Specify the fields to retrieve.
      $selector->fields = array('CustomerId',  'Name');

      // Make the get request.
      $graph = $managedCustomerService->get($selector);

      // Display serviced account graph.
      if (isset($graph->entries)) {
        // Create map from customerId to parent and child links.
        $childLinks = array();
        $parentLinks = array();
        if (isset($graph->links)) {
          foreach ($graph->links as $link) {
            $childLinks[$link->managerCustomerId][] = $link;
            $parentLinks[$link->clientCustomerId][] = $link;
          }
        }
        // Create map from customerID to account, and find root account.
        $accounts = array();
        $rootAccount = NULL;
        foreach ($graph->entries as $account) {
          $accounts[$account->customerId] = $account;
          if (!array_key_exists($account->customerId, $parentLinks)) {
            $rootAccount = $account;
          }
        }
        // The root account may not be returned in the sandbox.
        if (!isset($rootAccount)) {
          $rootAccount = new Account();
          $rootAccount->customerId = 0;
        }
        // Display account tree.
        print "(Customer Id, Account Name)\n";
        DisplayAccountTree($rootAccount, NULL, $accounts, $childLinks, 0);
      } else {
        print "No serviced accounts were found.\n";
      }
    }

    GetAccountHierarchyExample($user);

SetClientCustomerId will be the parent ID of your all accounts, It will be appeared near the Sign Out button of you google AdWords account, Please see the attached image

enter image description here

I hope this answer will be helpful, Please add your comments below if you want any further help

1
votes

If you need just the list of clientCustomerIds, try ServicedAccountService.
Here is a code example that shows how this may be done.

Next time, you might also want to consider asking the question on the official forum for AdWords API: https://groups.google.com/forum/?fromgroups#!forum/adwords-api