如何获得" clientCustomerId"来自PHP的Adwrods API与OAuth?

时间:2017-03-30 05:08:00

标签: google-adwords

我正在开发Google Adwords应用。我尝试使用PHP中提供的API库。我发现,我需要" clientCustomerId" in" adsapi_php.ini"。我无论如何也看不到这个" clientCustomerId"使用API​​与OAuth。我在做错什么吗?

3 个答案:

答案 0 :(得分:1)

这实际上取决于您的应用程序设计。

我的设置是使用可以访问所有客户帐户的顶级经理帐户。

clientCustomerID设置为该经理ID。

通过它,您可以使用PHP库提供的ManagedCustomerService->get()方法获取客户列表。您可以在此处查看示例代码:

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

该回购现在已折旧。自2018年10月起,您应使用此文档:https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201802/AccountManagement/GetAccountHierarchy.php

答案 1 :(得分:0)

尝试CustomerService

  

CustomerService提供有关您帐户的信息。它有一个   getCustomers()方法,不带参数并返回列表   客户对象包含customerId,currencyCode等字段,   和dateTimeZone。的CustomerService

     

参考:Managing Accounts

答案 2 :(得分:0)

Google Adwords验证码(Using PHP Client Lib

您可以查看Google Adwords PHP sample

中的代码示例

Get Credentials供API使用

    $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