Skip to content

Instantly share code, notes, and snippets.

@kidker
Last active October 17, 2018 16:04
Show Gist options
  • Save kidker/ed5c4bc5da20614ee09ed0b96cd741c8 to your computer and use it in GitHub Desktop.
Save kidker/ed5c4bc5da20614ee09ed0b96cd741c8 to your computer and use it in GitHub Desktop.
Google AdWords get ClientCustomerId
<?php
namespace App\Libraries;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Reporting\v201710\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201710\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201710\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201710\ReportDownloader;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\v201710\cm\Predicate;
use Google\AdsApi\AdWords\v201710\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201710\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201710\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201710\mcm\CustomerService;
class GoogleService
{
var $refresh_token;
var $debug;
public function __construct($token)
{
$this->refresh_token = $token;
$this->debug = true;
}
private function main($all)
{
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
//->fromFile()
->withClientId('.apps.googleusercontent.com')
->withClientSecret('')
->withRefreshToken($this->refresh_token)
->build();
// See: AdWordsSessionBuilder for setting a client customer ID that is
// different from that specified in your adsapi_php.ini file.
// Construct an API session configured from a properties file and the OAuth2
// credentials above.
$session = (new AdWordsSessionBuilder())
->withDeveloperToken('')
->withOAuth2Credential($oAuth2Credential)
->build();
$adWordsServices = new AdWordsServices();
$customerService = $adWordsServices->get($session, CustomerService::class);
$cliendData = $customerService->getCustomers();
$clientCustomerId = $cliendData[0]->getCustomerId();
$session = (new AdWordsSessionBuilder())
->withDeveloperToken('')
->withClientCustomerId($clientCustomerId)
->withOAuth2Credential($oAuth2Credential)
->build();
$selector = new Selector();
$selector->setFields(['CustomerId', 'Name', 'CanManageClients', 'TestAccount']);
$selector->setOrdering([new OrderBy('CustomerId', SortOrder::ASCENDING)]);
$selector->setPaging(new Paging(0, self::PAGE_LIMIT));
$managedCustomerService = $adWordsServices->get($session, ManagedCustomerService::class);
$customers = $managedCustomerService->get($selector);
var_dump($customers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment