Hi Team,
I know that theres new Update in Adwords API Php Library.. I'm in-process of upgrading my old source code to latest version of the v201708
But it looks like the new library was written from ground up, and some of the functions are now similar to the old source code i have..
So im currently stuck with ... (By the way the purpose of my program is to Access the AdWords Account of Customer on there behalf..)
Code:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require("vendor/autoload.php");
use Google\Auth\OAuth2;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201708\cm\CampaignService;
use Google\AdsApi\AdWords\v201708\cm\OrderBy;
use Google\AdsApi\AdWords\v201708\cm\Paging;
use Google\AdsApi\AdWords\v201708\cm\Selector;
use Google\AdsApi\AdWords\v201708\cm\SortOrder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201708\mcm\CustomerService;
use Google\AdsApi\AdWords\v201708\mcm\ManagedCustomerService;
session_start();
$oauth2 = new OAuth2([
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => 'http://dev.xxxxx.com/testing.php',
'clientId' => '826982.....ontent.com',
'clientSecret' => 'dJCcn......S1T8I',
'scope' => 'https://www.googleapis.com/auth/adwords'
]);
if (isset($_GET['sign_in'])) {
// 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 (isset($_GET['code'])) {
$oauth2->setCode($_GET['code']);
$authToken = $oauth2->fetchAuthToken();
TestCall::run($oauth2);
}
elseif (isset($_GET['try_again'])) {
TestCall::run($oauth2);
}
elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state.');
}
/* Test Class */
class TestCall {
public static function run(OAuth2 $oauth2) {
$session = (new AdWordsSessionBuilder())
->fromFile('/var/www/html/dev.xxxxxxx.com/adsapi_php.ini')
->withOAuth2Credential($oauth2)
->withClientCustomerId('932-275-9700')
->build();
$adWordsServices = new AdWordsServices();
$campaignService = $adWordsServices->get($session, CampaignService::class);
// Create selector.
$selector = new Selector();
$selector->setFields(['Id', 'Name']);
$selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]);
$selector->setPaging(new Paging(0, 500));
$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() + 500);
} while ($selector->getPaging()->getStartIndex() < $totalNumEntries);
printf("Number of results found: %d\n", $totalNumEntries);
}
}
Feel free to try this Test Endpoints... (this URL will be deleted soon)
It will redirect correctly on to my redirect_uri (that include the Auth Code, and on background I will process that auth code to request for Access and Refresh token)
In background I will try to get the Client Account Campaigns and list its (this works fine in first load, but of course when you reload it will throw an error that Auth Code already redeemed)
So on my understanding... after you redeemed the Access and Refresh token, that access token will automatically appended on the OAuth, so I will just open this OAuth2 and the library will handle the whole process?
This the test url (after you redeemed the tokens)
But I guess there still missing or wrong on my logic? I hope somebody can enlightened me on this...
Thank you very much