Blame | Last modification | View Log | RSS feed
# Google Auth Library for PHP<dl><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><dt>Authors</dt><dd><a href="mailto:temiola@google.com">Tim Emiola</a></dd><dd><a href="mailto:stanleycheung@google.com">Stanley Cheung</a></dd><dd><a href="mailto:betterbrent@google.com">Brent Shaffer</a></dd><dt>Copyright</dt><dd>Copyright © 2015 Google, Inc.</dd><dt>License</dt><dd>Apache 2.0</dd></dl>## DescriptionThis is Google's officially supported PHP client library for using OAuth 2.0authorization and authentication with Google APIs.### Installing via ComposerThe recommended way to install the google auth library is through[Composer](http://getcomposer.org).```bash# Install Composercurl -sS https://getcomposer.org/installer | php```Next, run the Composer command to install the latest stable version:```bashcomposer.phar require google/auth```## Application Default CredentialsThis library provides an implementation of[application default credentials][application default credentials] for PHP.The Application Default Credentials provide a simple way to get authorizationcredentials for use in calling Google APIs.They are best suited for cases when the call needs to have the same identityand authorization level for the application independent of the user. This isthe recommended approach to authorize calls to Cloud APIs, particularly whenyou're building an application that uses Google Compute Engine.#### Download your Service Account Credentials JSON fileTo use `Application Default Credentials`, You first need to download a set ofJSON credentials for your project. Go to **APIs & Auth** > **Credentials** inthe [Google Developers Console][developer console] and select**Service account** from the **Add credentials** dropdown.> This file is your *only copy* of these credentials. It should never be> committed with your source code, and should be stored securely.Once downloaded, store the path to this file in the`GOOGLE_APPLICATION_CREDENTIALS` environment variable.```phpputenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');```> PHP's `putenv` function is just one way to set an environment variable.> Consider using `.htaccess` or apache configuration files as well.#### Enable the API you want to useBefore making your API call, you must be sure the API you're calling has beenenabled. Go to **APIs & Auth** > **APIs** in the[Google Developers Console][developer console] and enable the APIs you'd like tocall. For the example below, you must enable the `Drive API`.#### Call the APIsAs long as you update the environment variable below to point to *your* JSONcredentials file, the following code should output a list of your Drive files.```phpuse Google\Auth\ApplicationDefaultCredentials;use GuzzleHttp\Client;use GuzzleHttp\HandlerStack;// specify the path to your application credentialsputenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');// define the scopes for your API call$scopes = ['https://www.googleapis.com/auth/drive.readonly'];// create middleware$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);$stack = HandlerStack::create();$stack->push($middleware);// create the HTTP client$client = new Client(['handler' => $stack,'base_url' => 'https://www.googleapis.com','auth' => 'google_auth' // authorize all requests]);// make the request$response = $client->get('drive/v2/files');// show the result!print_r((string) $response->getBody());```##### Guzzle 5 CompatibilityIf you are using [Guzzle 5][Guzzle 5], replace the `create middleware` and`create the HTTP Client` steps with the following:```php// create the HTTP client$client = new Client(['base_url' => 'https://www.googleapis.com','auth' => 'google_auth' // authorize all requests]);// create subscriber$subscriber = ApplicationDefaultCredentials::getSubscriber($scopes);$client->getEmitter()->attach($subscriber);```## LicenseThis library is licensed under Apache 2.0. Full license text isavailable in [COPYING][copying].## ContributingSee [CONTRIBUTING][contributing].## SupportPlease[report bugs at the project on Github](https://github.com/google/google-auth-library-php/issues). Don'thesitate to[ask questions](http://stackoverflow.com/questions/tagged/google-auth-library-php)about the client or APIs on [StackOverflow](http://stackoverflow.com).[google-apis-php-client]: https://github.com/google/google-api-php-client[application default credentials]: https://developers.google.com/accounts/docs/application-default-credentials[contributing]: https://github.com/google/google-auth-library-php/tree/master/CONTRIBUTING.md[copying]: https://github.com/google/google-auth-library-php/tree/master/COPYING[Guzzle]: https://github.com/guzzle/guzzle[Guzzle 5]: http://docs.guzzlephp.org/en/5.3[developer console]: https://console.developers.google.com