-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
81 changed files
with
9,512 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# DCM/DFA Reporting and Trafficking API PHP Samples | ||
|
||
This is a collection of samples written in PHP which provide a starting place | ||
for your experimentation into the DCM/DFA Reporting and Trafficking API. | ||
|
||
## Prerequisites | ||
|
||
- PHP 5.4+ | ||
- JSON PHP extension | ||
- Composer | ||
|
||
From the example directory, run `composer install` to install all dependencies. | ||
|
||
## Setup Authentication | ||
|
||
This API uses OAuth 2.0. Learn more about Google APIs and OAuth 2.0 here: | ||
https://developers.google.com/accounts/docs/OAuth2 | ||
|
||
Or, if you'd like to dive right in, follow these steps. | ||
- Visit https://console.developers.google.com to register your application. | ||
- From the API Manager -> Overview screen, activate access to "DCM/DFA Reporting and Trafficking API". | ||
- Click on "Credentials" in the left navigation menu | ||
- Click the button labeled "Create credentials" -> "OAuth2 client ID" | ||
- Select "Web Application" as the "Application type" | ||
- Configure javascript origins and redirect URIs | ||
- Authorized Javascript Origins: http://localhost | ||
- Authorized Redirect URIs: http://localhost/path/to/index.php | ||
- Click "Create client ID" | ||
- Click "Download JSON" and save the file as `client_secrets.json` in your | ||
examples directory | ||
|
||
> #### Security alert! | ||
> Always ensure that your client_secrets.json file is not publicly accessible. | ||
> This file contains credential information which could allow unauthorized access | ||
> to your DFA account. | ||
## Running the Examples | ||
|
||
I'm assuming you've checked out the code and are reading this from a local | ||
directory. If not check out the code to a local directory. | ||
|
||
1. Open the sample (`http://your/path/index.php`) in your browser | ||
|
||
2. Complete the authorization steps | ||
|
||
3. Select an example and provide the required information | ||
|
||
3. Examine the response, be inspired and start hacking an amazing new app! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/* | ||
* Copyright 2017 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_once dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
/** | ||
* This example demonstrates how to authenticate and make a basic request using | ||
* a service account. | ||
* | ||
* This example is written to be run as a command line application, not as a | ||
* webpage. An optional Google account email to impersonate may be specified as | ||
* follows: | ||
* | ||
* AuthenticateUsingServiceAccount.php /path/to/client_secrets.json <email> | ||
* | ||
* This optional flag only applies to service accounts which have domain-wide | ||
* delegation enabled and wish to make API requests on behalf of an account | ||
* within that domain. Using this flag will not allow you to impersonate a user | ||
* from a domain that you don't own (e.g., gmail.com). | ||
*/ | ||
class AuthenticateUsingServiceAccount | ||
{ | ||
// The OAuth 2.0 scopes to request. | ||
private static $OAUTH_SCOPES = [ | ||
Google_Service_Dfareporting::DFAREPORTING | ||
]; | ||
|
||
public function run($pathToJsonFile, $email = null) | ||
{ | ||
// Create an authenticated client object. | ||
$client = $this->createAuthenticatedClient($pathToJsonFile, $email); | ||
|
||
// Create a Dfareporting service object. | ||
$service = new Google_Service_Dfareporting($client); | ||
|
||
$this->getUserProfiles($service); | ||
} | ||
|
||
private function createAuthenticatedClient($pathToJsonFile, $email) | ||
{ | ||
// Create a Google_Client instance. | ||
// | ||
// Note: application name should be replaced with a value that identifies | ||
// your application. Suggested format is "MyCompany-ProductName". | ||
$client = new Google_Client(); | ||
$client->setApplicationName('PHP service account sample'); | ||
$client->setScopes(self::$OAUTH_SCOPES); | ||
|
||
// Load the service account credentials. | ||
$client->setAuthConfig($pathToJsonFile); | ||
|
||
// Configure impersonation (if applicable). | ||
if (!is_null($email)) { | ||
$client->setSubject($email); | ||
} | ||
|
||
return $client; | ||
} | ||
|
||
private function getUserProfiles($service) | ||
{ | ||
// Retrieve and print all user profiles for the current authorized user. | ||
$result = $service->userProfiles->listUserProfiles(); | ||
foreach ($result['items'] as $userProfile) { | ||
printf( | ||
"User profile \"%s\" (ID: %d) found for account %d.\n", | ||
$userProfile->getUserName(), | ||
$userProfile->getProfileId(), | ||
$userProfile->getAccountId() | ||
); | ||
} | ||
} | ||
} | ||
|
||
if ($argc < 2 || $argc >= 4) { | ||
printf( | ||
"Usage: %s /path/to/client_secrets.json [email_to_impersonate]\n", | ||
$argv[0] | ||
); | ||
} else { | ||
$sample = new AuthenticateUsingServiceAccount(); | ||
|
||
if ($argc == 2) { | ||
$sample->run($argv[1]); | ||
} else { | ||
$sample->run($argv[1], $argv[2]); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/* | ||
* Copyright 2017 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_once dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
/** | ||
* This example demonstrates how to authenticate and make a basic request using | ||
* a user account, via the OAuth 2.0 installed application fow. | ||
* | ||
* This example is written to be run as a command line application, not as a | ||
* webpage. | ||
*/ | ||
class AuthenticateUsingUserAccount | ||
{ | ||
// This redirect URI allows you to copy the token from the success screen. | ||
const OAUTH_REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'; | ||
|
||
// Location where authorization credentials will be cached. | ||
const TOKEN_STORE = 'auth-sample.dat'; | ||
|
||
// The OAuth 2.0 scopes to request. | ||
private static $OAUTH_SCOPES = [ | ||
Google_Service_Dfareporting::DFAREPORTING | ||
]; | ||
|
||
public function run($pathToJsonFile) | ||
{ | ||
// Create an authenticated client object. | ||
$client = $this->createAuthenticatedClient( | ||
$pathToJsonFile, | ||
self::TOKEN_STORE | ||
); | ||
|
||
// Create a Dfareporting service object. | ||
$service = new Google_Service_Dfareporting($client); | ||
|
||
$this->getUserProfiles($service); | ||
} | ||
|
||
private function createAuthenticatedClient($pathToJsonFile, $tokenStore) | ||
{ | ||
// Create a Google_Client instance. | ||
// | ||
// Note: application name should be replaced with a value that identifies | ||
// your application. Suggested format is "MyCompany-ProductName". | ||
$client = new Google_Client(); | ||
$client->setAccessType('offline'); | ||
$client->setApplicationName('PHP installed app sample'); | ||
$client->setRedirectUri(self::OAUTH_REDIRECT_URI); | ||
$client->setScopes(self::$OAUTH_SCOPES); | ||
|
||
// Load the client secrets file. | ||
$client->setAuthConfig($pathToJsonFile); | ||
|
||
// Try to load cached credentials from the token store. Using a token store | ||
// allows auth credentials to be cached, so they survive multiple runs of | ||
// the application. This avoids prompting the user for authorization every | ||
// time the access token expires, by remembering the refresh token. | ||
if (file_exists($tokenStore) && filesize($tokenStore) > 0) { | ||
$client->setAccessToken(file_get_contents($tokenStore)); | ||
} else { | ||
// If no cached credentials were found, authorize and persist | ||
// credentials to the token store. | ||
print 'Open this URL in your browser and authorize the application.'; | ||
printf("\n\n%s\n\n", $client->createAuthUrl()); | ||
print 'Enter the authorization code: '; | ||
$code = trim(fgets(STDIN)); | ||
$client->authenticate($code); | ||
|
||
file_put_contents($tokenStore, json_encode($client->getAccessToken())); | ||
} | ||
|
||
return $client; | ||
} | ||
|
||
private function getUserProfiles($service) | ||
{ | ||
// Retrieve and print all user profiles for the current authorized user. | ||
$result = $service->userProfiles->listUserProfiles(); | ||
foreach ($result['items'] as $userProfile) { | ||
printf( | ||
"User profile \"%s\" (ID: %d) found for account %d.\n", | ||
$userProfile->getUserName(), | ||
$userProfile->getProfileId(), | ||
$userProfile->getAccountId() | ||
); | ||
} | ||
} | ||
} | ||
|
||
if ($argc !== 2) { | ||
printf("Usage: %s /path/to/client_secrets.json\n", $argv[0]); | ||
} else { | ||
$sample = new AuthenticateUsingUserAccount(); | ||
$sample->run($argv[1]); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"web": { | ||
"client_id": "Enter Client ID", | ||
"client_secret": "Enter Client Secret", | ||
"redirect_uris": ["http://path/to/the/samples"] | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"require": { | ||
"google/apiclient-services": "dev-master", | ||
"google/apiclient": "^2.0", | ||
"guzzlehttp/psr7": "^1.2" | ||
} | ||
} |
Oops, something went wrong.