Answer the question
In order to leave comments, you need to log in
How to get list of google adwords api companies?
How to get user's RK list using Refresh Token (OAuth2)?
I installed this library - https://github.com/googleads/googleads-php-lib But I did not find any documentation on working using this particular method ... I
use this code:
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);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question