For example:
(just remember to put correct path to autoload.php, and place
"adsapi_php.ini" in same directory as script bellow. With correct API
settings)
accounts_get_all.php
<?php
require '../vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\cm\OrderBy;
use Google\AdsApi\AdWords\v201809\cm\Paging;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\AdWords\v201809\cm\SortOrder;
use Google\AdsApi\AdWords\v201809\mcm\ManagedCustomerService;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example gets the account hierarchy under the current account.
*/
class GetAccountHierarchy {
const PAGE_LIMIT = 1000;
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSession $session) {
$managedCustomerService = $adWordsServices->get ( $session,
ManagedCustomerService::class );
// Create selector.
$selector = new Selector ();
$selector->setFields ( [
'CustomerId',
'Name',
'CanManageClients'
] );
$selector->setOrdering ( [
new OrderBy ( 'CustomerId', SortOrder::ASCENDING )
] );
$selector->setPaging ( new Paging ( 0, self::PAGE_LIMIT ) );
// Maps from customer IDs to accounts and links.
$customerIdsToAccounts = [ ];
$customerIdsToChildLinks = [ ];
$customerIdsToParentLinks = [ ];
$totalNumEntries = 0;
do {
// Make the get request.
$page = $managedCustomerService->get ( $selector );
// Create links between manager and clients.
if ($page->getEntries () !== null) {
$totalNumEntries = $page->getTotalNumEntries ();
if ($page->getLinks () !== null) {
foreach ( $page->getLinks () as $link ) {
$customerIdsToChildLinks [$link->getManagerCustomerId ()] [] = $link;
$customerIdsToParentLinks [$link->getClientCustomerId ()] = $link;
}
}
foreach ( $page->getEntries () as $account ) {
$customerIdsToAccounts [$account->getCustomerId ()] = $account;
}
}
// Advance the paging index.
$selector->getPaging ()->setStartIndex ( $selector->getPaging
()->getStartIndex () + self::PAGE_LIMIT );
} while ( $selector->getPaging ()->getStartIndex () < $totalNumEntries );
// Find the root account.
$rootAccount = null;
foreach ( $customerIdsToAccounts as $account ) {
if (! array_key_exists ( $account->getCustomerId (),
$customerIdsToParentLinks )) {
$rootAccount = $account;
break;
}
}
if ($rootAccount !== null) {
// Display results.
self::printAccountHierarchy ( $rootAccount, $customerIdsToAccounts,
$customerIdsToChildLinks );
} else {
printf ( "No accounts were found.\n" );
}
}
/**
* Prints the specified account's hierarchy using recursion.
*
* @param ManagedCustomer $account
* the account to print
* @param array $customerIdsToAccounts
* a map from customer IDs to accounts
* @param array $customerIdsToChildLinks
* a map from customer IDs to child
* links
* @param int|null $depth
* the current depth we are printing from in the
* account hierarchy; i.e., how far we've recursed
*/
private static function printAccountHierarchy($account,
$customerIdsToAccounts, $customerIdsToChildLinks, $depth = null) {
if ($depth === null) {
// print "(Customer ID, Account Name)\n";
self::printAccountHierarchy ( $account, $customerIdsToAccounts,
$customerIdsToChildLinks, 0 );
return;
}
global $customer_ids;
$customerId = $account->getCustomerId ();
$canManageClients = $account->getCanManageClients ();
if ($canManageClients == false) {
//file_put_contents ( dirname ( __FILE__ ) . '/accounts_list.ini',
$customerId . "\r\n", FILE_APPEND );
$customer_ids[] = $customerId;
}
if (array_key_exists ( $customerId, $customerIdsToChildLinks )) {
foreach ( $customerIdsToChildLinks [$customerId] as $childLink ) {
$childAccount = $customerIdsToAccounts [$childLink->getClientCustomerId ()];
self::printAccountHierarchy ( $childAccount, $customerIdsToAccounts,
$customerIdsToChildLinks, $depth + 1 );
}
}
}
public static function main() {
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder ())->fromFile ()->build ();
// Construct an API session configured from a properties file and the OAuth2
// credentials above.
$session = (new AdWordsSessionBuilder ())->fromFile
()->withOAuth2Credential ( $oAuth2Credential )->build ();
self::runExample ( new AdWordsServices (), $session );
}
}
/*
if (file_exists ( dirname ( __FILE__ ) . '/accounts_list.ini' )) {
unlink ( dirname ( __FILE__ ) . '/accounts_list.ini' );
}
*/
function get_all_accounts() {
global $customer_ids;
GetAccountHierarchy::main ();
if(is_array($customer_ids)) {
return $customer_ids;
} else {
return false;
}
}
print_r(get_all_accounts());
?>
adsapi_php.ini
[ADWORDS]
; Required AdWords API properties. Details can be found at:
;
https://developers.google.com/adwords/api/docs/guides/basic-concepts#soap_and_xml
developerToken = "xxxxxxxxxxxxxxxxxxxxxxxxx"
clientCustomerId = "xxxxxxxxxxxxxxxxxxxx"
; Optional. Set a friendly application name identifier.
userAgent = "xxxxxxxxxxxxxxxxxxxxxxxx"
; Optional additional AdWords API settings.
; endpoint = "https://adwords.google.com/"
; isPartialFailure = false
; Optional setting for utility usage tracking in the user agent in requests.
; Defaults to true.
; includeUtilitiesInUserAgent = true
[ADWORDS_REPORTING]
; Optional reporting settings.
isSkipReportHeader = true
isSkipColumnHeader = false
isSkipReportSummary = true
isUseRawEnumValues = false
[OAUTH2]
; Required OAuth2 credentials. Uncomment and fill in the values for the
; appropriate flow based on your use case. See the README for guidance:
;
https://github.com/googleads/googleads-php-lib/blob/master/README.md#getting-started
; For installed application or web application flow.
clientId =
"237371307973-n0312lu7o6tgvlpcucaqe7akqtrfva1s.apps.googleusercontent.com"
clientSecret = "S1zzON_ZbKTx-lD225xxwxEu"
refreshToken = "1/ASKR1FxESYHy5gAjkbPpBm2sJhAEU6mcKcZ6d0Ng3gc"
; For service account flow.
; jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
; scopes = "https://www.googleapis.com/auth/adwords"
; impersonatedEmail = "INSERT_EMAIL_OF_ACCOUNT_TO_IMPERSONATE_HERE"
[SOAP]
; Optional SOAP settings. See SoapSettingsBuilder.php for more information.
; compressionLevel = <COMPRESSION_LEVEL>
; wsdlCache = <WSDL_CACHE>
[PROXY]
; Optional proxy settings to be used by SOAP requests.
; host = "<HOST>"
; port = <PORT>
; user = "<USER>"
; password = "<PASSWORD>"
[LOGGING]
; Optional logging settings.
; soapLogFilePath = "path/to/your/soap.log"
; soapLogLevel = "INFO"
; reportDownloaderLogFilePath = "path/to/your/report-downloader.log"
; reportDownloaderLogLevel = "INFO"
; batchJobsUtilLogFilePath = "path/to/your/bjutil.log"
; batchJobsUtilLogLevel = "INFO"
W dniu poniedziałek, 26 sierpnia 2019 08:06:47 UTC+2 użytkownik 윤성원 napisał:
>
> Hi
> My developer token has been approved by adwords team few days ago,
> But I can't get all accounts of the users.
> I got the auth associated with this MCC account(867-685-6055).
> And I logged in adcircle0710@gm*il.com who has the manager authorization
> about 867-685-6055, but I got the only one account like bellow.
> There's no 'links:protected' under the username:Adcircle, also only that
> account. I want to get all accounts under 867-685-6055 account.
>
> Google\AdsApi\AdWords\v201809\mcm\ManagedCustomerPage Object
> (
> [entries:protected] => Array
> (
> [0] => Google\AdsApi\AdWords\v201809\mcm\ManagedCustomer Object
> (
> [name:protected] => Adcircle
> [customerId:protected] => 7344067646
> [canManageClients:protected] =>
> [currencyCode:protected] =>
> [dateTimeZone:protected] =>
> [testAccount:protected] =>
> [accountLabels:protected] =>
> [excludeHiddenAccounts:protected] =>
> )
>
> )
>
> [links:protected] =>
> [totalNumEntries:protected] => 1
> [PageType:protected] => ManagedCustomerPage
> [parameterMap:Google\AdsApi\AdWords\v201809\cm\Page:private] => Array
> (
> [Page.Type] => PageType
> )
>
> )
>
> I used PHP AdWords library.
> Why can't I get all accounts even though I have administrative rights? And
> what can I do for get the information?
> Thanks.
>
>
> 867-685-6055)
>
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/cdbb4e68-2407-4ee0-aa58-57b5c661d90a%40googlegroups.com.