CustomerService getCustomers

235 views
Skip to first unread message

Minas Gratas

unread,
Jan 16, 2017, 8:05:22 AM1/16/17
to AdWords API Forum

Hi there,

After having loads of suggestions from you guys I solved my problems. However there is one thing left.
Before I begin I will add my code here so anyone can see and get an idea.

Please note; I am trying to avoid using .ini file and create this as user authenticates with the application. I am getting my RefreshToken then I am trying to get the ClientCustomerId to be able to fetch the campaigns.

require 'vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201609\cm\CampaignService;
use Google\AdsApi\AdWords\v201609\cm\OrderBy;
use Google\AdsApi\AdWords\v201609\cm\Paging;
use Google\AdsApi\AdWords\v201609\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201609\mcm\CustomerService;
session_start();
$clientId = "*****";
$clientSecret = "****";
if(isset($_SESSION['refreshToken'])){
    $refreshToken =  $_SESSION['refreshToken'];
}else{
    $refreshToken = NULL;
}


$oAuth2Credential = (new OAuth2TokenBuilder())
    ->withClientId($clientId)
    ->withClientSecret($clientSecret)
    ->withRefreshToken($refreshToken)
    ->build();

The trick was removing ->fromFile() and adding withClientId, withClientSecret and withRefreshToken.

All good now!  The last missing puzzle is how to optain ClientCustomerId. 

$session = (new AdWordsSessionBuilder())    
    ->withClientCustomerId($clientCustomerId)
    ->withOAuth2Credential($oAuth2Credential)
    ->build();

I tried to get it like this;
$customerService = new CustomerService();
$cliendData = $customerService->getCustomers();

However I am facing with the following error.
Fatal error: Call to a member function generateHttpHeaders() on null in /vendor/googleads/googleads-php-lib/src/Google/AdsApi/Common/AdsSoapClient.php on line 86

Can you put me in a right direction? Because as far as I understand from the docs here getCustomers does not require any parameters.

Minas Gratas

unread,
Jan 17, 2017, 4:41:49 AM1/17/17
to AdWords API Forum
So no one can help to get clientCustomerId with the updated library?
If I use the code below I get no error but no clientCustomerId as well. 
$managedCustomerService = $adWordsServices->get(
        $session, ManagedCustomerService::class);

cv

unread,
Jan 17, 2017, 7:51:26 AM1/17/17
to AdWords API Forum
Hello,

I think you need to follow this steps

1) You need to prepare Adwords Session without    ->withClientCustomerId($clientCustomerId)
2) You need to use customer service to get all customers
3) Check for canManageClients field, if that is yes you need to call manage customer service (You can use example GetAccountHierarchy ) 
and if not you can see customer details within the object return
here you can find the clientCustomerId as 
$cliendData = $customerService->getCustomers();
$clientCustomerId = $cliendData[0]->customerId;

so Now you can prepare adwords session again to pull the campaign details, 

$session = (new AdWordsSessionBuilder())   
    ->withClientCustomerId($clientCustomerId)
    ->withOAuth2Credential($oAuth2Credential)
    ->build();

Hope this helps.

Thanks

Dmitrii Matrosov

unread,
Jan 12, 2018, 4:13:04 AM1/12/18
to AdWords API Forum

Dmitrii Matrosov

unread,
Jan 12, 2018, 4:13:04 AM1/12/18
to AdWords API Forum
Reply all
Reply to author
Forward
0 new messages