You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+189-3Lines changed: 189 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server.
7
7
8
8
## Beta ##
9
-
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.
9
+
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.
And then browsing to the host and port you specified
61
61
(in the above example, `http://localhost:8000`).
62
62
63
+
### Basic Example ###
64
+
63
65
```php
64
66
// include your composer dependencies
65
67
require_once 'vendor/autoload.php';
@@ -77,6 +79,171 @@ foreach ($results as $item) {
77
79
}
78
80
```
79
81
82
+
### Authentication with OAuth ###
83
+
84
+
> An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php).
85
+
86
+
**NOTE:** If you are using Google App Engine or Google Compute Engine, you can skip steps 1-3, as Application Default Credentials are included automatically when `useApplicationDefaultCredentials` is called.
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 the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
1. Tell the Google client to use your service account credentials to authenticate:
97
+
98
+
```php
99
+
$client = new Google_Client();
100
+
$client->useApplicationDefaultCredentials();
101
+
```
102
+
103
+
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:
104
+
105
+
```php
106
+
$ client->setSubject($user_to_impersonate);
107
+
```
108
+
109
+
### Authentication with Service Accounts ###
110
+
111
+
> An example of this can be seen in [`examples/service-account.php`](examples/service-account.php).
112
+
113
+
1. Follow the instructions to [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount)
114
+
1. Download the JSON credentials
115
+
1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
1. Tell the Google client to use your service account credentials to authenticate:
122
+
123
+
```php
124
+
$client = new Google_Client();
125
+
$client->useApplicationDefaultCredentials();
126
+
```
127
+
128
+
1. Set the scopes required for the API you are going to call
129
+
130
+
```php
131
+
$client->addScope(Google_Service_Drive::DRIVE);
132
+
```
133
+
134
+
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:
135
+
136
+
```php
137
+
$client->setSubject($user_to_impersonate);
138
+
```
139
+
140
+
### Making Requests ###
141
+
142
+
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/).
143
+
144
+
A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this:
145
+
146
+
```json
147
+
POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY
148
+
149
+
{
150
+
"query": {
151
+
"kind": [{
152
+
"name": "Book"
153
+
}],
154
+
"order": [{
155
+
"property": {
156
+
"name": "title"
157
+
},
158
+
"direction": "descending"
159
+
}],
160
+
"limit": 10
161
+
}
162
+
}
163
+
```
164
+
165
+
Using this library, the same call would look something like this:
166
+
167
+
```php
168
+
// create the datastore service class
169
+
$datastore = new Google_Service_Datastore($client)
170
+
171
+
// build the query - this maps directly to the JSON
172
+
$query = new Google_Service_Datastore_Query([
173
+
'kind' => [
174
+
[
175
+
'name' => 'Book',
176
+
],
177
+
],
178
+
'order' => [
179
+
'property' => [
180
+
'name' => 'title',
181
+
],
182
+
'direction' => 'descending',
183
+
],
184
+
'limit' => 10,
185
+
]);
186
+
187
+
// build the request and response
188
+
$request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]);
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.
221
+
222
+
### Making HTTP Requests Directly ###
223
+
224
+
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.
225
+
226
+
The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlephp.org/), so any request made using the client will contain the corresponding authorization.
227
+
228
+
```php
229
+
// create the Google client
230
+
$client = new Google_Client();
231
+
232
+
/**
233
+
* Set your method for authentication. Depending on the API, This could be
234
+
* directly with an access token, API key, or (recommended) using
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:
@@ -104,6 +271,25 @@ $tokenCallback = function ($cacheKey, $accessToken) use ($logger) {
104
271
$client->setTokenCallback($tokenCallback);
105
272
```
106
273
274
+
### Debugging Your HTTP Request using Charles ###
275
+
276
+
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:
277
+
278
+
```php
279
+
// FOR DEBUGGING ONLY
280
+
$httpClient = new GuzzleHttp\Client([
281
+
'proxy' => 'localhost:8888', // by default, Charles runs on localhost port 8888
282
+
'verify' => false, // otherwise HTTPS requests will fail.
283
+
]);
284
+
285
+
$client = new Google_Client();
286
+
$client->setHttpClient($httpClient);
287
+
```
288
+
289
+
Now all calls made by this library will appear in the Charles UI.
290
+
291
+
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`.
0 commit comments