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;
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Google Auth Library Client - Class ScopedAccessTokenMiddleware (1.47.1)\n\nVersion latestkeyboard_arrow_down\n\n- [1.47.1 (latest)](/php/docs/reference/auth/latest/Middleware.ScopedAccessTokenMiddleware)\n- [1.47.0](/php/docs/reference/auth/1.47.0/Middleware.ScopedAccessTokenMiddleware)\n- [1.46.0](/php/docs/reference/auth/1.46.0/Middleware.ScopedAccessTokenMiddleware)\n- [1.45.2](/php/docs/reference/auth/1.45.2/Middleware.ScopedAccessTokenMiddleware) \nReference documentation and code samples for the Google Auth Library Client class ScopedAccessTokenMiddleware.\n\nScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization\nheader provided by a closure.\n\nThe closure returns an access token, taking the scope, either a single\nstring or an array of strings, as its value. If provided, a cache will be\nused to preserve the access token for a given lifetime.\n\nRequests will be accessed with the authorization header:\n\n'authorization' 'Bearer\n\n\nNamespace\n---------\n\nGoogle \\\\ Auth \\\\ Middleware\n\nMethods\n-------\n\n### __construct\n\nCreates a new ScopedAccessTokenMiddleware.\n\n### __invoke\n\nUpdates the request with an Authorization header when auth is 'scoped'.\n\nE.g this could be used to authenticate using the AppEngine\nAppIdentityService.\n\nuse google\\\\appengine\\\\api\\\\app_identity\\\\AppIdentityService;\nuse Google\\\\Auth\\\\Middleware\\\\ScopedAccessTokenMiddleware;\nuse GuzzleHttp\\\\Client;\nuse GuzzleHttp\\\\HandlerStack;\n\n$scope = '\u003chttps://www.googleapis.com/auth/taskqueue\u003e'\n$middleware = new ScopedAccessTokenMiddleware(\n'AppIdentityService::getAccessToken',\n$scope,\n\\[ 'prefix' =\\\u003e 'Google\\\\Auth\\\\ScopedAccessToken::' \\],\n$cache = new Memcache()\n);\n$stack = HandlerStack::create();\n$stack-\\\u003epush($middleware);\n\n$client = new Client(\\[\n'handler' =\\\u003e $stack,\n'base_url' =\\\u003e '\u003chttps://www.googleapis.com/taskqueue/v1beta2/projects/\u003e',\n'auth' =\\\u003e 'scoped' // authorize all requests\n\\]);\n\n$res = $client-\\\u003eget('myproject/taskqueues/myqueue');\n\nConstants\n---------\n\n### DEFAULT_CACHE_LIFETIME\n\n Value: 1500"]]