Skip to content

Commit

Permalink
Merge pull request googleapis#63 from bshaffer/add-readme-example
Browse files Browse the repository at this point in the history
RFC: Add code example to readme
  • Loading branch information
tbetbetbe committed Sep 15, 2015
2 parents 3d8ffee + 7d4920d commit c9511cf
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,62 @@ and authorization level for the application independent of the user. This is
the recommended approach to authorize calls to Cloud APIs, particularly when
you're building an application that uses Google Compute Engine.

#### Download your Service Account Credentials JSON file

To use `Application Default Credentials`, You first need to download a set of
JSON credentials for your project. Go to **APIs & Auth** > **Credentials** in
the [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.

```php
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
```

#### Enable the API you want to use

Before making your API call, you must be sure the API you're calling has been
enabled. Go to **APIs & Auth** > **APIs** in the
[Google Developers Console](developer console) and enable the APIs you'd like to
call. For the example below, you must enable the `Drive API`.

#### Call the APIs

As long as you update the environment variable below to point to *your* JSON
credentials file, the following code should output a list of your Drive files.

```php
use GuzzleHttp\Client;
use Google\Auth\ApplicationDefaultCredentials;

// specify the path to your application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');

// define the scopes for your API call
$scopes = ['https://www.googleapis.com/auth/drive.readonly'];

// create the HTTP client
$client = new Client([
'base_url' => 'https://www.googleapis.com',
'defaults' => ['auth' => 'google_auth'] // authorize all requests
]);

// attach this library's auth listener
$fetcher = ApplicationDefaultCredentials::getFetcher($scopes);
$client->getEmitter()->attach($fetcher);

// make the request
$response = $client->get('drive/v2/files');

// show the result!
print_r($response->json());
```

## What about auth in google-apis-php-client?

The goal is for auth done by
Expand Down Expand Up @@ -83,4 +139,4 @@ about the client or APIs on [StackOverflow](http://stackoverflow.com).
[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

[developer console]: https://console.developers.google.com

0 comments on commit c9511cf

Please sign in to comment.