Skip to content

Commit 42fc696

Browse files
authored
adds auth code flow to README (#989)
1 parent 984f6a9 commit 42fc696

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,37 @@ foreach ($results as $item) {
8383

8484
> An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php).
8585
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-
8886
1. Follow the instructions to [Create Web Application Credentials](https://developers.google.com/api-client-library/php/auth/web-app#creatingcred)
8987
1. Download the JSON credentials
90-
1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
88+
1. Set the path to these credentials using `Google_Client::setAuthConfig`:
9189

9290
```php
93-
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
91+
$client = new Google_Client();
92+
$client->setAuthConfig('/path/to/client_credentials.json');
9493
```
9594

96-
1. Tell the Google client to use your service account credentials to authenticate:
95+
1. Set the scopes required for the API you are going to call
9796

9897
```php
99-
$client = new Google_Client();
100-
$client->useApplicationDefaultCredentials();
98+
$client->addScope(Google_Service_Drive::DRIVE);
10199
```
102100

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:
101+
1. Set your application's redirect URI
104102

105103
```php
106-
$ client->setSubject($user_to_impersonate);
104+
// Your redirect URI can be any registered URI, but in this example
105+
// we redirect back to this same page
106+
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
107+
$client->setRedirectUri($redirect_uri);
108+
```
109+
110+
1. In the script handling the redirect URI, exchange the authorization code for an access token:
111+
112+
```php
113+
if (isset($_GET['code'])) {
114+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
115+
$client->setAccessToken($token);
116+
}
107117
```
108118

109119
### Authentication with Service Accounts ###
@@ -189,7 +199,7 @@ $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]);
189199
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
190200
```
191201

192-
However, as each property of the JSON API has a corresponding generated class, the above code could also be written lile this:
202+
However, as each property of the JSON API has a corresponding generated class, the above code could also be written like this:
193203

194204
```php
195205
// create the datastore service class

0 commit comments

Comments
 (0)