Skip to content

Commit

Permalink
Updated to latest stable version of Google API Client Library (0.6.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
azappella committed Mar 17, 2013
1 parent 18eb6c9 commit 83a2049
Show file tree
Hide file tree
Showing 38 changed files with 14,033 additions and 4,666 deletions.
133 changes: 133 additions & 0 deletions google-api-php-client/examples/adsensehost/AdSenseHostAuth.php
@@ -0,0 +1,133 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Error if PDO and PDO_SQLITE not present
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
throw new Exception('The sample code needs PDO and PDO_SQLITE PHP extension');
}

/**
* Include the library files for the api client and AdSense service class.
*/
require_once "../../src/Google_Client.php";
require_once "../../src/contrib/Google_AdsensehostService.php";

/**
* Handles authentication and OAuth token storing.
* Assumes the presence of a sqlite database called './examples.sqlite'
* containing a table called 'auth' composed of two VARCHAR(255) fields called
* 'user' and 'token'.
*
* @author Sérgio Gomes <sgomes@google.com>
* @author Silvano Luciani <silvano.luciani@gmail.com>
*/

class AdSenseHostAuth {
protected $apiClient;
protected $adSenseHostService;
private $user;

/**
* Create the dependencies.
* (Inject them in a real world app!!)
*/
public function __construct() {
// Create the Google_Client instance.
// You can set your credentials in the config.php file, included under the
// src/ folder in your client library install.
$this->apiClient = new Google_Client();
// Create the api AdsensehostService instance.
$this->adSenseHostService = new Google_AdsensehostService($this->apiClient);
}

/**
* Check if a token for the user is already in the db, otherwise perform
* authentication.
* @param string $user The user to authenticate
*/
public function authenticate($user) {
$this->user = $user;
$dbh = new PDO('sqlite:examples.sqlite');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare('CREATE TABLE IF NOT EXISTS auth ' .
'(user VARCHAR(255), token VARCHAR(255))');
$stmt->execute();
$token = $this->getToken($dbh);
if (isset($token)) {
// I already have the token.
$this->apiClient->setAccessToken($token);
} else {
// Override the scope to use the readonly one
$this->apiClient->setScopes(
array("https://www.googleapis.com/auth/adsensehost"));
// Go get the token
$this->apiClient->setAccessToken($this->apiClient->authenticate());
$this->saveToken($dbh, false, $this->apiClient->getAccessToken());
}
$dbh = null;
}

/**
* Return the AdsenseService instance (to be used to retrieve data).
* @return apiAdsenseService the authenticated apiAdsenseService instance
*/
public function getAdSenseHostService() {
return $this->adSenseHostService;
}

/**
* During the request, the access code might have been changed for another.
* This function updates the token in the db.
*/
public function refreshToken() {
if ($this->apiClient->getAccessToken() != null) {
$dbh = new PDO('sqlite:examples.sqlite');
$this->saveToken($dbh, true, $this->apiClient->getAccessToken());
}
}

/**
* Insert/update the auth token for the user.
* @param PDO $dbh a PDO object for the local authentication db
* @param bool $userExists true if the user already exists in the db
* @param string $token the auth token to be saved
*/
private function saveToken($dbh, $userExists, $token) {
if ($userExists) {
$stmt = $dbh->prepare('UPDATE auth SET token=:token WHERE user=:user');
} else {
$stmt = $dbh
->prepare('INSERT INTO auth (user, token) VALUES (:user, :token)');
}
$stmt->bindParam(':user', $this->user);
$stmt->bindParam(':token', $this->apiClient->getAccessToken());
$stmt->execute();
}

/**
* Retrieves token for use.
* @param PDO $dbh a PDO object for the local authentication db
* @return string a JSON object representing the token
*/
private function getToken($dbh) {
$stmt = $dbh->prepare('SELECT token FROM auth WHERE user= ?');
$stmt->execute(array($this->user));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['token'];
}
}

23 changes: 16 additions & 7 deletions google-api-php-client/examples/adsensehost/BaseExample.php
Expand Up @@ -16,16 +16,16 @@
*/

/**
* Include the libraries file for the AdSense service class and the HTML
* generation functions.
* Include the AdSenseHost service class and the HTML generation functions.
*/
require_once "../../src/contrib/apiAdsensehostService.php";
require_once "../../src/contrib/Google_AdsensehostService.php";
require_once "htmlHelper.php";

/**
* Uses an instance of apiAdsenseService to retrieve the data and renders
* Uses an instance of apiAdsensehostService to retrieve the data and renders
* the screens.
*
* @author Sérgio Gomes <sgomes@google.com>
* @author Silvano Luciani <silvano.luciani@gmail.com>
*/
abstract class BaseExample {
Expand All @@ -34,10 +34,10 @@ abstract class BaseExample {

/**
* Inject the dependency.
* @param apiAdsensehostService $adSenseHostService an authenticated instance
* of apiAdsensehostService
* @param Google_AdsensehostService $adSenseHostService an authenticated
* instance of Google_AdsensehostService
*/
public function __construct(apiAdsensehostService $adSenseHostService) {
public function __construct(Google_AdsensehostService $adSenseHostService) {
$this->adSenseHostService = $adSenseHostService;
}

Expand All @@ -59,6 +59,15 @@ protected function getSixMonthsBeforeNow() {
return $sixMonthsAgo->format($this->dateFormat);
}

/**
* Returns a unique value to append to various properties in the samples.
* @return string unique value
*/
protected function getUniqueName() {
$now = new DateTime();
return $now->format('YmdHisu');
}

/**
* Implemented in the specific example class.
*/
Expand Down
@@ -0,0 +1,73 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Require the base class.
require_once __DIR__ . "/../BaseExample.php";

/**
* This example adds a new ad unit to a publisher ad client.
*
* To get ad clients, see GetAllAdClientsForPublisher.php.
*
* Tags: accounts.adunits.insert
*
* @author Sérgio Gomes <sgomes@google.com>
*/
class AddAdUnitToPublisher extends BaseExample {
public function render() {
$accountId = PUBLISHER_ACCOUNT_ID;
$adClientId = PUBLISHER_AD_CLIENT_ID;

$adUnit = new Google_AdUnit();
$adUnit->setName(sprintf('Ad Unit #%s', $this->getUniqueName()));

$contentAdsSettings = new Google_AdUnitContentAdsSettings();
$backupOption = new Google_AdUnitContentAdsSettingsBackupOption();
$backupOption->setType('COLOR');
$backupOption->setColor('ffffff');
$contentAdsSettings->setBackupOption($backupOption);
$contentAdsSettings->setSize('SIZE_200_200');
$contentAdsSettings->setType('TEXT');
$adUnit->setContentAdsSettings($contentAdsSettings);

$customStyle = new Google_AdStyle();
$colors = new Google_AdStyleColors();
$colors->setBackground('ffffff');
$colors->setBorder('000000');
$colors->setText('000000');
$colors->setTitle('000000');
$colors->setUrl('0000ff');
$customStyle->setColors($colors);
$customStyle->setCorners('SQUARE');
$font = new AdStyleFont();
$font->setFamily('ACCOUNT_DEFAULT_FAMILY');
$font->setSize('ACCOUNT_DEFAULT_SIZE');
$customStyle->setFont($font);
$adUnit->setCustomStyle($customStyle);

// Retrieve custom channels list, and display it.
$result = $this->adSenseHostService->accounts_adunits
->insert($accountId, $adClientId, $adUnit);
$mainFormat =
'Ad unit w/ ID "%s", type "%s", name "%s" and status "%s" was created.';
$content = sprintf($mainFormat, $result['id'],
$result['contentAdsSettings']['type'], $result['name'],
$result['status']);
print $content;
}
}

@@ -0,0 +1,47 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Require the base class.
require_once __DIR__ . "/../BaseExample.php";

/**
* This example adds a custom channel to a host ad client.
*
* To get ad clients, see GetAllAdClientsForHost.php.
* Tags: customchannels.insert
*
* @author Sérgio Gomes <sgomes@google.com>
*/
class AddCustomChannelToHost extends BaseExample {
public function render() {
$adClientId = HOST_AD_CLIENT_ID;

$customChannel = new Google_CustomChannel();
$customChannel->setName(sprintf('Sample Channel #%s',
$this->getUniqueName()));

// Retrieve custom channels list, and display it.
$result = $this->adSenseHostService->customchannels
->insert($adClientId, $customChannel);
$mainFormat =
'Custom channel with ID "%s", code "%s" and name "%s" was created.';
$content = sprintf($mainFormat, $result['id'], $result['code'],
$result['name']);
print $content;
}
}

@@ -0,0 +1,45 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Require the base class.
require_once __DIR__ . "/../BaseExample.php";

/**
* This example adds a URL channel to a host ad client.
*
* To get ad clients, see GetAllAdClientsForHost.php.
* Tags: urlchannels.insert
*
* @author Sérgio Gomes <sgomes@google.com>
*/
class AddUrlChannelToHost extends BaseExample {
public function render() {
$adClientId = HOST_AD_CLIENT_ID;

$urlChannel = new Google_UrlChannel();
$urlChannel->setUrlPattern(sprintf('www.example.com/%s',
$this->getUniqueName()));

// Retrieve URL channels list, and display it.
$result = $this->adSenseHostService->urlchannels
->insert($adClientId, $urlChannel);
$mainFormat = 'URL channel with id "%s" and URL pattern "%s" was created.';
$content = sprintf($mainFormat, $result['id'], $result['urlPattern']);
print $content;
}
}

0 comments on commit 83a2049

Please sign in to comment.