Get descriptive_name Customer

332 views
Skip to first unread message

Непомнющий Александр

unread,
Apr 8, 2019, 2:53:52 PM4/8/19
to AdWords API and Google Ads API Forum
Введите код...

Hi,

when we create a Customer, we specify a 'descriptive_name'. How to get this field, knowing Client Id?

        // Iterates over all accessible customers' resource names and prints them.
        foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
            /** @var string $resourceName */
          //  printf("Customer resource name: '%s'%s", $resourceName, PHP_EOL);
$customerId = explode("/", $resourceName);
$customerId = $customerId[1];

        }


googleadsapi...@google.com

unread,
Apr 8, 2019, 4:48:37 PM4/8/19
to AdWords API and Google Ads API Forum
Hello,

When you know the client customer Id of that created account, you could use the customer resource to pull the customer.descriptive_name against that customer. You have to make another API call for each customer in the accessibleCustomers array. Please let me know if you have any further questions.

Regards,
Sai Teja, Google Ads API Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Was your question answered? Please rate your experience with us by taking a short survey.
If not -- reply to this email and tell us what else we can do to help.

Take Survey

Also find us on our blog and discussion group:
http://googleadsdeveloper.blogspot.com/search/label/adwords_api
https://developers.google.com/adwords/api/community/

Непомнющий Александр

unread,
Apr 9, 2019, 9:27:18 AM4/9/19
to AdWords API and Google Ads API Forum
Hello,
Thank you for answer.

namespace Google\AdsApi\Examples\Authentication;

require __DIR__ . '/vendor/autoload.php';

use GetOpt\GetOpt;
use Google\Auth\CredentialsLoader;
use Google\Auth\OAuth2;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Server;
use UnexpectedValueException;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\V1\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V1\Services\GoogleAdsRow;
use Google\ApiCore\ApiException;
use Google\Ads\GoogleAds\V1\Resources\Customer;
use Google\Ads\GoogleAds\Util\V1\ResourceNames;


session_start();

define("PAGE_SIZE", 100);


$oauth2 = new OAuth2([
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'redirectUri' => 'https://xxxxxxxxxxxxxx', 
    'clientSecret' => 'xxxxxxxxxxxxxxxxxxxxxx',
    'scope' => array('https://www.googleapis.com/auth/adwords',
]);


if (!isset($_GET['code'])) {
  // Create a 'state' token to prevent request forgery.
  // Store it in the session for later validation.
  $oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
  $_SESSION['oauth2state'] = $oauth2->getState();

  // Redirect the user to the authorization URL.
  $config = [
    // Set to 'offline' if you require offline access.
'prompt' => 'consent',
    'access_type' => 'offline'
  ];
  header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
  exit;
}elseif (empty($_GET['state'])
    || ($_GET['state'] !== $_SESSION['oauth2state'])) {  // Check given state against previously stored one to mitigate CSRF attack.
  unset($_SESSION['oauth2state']);
  exit('Invalid state.');
} else {
  $oauth2->setCode($_GET['code']);
  $authToken = $oauth2->fetchAuthToken();
  
  $refreshToken = $authToken['refresh_token'];
  
  //print_r($refreshToken);
  
  $_SESSION["refresh_token"] = $refreshToken;
 
 
         $googleAdsClient = (new GoogleAdsClientBuilder())->fromFile()
            ->withOAuth2Credential($oauth2)
            ->build();
         $customerServiceClient = $googleAdsClient->getCustomerServiceClient();

        // Issues a request for listing all accessible customers.
        $accessibleCustomers = $customerServiceClient->listAccessibleCustomers();
        print 'Total results: ' . count($accessibleCustomers) . PHP_EOL;
        // Iterates over all accessible customers' resource names and prints them.
        foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
            /** @var string $resourceName */
          //  printf("Customer resource name: '%s'%s", $resourceName, PHP_EOL);
$customerId = explode("/", $resourceName);
$customerId = $customerId[1];

        }



/*  This returns an empty result. */
$customer = $customerServiceClient->getCustomer(ResourceNames::forCustomer('xxxxxxxxx'));

        // Print information about the account.
        printf(
            "Customer with ID %d, descriptive name '%s', currency code '%s', timezone '%s', "
            . "tracking URL template '%s' and auto tagging enabled '%s' was retrieved.%s",
            $customer->getId()->getValue(),
            $customer->getDescriptiveName()->getValue(),
            $customer->getCurrencyCode()->getValue(),
            $customer->getTimeZone()->getValue(),
            $customer->getTrackingUrlTemplate()->getValue(),
            $customer->getAutoTaggingEnabled()->getValue() ? 'true' : 'false',
            PHP_EOL
        );




понедельник, 8 апреля 2019 г., 20:53:52 UTC+2 пользователь Непомнющий Александр написал:

googleadsapi...@google.com

unread,
Apr 9, 2019, 1:27:54 PM4/9/19
to AdWords API and Google Ads API Forum
Hello,

Could you please share the email address of the user authenticating the API call? Also, could you please share the customerId you are passing in the forcustomer function to fetch the customer data? Please use reply privately to the user option while sharing the information requested.


Regards,
Sai Teja, Google Ads API Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Reply all
Reply to author
Forward
0 new messages