| 1 |
# Google Auth Library for PHP |
| 2 |
|
| 3 |
<dl> |
| 4 |
<dt>Homepage</dt><dd><a href="http://www.github.com/google/google-auth-library-php">http://www.github.com/google/google-auth-library-php</a></dd> |
| 5 |
<dt>Authors</dt> |
| 6 |
<dd><a href="mailto:temiola@google.com">Tim Emiola</a></dd> |
| 7 |
<dd><a href="mailto:stanleycheung@google.com">Stanley Cheung</a></dd> |
| 8 |
<dd><a href="mailto:betterbrent@google.com">Brent Shaffer</a></dd> |
| 9 |
<dt>Copyright</dt><dd>Copyright © 2015 Google, Inc.</dd> |
| 10 |
<dt>License</dt><dd>Apache 2.0</dd> |
| 11 |
</dl> |
| 12 |
|
| 13 |
## Description |
| 14 |
|
| 15 |
This is Google's officially supported PHP client library for using OAuth 2.0 |
| 16 |
authorization and authentication with Google APIs. |
| 17 |
|
| 18 |
### Installing via Composer |
| 19 |
|
| 20 |
The recommended way to install the google auth library is through |
| 21 |
[](http://getcomposer.orgComposer](http://getcomposer.org](http://getcomposer.org). |
| 22 |
|
| 23 |
```bash |
| 24 |
# Install Composer |
| 25 |
curl -sS https://getcomposer.org/installer | php |
| 26 |
``` |
| 27 |
|
| 28 |
Next, run the Composer command to install the latest stable version: |
| 29 |
|
| 30 |
```bash |
| 31 |
composer.phar require google/auth |
| 32 |
``` |
| 33 |
|
| 34 |
## Application Default Credentials |
| 35 |
|
| 36 |
This library provides an implementation of |
| 37 |
[application default credentials][application default credentials] for PHP. |
| 38 |
|
| 39 |
The Application Default Credentials provide a simple way to get authorization |
| 40 |
credentials for use in calling Google APIs. |
| 41 |
|
| 42 |
They are best suited for cases when the call needs to have the same identity |
| 43 |
and authorization level for the application independent of the user. This is |
| 44 |
the recommended approach to authorize calls to Cloud APIs, particularly when |
| 45 |
you're building an application that uses Google Compute Engine. |
| 46 |
|
| 47 |
#### Download your Service Account Credentials JSON file |
| 48 |
|
| 49 |
To use `Application Default Credentials`, You first need to download a set of |
| 50 |
JSON credentials for your project. Go to **APIs & Auth** > **Credentials** in |
| 51 |
the [Google Developers Console](developer console) and select |
| 52 |
**Service account** from the **Add credentials** dropdown. |
| 53 |
|
| 54 |
> This file is your *only copy* of these credentials. It should never be |
| 55 |
> committed with your source code, and should be stored securely. |
| 56 |
|
| 57 |
Once downloaded, store the path to this file in the |
| 58 |
`GOOGLE_APPLICATION_CREDENTIALS` environment variable. |
| 59 |
|
| 60 |
```php |
| 61 |
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); |
| 62 |
``` |
| 63 |
|
| 64 |
> PHP's `putenv` function is just one way to set an environment variable. |
| 65 |
> Consider using `.htaccess` or apache configuration files as well. |
| 66 |
|
| 67 |
#### Enable the API you want to use |
| 68 |
|
| 69 |
Before making your API call, you must be sure the API you're calling has been |
| 70 |
enabled. Go to **APIs & Auth** > **APIs** in the |
| 71 |
[Google Developers Console](developer console) and enable the APIs you'd like to |
| 72 |
call. For the example below, you must enable the `Drive API`. |
| 73 |
|
| 74 |
#### Call the APIs |
| 75 |
|
| 76 |
As long as you update the environment variable below to point to *your* JSON |
| 77 |
credentials file, the following code should output a list of your Drive files. |
| 78 |
|
| 79 |
```php |
| 80 |
use Google\Auth\ApplicationDefaultCredentials; |
| 81 |
use GuzzleHttp\Client; |
| 82 |
use GuzzleHttp\HandlerStack; |
| 83 |
|
| 84 |
// specify the path to your application credentials |
| 85 |
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); |
| 86 |
|
| 87 |
// define the scopes for your API call |
| 88 |
$scopes = ['https://www.googleapis.com/auth/drive.readonly']; |
| 89 |
|
| 90 |
// create middleware |
| 91 |
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes); |
| 92 |
$stack = HandlerStack::create(); |
| 93 |
$stack->push($middleware); |
| 94 |
|
| 95 |
// create the HTTP client |
| 96 |
$client = new Client([ |
| 97 |
'handler' => $stack |
| 98 |
'base_url' => 'https://www.googleapis.com', |
| 99 |
'auth' => 'google_auth' // authorize all requests |
| 100 |
]); |
| 101 |
|
| 102 |
// make the request |
| 103 |
$response = $client->get('drive/v2/files'); |
| 104 |
|
| 105 |
// show the result! |
| 106 |
print_r((string) $response->getBody()); |
| 107 |
``` |
| 108 |
|
| 109 |
## What about auth in google-apis-php-client? |
| 110 |
|
| 111 |
The goal is for auth done by |
| 112 |
[google-apis-php-client][google-apis-php-client] to be be performed |
| 113 |
by this library. |
| 114 |
|
| 115 |
Eventually, google-apis-php-client should have a dependency on this library. |
| 116 |
At the moment, there is no ETA for this, a key prequisite being for google-apis-php-client |
| 117 |
itself take a dependency on [Guzzle][Guzzle] so that it can use the Guzzle |
| 118 |
subscribers that this package provides. That's currently [](http://github.com/google/google-api-php-client#473being discussed](http://github.com/google/google-api-php-client#473](http://github.com/google/google-api-php-client#473). |
| 119 |
This package's availability should make that transition simpler as there is one |
| 120 |
less thing that need to be handled. |
| 121 |
|
| 122 |
## License |
| 123 |
|
| 124 |
This library is licensed under Apache 2.0. Full license text is |
| 125 |
available in [COPYING][copying]. |
| 126 |
|
| 127 |
## Contributing |
| 128 |
|
| 129 |
See [CONTRIBUTING][contributing]. |
| 130 |
|
| 131 |
## Support |
| 132 |
|
| 133 |
Please |
| 134 |
[](https://github.com/google/google-auth-library-php/issuesreport bugs at the project on Github](https://github.com/google/google-auth-library-php/issues](https://github.com/google/google-auth-library-php/issues). Don't |
| 135 |
hesitate to |
| 136 |
[](http://stackoverflow.com/questions/tagged/google-auth-library-phpask questions](http://stackoverflow.com/questions/tagged/google-auth-library-php](http://stackoverflow.com/questions/tagged/google-auth-library-php) |
| 137 |
about the client or APIs on [](http://stackoverflow.comStackOverflow](http://stackoverflow.com](http://stackoverflow.com). |
| 138 |
|
| 139 |
[google-apis-php-client]: https://github.com/google/google-api-php-client |
| 140 |
[application default credentials]: https://developers.google.com/accounts/docs/application-default-credentials |
| 141 |
[contributing]: https://github.com/google/google-auth-library-php/tree/master/CONTRIBUTING.md |
| 142 |
[copying]: https://github.com/google/google-auth-library-php/tree/master/COPYING |
| 143 |
[Guzzle]: https://github.com/guzzle/guzzle |
| 144 |
[developer console]: https://console.developers.google.com |
| 145 |
|