Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 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
[Composer](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
##### Guzzle 5 Compatibility
110
 
111
If you are using [Guzzle 5][Guzzle 5], replace the `create middleware` and
112
`create the HTTP Client` steps with the following:
113
 
114
```php
115
// create the HTTP client
116
$client = new Client([
117
  'base_url' => 'https://www.googleapis.com',
118
  'auth' => 'google_auth'  // authorize all requests
119
]);
120
 
121
// create subscriber
122
$subscriber = ApplicationDefaultCredentials::getSubscriber($scopes);
123
$client->getEmitter()->attach($subscriber);
124
 
125
```
126
 
127
## License
128
 
129
This library is licensed under Apache 2.0. Full license text is
130
available in [COPYING][copying].
131
 
132
## Contributing
133
 
134
See [CONTRIBUTING][contributing].
135
 
136
## Support
137
 
138
Please
139
[report bugs at the project on Github](https://github.com/google/google-auth-library-php/issues). Don't
140
hesitate to
141
[ask questions](http://stackoverflow.com/questions/tagged/google-auth-library-php)
142
about the client or APIs on [StackOverflow](http://stackoverflow.com).
143
 
144
[google-apis-php-client]: https://github.com/google/google-api-php-client
145
[application default credentials]: https://developers.google.com/accounts/docs/application-default-credentials
146
[contributing]: https://github.com/google/google-auth-library-php/tree/master/CONTRIBUTING.md
147
[copying]: https://github.com/google/google-auth-library-php/tree/master/COPYING
148
[Guzzle]: https://github.com/guzzle/guzzle
149
[Guzzle 5]: http://docs.guzzlephp.org/en/5.3
150
[developer console]: https://console.developers.google.com