# Google Auth Library Client - Class ScopedAccessTokenMiddleware (1.50.1)

1.50.1 (latest) 1.50.0 1.49.0 1.48.1 1.47.1 1.46.0 1.45.2 Reference documentation and code samples for the Google Auth Library Client class ScopedAccessTokenMiddleware.

ScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization
header provided by a closure.

The closure returns an access token, taking the scope, either a single
string or an array of strings, as its value. If provided, a cache will be
used to preserve the access token for a given lifetime.

Requests will be accessed with the authorization header:

'authorization' 'Bearer


## Namespace

Google \\ Auth \\ Middleware

## Methods

### __construct

Creates a new ScopedAccessTokenMiddleware.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `tokenFunc` | `callable` a token generator function |
| `scopes` | `string[]|string` the token authentication scopes |
| `cacheConfig` | `array|null` configuration for the cache when it's present |
| `cache` | `Psr\Cache\CacheItemPoolInterface|null` an implementation of CacheItemPoolInterface |

### __invoke

Updates the request with an Authorization header when auth is 'scoped'.

E.g this could be used to authenticate using the AppEngine
AppIdentityService.

use google\\appengine\\api\\app_identity\\AppIdentityService;
use Google\\Auth\\Middleware\\ScopedAccessTokenMiddleware;
use GuzzleHttp\\Client;
use GuzzleHttp\\HandlerStack;

$scope = '<https://www.googleapis.com/auth/taskqueue>'
$middleware = new ScopedAccessTokenMiddleware(
'AppIdentityService::getAccessToken',
$scope,
\[ 'prefix' =\> 'Google\\Auth\\ScopedAccessToken::' \],
$cache = new Memcache()
);
$stack = HandlerStack::create();
$stack-\>push($middleware);

$client = new Client(\[
'handler' =\> $stack,
'base_url' =\> '<https://www.googleapis.com/taskqueue/v1beta2/projects/>',
'auth' =\> 'scoped' // authorize all requests
\]);

$res = $client-\>get('myproject/taskqueues/myqueue');

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `handler` | `callable` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `Closure` |   |

## Constants

### DEFAULT_CACHE_LIFETIME

    Value: 1500