How to get client customer id with oauth2?

1,061 views
Skip to first unread message

ankitt...@gmail.com

unread,
Aug 31, 2018, 5:16:46 AM8/31/18
to AdWords API and Google Ads API Forum
Hello Sir,

I have integrate oauth2 but If anyone user allow oauth service which is not link on my adwords account then I get USER_PERMISSION_DENIED error.

If user is link on my adwords account then I get customer id, but only that users which is not link in my adword account.


we want to create a tool to link all customer account with a manager account which is allow the oauth then get all campaigns/accounts


Please review my code on below

Thanks



<?php
require_once '../vendor2/autoload.php';

use Google\Auth\OAuth2;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords;
use Google\AdsApi\AdWords\v201806\cm\CampaignService;
use Google\AdsApi\AdWords\v201806\mcm\CustomerService;
session_start();

$oauth2 = new OAuth2([
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'redirectUri' => 'http://localhost/adword/test.php',
    'clientId' => 'client_id_here',
    'clientSecret' => 'client_secret_here',
    'scope' => 'https://www.googleapis.com/auth/adwords',
    'access_type' => 'offline',
    'prompt' => 'consent',
]);
 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['access_token'];
    }


    $session = (new AdWordsSessionBuilder())
        ->fromFile('adsapi_php.ini')
        ->withOAuth2Credential($oauth2)
        ->build();
   
   
    $adWordsServices = new AdWordsServices();

   
    $customerService = $adWordsServices->get($session, CustomerService::class);
    $customers = $customerService->getCustomers();
    $customerId = $customers[0]->getCustomerId();

    echo $customerId;

Teja Makani

unread,
Aug 31, 2018, 2:54:22 PM8/31/18
to AdWords API and Google Ads API Forum
Hello Ankit,

The USER_PERMISSION_DENIED usually occurs when your OAuth credentials are for a user who does not have access to the client customer id specified or any of its manager accounts. If you are trying to link client accounts to manager account, you could do that using ManagedCustomerService.mutateLink to invite a client account and client can either approve or decline the invitation as well. You could refer the Linking accounts section of this guide to know more. 

Your code it is to fetch the customers and then print customer's Id. You might be facing the error for the same reason explained above. Make sure the user making the call (owning OAuth credentials) has access to the account you are making calls to. In case you are trying to get the account under manager account, you could do that by using the sample code GetAccountHierarchy.php.

Let me know if you have any further questions.

Regards,
Sai Teja, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages