Created
November 4, 2016 08:38
-
-
Save lomholdt/d86746cd00dfedd7425b04a3fd3b763c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Use composers autoload class to load library files | |
*/ | |
require_once '../vendor/autoload.php'; | |
/** | |
* AdExchangeSeller Api class | |
* | |
* - Create OAuth Client ID credentials from https://console.cloud.google.com/apis/credentials | |
* - Download the client_secrets.json file and us it in the class below when calling initClient. | |
* - Go to https://developers.google.com/oauthplayground/ and pick _all_ the API's you wan't to | |
* be able to access. | |
* - Click the settings button on the upper right hand side and select *Use your own OAuth credentials*. | |
* Add the credentials from before (found at https://console.cloud.google.com/apis/credentials) and | |
* click Exchange authorization code for tokens. | |
* - The given refresh token will never expire, so it can be used until a new one is generated. | |
*/ | |
class AdExchange | |
{ | |
private $client; | |
private $configFile; | |
private $authConfigFile; | |
private $service; | |
private $accountId; | |
private $refreshToken; | |
private $scope; | |
private function getClient() | |
{ | |
return $this->client; | |
} | |
private function setClient($client) | |
{ | |
$this->client = $client; | |
} | |
public function initClient($accountId, $refreshToken, $authConfigPath = 'client_secrets.json') | |
{ | |
$this->setAccountId($accountId); | |
$this->setRefreshToken($refreshToken); | |
$this->setAuthConfigFile($authConfigPath); | |
//$this->setScope(Google_Service_AdExchangeSeller::ADEXCHANGE_SELLER_READONLY); | |
$this->initClientHelper(); | |
return true; | |
} | |
private function initClientHelper() | |
{ | |
$this->setClient(new Google_Client()); | |
$this->client->setAuthConfig($this->getAuthConfigFile()); // client_secrets.json | |
$this->client->addScope(Google_Service_AdExchangeSeller::ADEXCHANGE_SELLER_READONLY); | |
// $this->client->setAccessType('offline'); | |
$this->client->fetchAccessTokenWithRefreshToken($this->getRefreshToken()); | |
$this->setService(new Google_Service_AdExchangeSeller($this->getClient())); | |
return true; | |
} | |
private function getScope() | |
{ | |
if(isset($this->scope)) | |
{ | |
return $this->scope; | |
} | |
throw new Exception('Scope is not set.'); | |
} | |
private function setScope($scope) | |
{ | |
$this->scope = $scope; | |
} | |
private function getAuthConfigFile() | |
{ | |
if (isset($this->authConfigFile) && is_file($this->authConfigFile)) | |
{ | |
return $this->authConfigFile; | |
} | |
throw new Exception('Auth config file is not set or is not a file.'); | |
} | |
private function setAuthConfigFile($authConfigFile) | |
{ | |
$this->authConfigFile = $authConfigFile; | |
} | |
private function getService() | |
{ | |
if(isset($this->service)) | |
{ | |
return $this->service; | |
} | |
throw new Exception('Service is not set'); | |
} | |
private function setService($service) | |
{ | |
$this->service = $service; | |
} | |
private function getAccountId() | |
{ | |
if(isset($this->accountId)) | |
{ | |
return $this->accountId; | |
} | |
throw new Exception('Account id is not set'); | |
} | |
private function setAccountId($accountId) | |
{ | |
$this->accountId = $accountId; | |
} | |
private function getRefreshToken() | |
{ | |
if(isset($this->refreshToken)) | |
{ | |
return $this->refreshToken; | |
} | |
throw new Exception('Access Token is not set'); | |
} | |
private function setRefreshToken($refreshToken) | |
{ | |
$this->refreshToken = $refreshToken; | |
} | |
public function getReportData($startDate, $endDate, $dimensions, $metrics) | |
{ | |
try | |
{ | |
$optParams = [ | |
'metric' => $metrics, | |
'dimension' => $dimensions, | |
'sort' => '+DATE', | |
'alt' => 'media' | |
]; | |
$report = $this->getService()->accounts_reports->generate($this->getAccountId(), $startDate, $endDate, $optParams); | |
if (isset($report)) // && isset($report['rows'])) | |
{ | |
return $report; | |
} | |
return $report; | |
} | |
catch(Exception $e) | |
{ | |
throw new Exception('Could not retrieve report data. ' . $report . $e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment