Как, используя Токен обновления (OAuth2) получить список РК пользователя?
Установил сию библиотеку -
https://github.com/googleads/googleads-php-lib Но документации по работе с использованием именно
этого способа не нашел...
Использую такой код:
function Auth() {
session_start();
$oauth2 = new OAuth2([
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => '***************',
'clientId' => '***************',
'clientSecret' => '***************',
'scope' => '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.
'access_type' => 'offline'
];
header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
exit;
}
elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state.');
} else {
$d = $oauth2->setCode($_GET['code']);
$authToken = $oauth2->fetchAuthToken();
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile()
->build();
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$adWordsServices = new AdWordsServices();
$campaignService = $adWordsServices->get($session, CampaignService::class);
define('PAGE_LIMIT',500);
// Create selector.
$selector = new Selector();
$selector->setFields(['Id', 'Name']);
$selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]);
$selector->setPaging(new Paging(0, PAGE_LIMIT));
$totalNumEntries = 0;
do {
// Make the get request.
$page = $campaignService->get($selector);
// Display results.
if ($page->getEntries() !== null) {
$totalNumEntries = $page->getTotalNumEntries();
foreach ($page->getEntries() as $campaign) {
printf(
"Campaign with ID %d and name '%s' was found.\n",
$campaign->getId(),
$campaign->getName()
);
}
}
// Advance the paging index.
$selector->getPaging()->setStartIndex(
$selector->getPaging()->getStartIndex() + PAGE_LIMIT
);
} while ($selector->getPaging()->getStartIndex() < $totalNumEntries);
printf("Number of results found: %d\n", $totalNumEntries);
}
В ответ постоянно "Message: [AuthorizationError.USER_PERMISSION_DENIED @ ; trigger:'']"