Skip to content

Commit fe24b17

Browse files
author
Md. Alimuzzaman Alim
committed
Updated Google SDK #176
Also removed unnecessary google serveces.
1 parent 7758a77 commit fe24b17

File tree

577 files changed

+25626
-290957
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

577 files changed

+25626
-290957
lines changed

lib/Google/.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.github export-ignore
2+
/.gitignore export-ignore
3+
/.travis.yml export-ignore
4+
/CONTRIBUTING.md export-ignore
5+
/examples export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/style export-ignore
8+
/tests export-ignore
9+
/UPGRADING.md export-ignore

lib/Google/README.md

Lines changed: 251 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
# Google APIs Client Library for PHP #
44

5-
> ### NOTE: If you arrived here from `developers.google.com`, you should be using the [v1-branch](https://github.com/google/google-api-php-client/tree/v1-master) of this repo
6-
7-
## Description ##
85
The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server.
96

10-
## Beta ##
11-
This library is in Beta. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it. We will make an effort to support the public and protected surface of the library and maintain backwards compatibility in the future. While we are still in Beta, we reserve the right to make incompatible changes. If we do remove some functionality (typically because better functionality exists or if the feature proved infeasible), our intention is to deprecate and provide ample time for developers to update their code.
7+
These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.
8+
9+
## Google Cloud Platform
10+
11+
For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/GoogleCloudPlatform/google-cloud-php) which is under active development.
1212

