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
<?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;