Gumption Technologies
Gumption Technologies

Reputation: 49

How to get "clientCustomerId" from Adwrods API in PHP with OAuth?

I am developing a Google Adwords app. I tried using the API libraries available in PHP. I found, I need "clientCustomerId" in "adsapi_php.ini". I don't see anyway to get this "clientCustomerId" using API with OAuth. Am I trying something wrong?

Upvotes: 1

Views: 2624

Answers (3)

hellojebus
hellojebus

Reputation: 3583

This really depends on your application design.

My setup is using a top-level manager account that has access to all the client accounts.

clientCustomerID is set to that manager id.

With that, you can grab a list of your clients using the ManagedCustomerService->get() method provided by the PHP library. You can look at the example code here:

https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201710/AccountManagement/GetAccountHierarchy.php

That repo is now depreciated. As of Oct 2018 you should use this documentation: https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201802/AccountManagement/GetAccountHierarchy.php

Upvotes: 1

Aman Kumar
Aman Kumar

Reputation: 4547

Google Adwords Authencation code ( Using PHP Client Lib )

You may check code samples from Google Adwords PHP sample

Get Credentials for API use

    $oauth2 = new OAuth2([
        'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
        'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
        'redirectUri' => 'http://localhost/adwordsWork/index.php',
        'clientId' => '1139632-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
        'clientSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'scope' => 'https://www.googleapis.com/auth/adwords'
    ]);


    if (!isset($_GET['code'])) {

      $oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
      $_SESSION['oauth2state'] = $oauth2->getState();

      $config = [
        'access_type' => 'offline'
      ];
      header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
      exit;
    }

    elseif (empty($_GET['state'])
        || ($_GET['state'] !== $_SESSION['oauth2state'])) {
      unset($_SESSION['oauth2state']);
      exit('Invalid state.');
    } else {
      $oauth2->setCode($_GET['code']);
      $authToken = $oauth2->fetchAuthToken();

      $refresh_token = $authToken['refresh_token'];
    }


    /* --------------------------- Authencation section End --------------------------------*/

    // echo "<pre>";
    // print_r($authToken);
    // die;

    /*---------------------------- Session Builder -----------------------------------------*/

    $session = (new AdWordsSessionBuilder())
        ->fromFile('adsapi_php.ini')
        ->withOAuth2Credential($oauth2)
        ->build();
    /* ------------------------- Session build ---------------------------------------------*/


    /* -------------------------------- Adwords Services section ----------------------------*/

    /* Creating object of Adwords services */
    $adWordsServices = new AdWordsServices();

    /* Adwords Customer services */
    $customerService = $adWordsServices->get($session, CustomerService::class);
    $customers = $customerService->getCustomers();
    $customerId = $customers[0]->getCustomerId();  // Getting main customer client id 

    echo $customerId; // customer id from adwords account 

Upvotes: 0

granch
granch

Reputation: 225

Try CustomerService

CustomerService provides information about your accounts. It has a getCustomers() method that takes no arguments and returns a list of Customer objects containing fields such as customerId, currencyCode, and dateTimeZone. CustomerService

Reference: Managing Accounts

Upvotes: 0

Related Questions