1313
## Requirements ##
1414
* [PHP 5.4.0 or higher](http://www.php.net/)
@@ -18,7 +18,7 @@ http://developers.google.com/api-client-library/php
1818

1919
## Installation ##
2020

21-
You can use **Composer** or simply **Download the Release**
21+
You can use **Composer** or simply **Download the Release**
2222

2323
### Composer
2424

@@ -29,7 +29,7 @@ composer installed.
2929
Once composer is installed, execute the following command in your project root to install this library:
3030

3131
```sh
32-
composer require google/apiclient:^2.0.0@RC
32+
composer require google/apiclient:^2.0
3333
```
3434

3535
Finally, be sure to include the autoloader:
@@ -51,8 +51,8 @@ require_once '/path/to/google-api-php-client/vendor/autoload.php';
5151

5252
For additional installation and setup instructions, see [the documentation](https://developers.google.com/api-client-library/php/start/installation).
5353

54-
## Basic Example ##
55-
See the examples/ directory for examples of the key client features. You can
54+
## Examples ##
55+
See the [`examples/`](examples) directory for examples of the key client features. You can
5656
view them in your browser by running the php built-in web server.
5757

5858
```
@@ -62,7 +62,9 @@ $ php -S localhost:8000 -t examples/
6262
And then browsing to the host and port you specified
6363
(in the above example, `http://localhost:8000`).
6464

65-
```PHP
65+
### Basic Example ###
66+
67+
```php
6668
// include your composer dependencies
6769
require_once 'vendor/autoload.php';
6870

@@ -79,21 +81,253 @@ foreach ($results as $item) {
7981
}
8082
```
8183

84+
### Authentication with OAuth ###
85+
86+
> An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php).
87+
88+
1. Follow the instructions to [Create Web Application Credentials](https://developers.google.com/api-client-library/php/auth/web-app#creatingcred)
89+
1. Download the JSON credentials
90+
1. Set the path to these credentials using `Google_Client::setAuthConfig`:
91+
92+
```php
93+
$client = new Google_Client();
94+
$client->setAuthConfig('/path/to/client_credentials.json');
95+
```
96+
97+
1. Set the scopes required for the API you are going to call
98+
99+
```php
100+
$client->addScope(Google_Service_Drive::DRIVE);
101+
```
102+
103+
1. Set your application's redirect URI
104+
105+
```php
106+
// Your redirect URI can be any registered URI, but in this example
107+
// we redirect back to this same page
108+
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
109+
$client->setRedirectUri($redirect_uri);
110+
```
111+
112+
1. In the script handling the redirect URI, exchange the authorization code for an access token:
113+
114+
```php
115+
if (isset($_GET['code'])) {
116+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
117+
}
118+
```
119+
120+
### Authentication with Service Accounts ###
121+
122+
> An example of this can be seen in [`examples/service-account.php`](examples/service-account.php).
123+
124+
Some APIs
125+
(such as the [YouTube Data API](https://developers.google.com/youtube/v3/)) do
126+
not support service accounts. Check with the specific API documentation if API
127+
calls return unexpected 401 or 403 errors.
128+
129+
1. Follow the instructions to [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount)
130+
1. Download the JSON credentials
131+
1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
132+
133+
```php
134+
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
135+
```
136+
137+
1. Tell the Google client to use your service account credentials to authenticate:
138+
139+
```php
140+
$client = new Google_Client();
141+
$client->useApplicationDefaultCredentials();
142+
```
143+
144+
1. Set the scopes required for the API you are going to call
145+
146+
```php
147+
$client->addScope(Google_Service_Drive::DRIVE);
148+
```
149+
150+
1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject:
151+
152+
```php
153+
$client->setSubject($user_to_impersonate);
154+
```
155+
156+
### Making Requests ###
157+
158+
The classes used to call the API in [google-api-php-client-services](https://github.com/Google/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/).
159+
160+
A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this:
161+
162+
```json
163+
POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY
164+
165+
{
166+
"query": {
167+
"kind": [{
168+
"name": "Book"
169+
}],
170+
"order": [{
171+
"property": {
172+
"name": "title"
173+
},
174+
"direction": "descending"
175+
}],
176+
"limit": 10
177+
}
178+
}
179+
```
180+
181+
Using this library, the same call would look something like this:
182+
183+
```php
184+
// create the datastore service class
185+
$datastore = new Google_Service_Datastore($client);
186+
187+
// build the query - this maps directly to the JSON
188+
$query = new Google_Service_Datastore_Query([
189+
'kind' => [
190+
[
191+
'name' => 'Book',
192+
],
193+
],
194+
'order' => [
195+
'property' => [
196+
'name' => 'title',
197+
],
198+
'direction' => 'descending',
199+
],
200+
'limit' => 10,
201+
]);
202+
203+
// build the request and response
204+
$request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]);
205+
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
206+
```
207+
208+
However, as each property of the JSON API has a corresponding generated class, the above code could also be written like this:
209+
210+
```php
211+
// create the datastore service class
212+
$datastore = new Google_Service_Datastore($client);
213+
214+
// build the query
215+
$request = new Google_Service_Datastore_RunQueryRequest();
216+
$query = new Google_Service_Datastore_Query();
217+
// - set the order
218+
$order = new Google_Service_Datastore_PropertyOrder();
219+
$order->setDirection('descending');
220+
$property = new Google_Service_Datastore_PropertyReference();
221+
$property->setName('title');
222+
$order->setProperty($property);
223+
$query->setOrder([$order]);
224+
// - set the kinds
225+
$kind = new Google_Service_Datastore_KindExpression();
226+
$kind->setName('Book');
227+
$query->setKinds([$kind]);
228+
// - set the limit
229+
$query->setLimit(10);
230+
231+
// add the query to the request and make the request
232+
$request->setQuery($query);
233+
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
234+
```
235+
236+
The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs Explorer](https://developers.google.com/apis-explorer/#p/) before using any of the services here.
237+
238+
### Making HTTP Requests Directly ###
239+
240+
If Google Authentication is desired for external applications, or a Google API is not available yet in this library, HTTP requests can be made directly.
241+
242+
The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlephp.org/), so any request made using the client will contain the corresponding authorization.
243+
244+
```php
245+
// create the Google client
246+
$client = new Google_Client();
247+
248+
/**
249+
* Set your method for authentication. Depending on the API, This could be
250+
* directly with an access token, API key, or (recommended) using
251+
* Application Default Credentials.
252+
*/
253+
$client->useApplicationDefaultCredentials();
254+
$client->addScope(Google_Service_Plus::PLUS_ME);
255+
256+
// returns a Guzzle HTTP Client
257+
$httpClient = $client->authorize();
258+
259+
// make an HTTP request
260+
$response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me');
261+
```
262+
263+
### Caching ###
264+
265+
It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](http://www.php-fig.org/psr/psr-6/) compatible library to the client:
266+
267+
```php
268+
use League\Flysystem\Adapter\Local;
269+
use League\Flysystem\Filesystem;
270+
use Cache\Adapter\Filesystem\FilesystemCachePool;
271+
272+
$filesystemAdapter = new Local(__DIR__.'/');
273+
$filesystem = new Filesystem($filesystemAdapter);
274+
275+
$cache = new FilesystemCachePool($filesystem);
276+
$client->setCache($cache);
277+
```
278+
279+
In this example we use [PHP Cache](http://www.php-cache.com/). Add this to your project with composer:
280+
281+
```
282+
composer require cache/filesystem-adapter
283+
```
284+
285+
### Updating Tokens ###
286+
287+
When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#refresh) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client:
288+
289+
```php
290+
$logger = new Monolog\Logger;
291+
$tokenCallback = function ($cacheKey, $accessToken) use ($logger) {
292+
$logger->debug(sprintf('new access token received at cache key %s', $cacheKey));
293+
};
294+
$client->setTokenCallback($tokenCallback);
295+
```
296+
297+
### Debugging Your HTTP Request using Charles ###
298+
299+
It is often very useful to debug your API calls by viewing the raw HTTP request. This library supports the use of [Charles Web Proxy](https://www.charlesproxy.com/documentation/getting-started/). Download and run Charles, and then capture all HTTP traffic through Charles with the following code:
300+
301+
```php
302+
// FOR DEBUGGING ONLY
303+
$httpClient = new GuzzleHttp\Client([
304+
'proxy' => 'localhost:8888', // by default, Charles runs on localhost port 8888
305+
'verify' => false, // otherwise HTTPS requests will fail.
306+
]);
307+
308+
$client = new Google_Client();
309+
$client->setHttpClient($httpClient);
310+
```
311+
312+
Now all calls made by this library will appear in the Charles UI.
313+
314+
One additional step is required in Charles to view SSL requests. Go to **Charles > Proxy > SSL Proxying Settings** and add the domain you'd like captured. In the case of the Google APIs, this is usually `*.googleapis.com`.
315+
82316
### Service Specific Examples ###
83317

84318
YouTube: https://github.com/youtube/api-samples/tree/master/php
85319

86-
## Frequently Asked Questions ##
320+
## How Do I Contribute? ##
87321

88-
### What do I do if something isn't working? ###
322+
Please see the [contributing](CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the [contributor license agreement](https://developers.google.com/api-client-library/php/contribute).
89323

90-
For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: http://stackoverflow.com/questions/tagged/google-api-php-client
324+
## Frequently Asked Questions ##
91325

92-
If there is a specific bug with the library, please file a issue in the Github issues tracker, including a (minimal) example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address.
326+
### What do I do if something isn't working? ###
93327

94-
### How do I contribute? ###
328+
For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: http://stackoverflow.com/questions/tagged/google-api-php-client
95329

96-
We accept contributions via Github Pull Requests, but all contributors need to be covered by the standard Google Contributor License Agreement. You can find links, and more instructions, in the documentation: https://developers.google.com/api-client-library/php/contribute
330+
If there is a specific bug with the library, please [file a issue](https://github.com/google/google-api-php-client/issues) in the Github issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address.
97331

98332
### I want an example of X! ###
99333

@@ -119,7 +353,7 @@ $opt_params = array(
119353

120354
### How do I set a field to null? ###
121355

122-
The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialised properties. To work around this, set the field you want to null to Google_Model::NULL_VALUE. This is a placeholder that will be replaced with a true null when sent over the wire.
356+
The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google_Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire.
123357

124358
## Code Quality ##
125359

lib/Google/composer.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
{
2-
"name": "usabilitydynamics/google-api-php-client",
2+
"name": "google/apiclient",
33
"type": "library",
44
"description": "Client library for Google APIs",
55
"keywords": ["google"],
66
"homepage": "http://developers.google.com/api-client-library/php",
77
"license": "Apache-2.0",
88
"require": {
99
"php": ">=5.4",
10-
"google/auth": "0.7",
11-
"firebase/php-jwt": "~2.0|~3.0",
10+
"google/auth": "^1.0",
11+
"firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0",
1212
"monolog/monolog": "^1.17",
13-
"phpseclib/phpseclib": "2.0.1",
14-
"guzzlehttp/guzzle": "~5.2",
15-
"guzzlehttp/psr7": "1.2.*",
16-
"psr/http-message": "1.0.*"
13+
"phpseclib/phpseclib": "~0.3.10|~2.0",
14+
"guzzlehttp/guzzle": "~5.3.1|~6.0",
15+
"guzzlehttp/psr7": "^1.2"
1716
},
1817
"require-dev": {
19-
"phpunit/phpunit": "~4",
18+
"phpunit/phpunit": "~4.8.36",
2019
"squizlabs/php_codesniffer": "~2.3",
21-
"symfony/dom-crawler": "~2.0",
22-
"symfony/css-selector": "~2.0"
20+
"symfony/dom-crawler": "~2.1",
21+
"symfony/css-selector": "~2.1",
22+
"cache/filesystem-adapter": "^0.3.2"
23+
},
24+
"suggest": {
25+
"cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)"
2326
},
2427
"autoload": {
2528
"psr-0": {

0 commit comments

Comments
 (0)