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
+32
Original file line number
Diff line number
Diff line change
@@ -163,3 +163,35 @@ By default, access tokens are not persisted anywhere. There are some built-in m
163
163
-`SimpleCacheTokenPersistence` Takes a PSR-16 SimpleCache and optionally a key name (default: `guzzle-oauth2-token`) where the access token will be saved. This allows any PSR-16 compatible cache to be used.
164
164
165
165
If you want to use your own persistence layer, you should write your own class that implements `TokenPersistenceInterface`.
166
+
167
+
To enable token persistence, you must use the `OAuth2Middleware::setTokenPersistence()` or `OAuth2Subscriber::setTokenPersistence()` method, like this:
168
+
169
+
```php
170
+
use kamermans\OAuth2\Persistence\FileTokenPersistence;
171
+
172
+
$token_path = '/tmp/access_token.json';
173
+
$token_persistence = new FileTokenPersistence($token_path);
174
+
175
+
$grant_type = new ClientCredentials($reauth_client, $reauth_config);
176
+
$oauth = new OAuth2Middleware($grant_type);
177
+
$oauth->setTokenPersistence($token_persistence);
178
+
```
179
+
180
+
Please see the `src/Persistence/` directory for more information on persistence.
181
+
182
+
### Manually Setting an Access Token
183
+
For a manually-obtained access token, you can use the `NullGrantType` and set the access token manually as follows:
184
+
185
+
```php
186
+
use kamermans\OAuth2\GrantType\NullGrantType;
187
+
188
+
$oauth = new OAuth2Middleware(new NullGrantType);
189
+
$oauth->setAccessToken([
190
+
// Your access token goes here
191
+
'access_token' => 'abcdefghijklmnop',
192
+
// You can specify 'expires_in` as well, but it doesn't make much sense in this scenario
193
+
// You can also specify 'scope' => 'list of scopes'
194
+
]);
195
+
```
196
+
197
+
Note that if the access token is not set using `setAccessToken()`, a `kamermans\OAuth2\Exception\ReauthorizationException` will be thrown since the `NullGrantType` has no way to get a new access token.
0 commit comments