Copied!

ApplicationDefaultCredentials obtains the default credentials for authorizing a request to a Google service.

Application Default Credentials are described here: https://developers.google.com/accounts/docs/application-default-credentials

This class implements the search for the application default credentials as described in the link.

It provides three factory methods:

  • #get returns the computed credentials object
  • #getSubscriber returns an AuthTokenSubscriber built from the credentials object
  • #getMiddleware returns an AuthTokenMiddleware built from the credentials object

This allows it to be used as follows with GuzzleHttp\Client:

use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; $middleware = ApplicationDefaultCredentials::getMiddleware( 'https://www.googleapis.com/auth/taskqueue' ); $stack = HandlerStack::create(); $stack->push($middleware); $client = new Client([ 'handler' => $stack, 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', 'auth' => 'google_auth' // authorize all requests ]); $res = $client->get('myproject/taskqueues/myqueue'); 
CloneableInstantiable
Methods
public static getCredentials( $scope = NULL, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL, $quotaProject = NULL, $defaultScope = NULL, ?string $universeDomain = NULL, Psr\Log\LoggerInterface|false|?null $logger = NULL)
 

Obtains the default FetchAuthTokenInterface implementation to use in this environment.

  • param string|string[] $scope the scope of the access request, expressed either as an Array or as a space-delimited String.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • param string|null $quotaProject specifies a project to bill for access charges associated with the request.
  • param string|string[]|null $defaultScope The default scope to use if no user-defined scopes exist, expressed either as an Array or as a space-delimited string.
  • param string|null $universeDomain Specifies a universe domain to use for the calling client library.
  • param null|false|\LoggerInterface $logger A PSR3 compliant LoggerInterface.
  • return \FetchAuthTokenInterface
  • throws \DomainException if no implementation can be obtained.
public static getDefaultLogger() : ?Psr\Log\LoggerInterface
 

Returns a StdOutLogger instance

  • internal
  • return null|\LoggerInterface
public static getIdTokenCredentials( $targetAudience, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 

Obtains the default FetchAuthTokenInterface implementation to use in this environment, configured with a $targetAudience for fetching an ID token.

  • param string $targetAudience The audience for the ID token.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \FetchAuthTokenInterface
  • throws \DomainException if no implementation can be obtained.
  • throws \InvalidArgumentException if JSON "type" key is invalid
public static getIdTokenMiddleware( $targetAudience, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 

Obtains an AuthTokenMiddleware which will fetch an ID token to use in the Authorization header. The middleware is configured with the default FetchAuthTokenInterface implementation to use in this environment.

If supplied, $targetAudience is used to set the "aud" on the resulting ID token.

  • param string $targetAudience The audience for the ID token.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \AuthTokenMiddleware
  • throws \DomainException if no implementation can be obtained.
public static getMiddleware( $scope = NULL, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL, $quotaProject = NULL)
 

Obtains an AuthTokenMiddleware that uses the default FetchAuthTokenInterface implementation to use in this environment.

If supplied, $scope is used to in creating the credentials instance if this does not fallback to the compute engine defaults.

  • param string|string[] $scope the scope of the access request, expressed either as an Array or as a space-delimited String.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • param string $quotaProject specifies a project to bill for access charges associated with the request.
  • return \AuthTokenMiddleware
  • throws \DomainException if no implementation can be obtained.
public static getProxyIdTokenMiddleware( $targetAudience, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 

Obtains an ProxyAuthTokenMiddleware which will fetch an ID token to use in the Authorization header. The middleware is configured with the default FetchAuthTokenInterface implementation to use in this environment.

If supplied, $targetAudience is used to set the "aud" on the resulting ID token.

  • param string $targetAudience The audience for the ID token.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \ProxyAuthTokenMiddleware
  • throws \DomainException if no implementation can be obtained.
public static getSubscriber( $scope = NULL, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 
  • deprecated Obtains an AuthTokenSubscriber that uses the default FetchAuthTokenInterface implementation to use in this environment. If supplied, $scope is used to in creating the credentials instance if this does not fallback to the compute engine defaults.
  • param string|string[] $scope the scope of the access request, expressed either as an Array or as a space-delimited String.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \AuthTokenSubscriber
  • throws \DomainException if no implementation can be obtained.
Constants
private Google\Auth\ApplicationDefaultCredentials::SDK_DEBUG_ENV_VAR = 'GOOGLE_SDK_PHP_LOGGING'
Methods
private static notFound()
 
  • return string
private static onGce(?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 
  • param callable|null $httpHandler
  • param array|null $cacheConfig
  • param \CacheItemPoolInterface|null $cache
  • return bool
Methods
public static getCredentials( $scope = NULL, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL, $quotaProject = NULL, $defaultScope = NULL, ?string $universeDomain = NULL, Psr\Log\LoggerInterface|false|?null $logger = NULL)
 

Obtains the default FetchAuthTokenInterface implementation to use in this environment.

  • param string|string[] $scope the scope of the access request, expressed either as an Array or as a space-delimited String.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • param string|null $quotaProject specifies a project to bill for access charges associated with the request.
  • param string|string[]|null $defaultScope The default scope to use if no user-defined scopes exist, expressed either as an Array or as a space-delimited string.
  • param string|null $universeDomain Specifies a universe domain to use for the calling client library.
  • param null|false|\LoggerInterface $logger A PSR3 compliant LoggerInterface.
  • return \FetchAuthTokenInterface
  • throws \DomainException if no implementation can be obtained.
public static getDefaultLogger() : ?Psr\Log\LoggerInterface
 

Returns a StdOutLogger instance

  • internal
  • return null|\LoggerInterface
public static getIdTokenCredentials( $targetAudience, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 

Obtains the default FetchAuthTokenInterface implementation to use in this environment, configured with a $targetAudience for fetching an ID token.

  • param string $targetAudience The audience for the ID token.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \FetchAuthTokenInterface
  • throws \DomainException if no implementation can be obtained.
  • throws \InvalidArgumentException if JSON "type" key is invalid
public static getIdTokenMiddleware( $targetAudience, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 

Obtains an AuthTokenMiddleware which will fetch an ID token to use in the Authorization header. The middleware is configured with the default FetchAuthTokenInterface implementation to use in this environment.

If supplied, $targetAudience is used to set the "aud" on the resulting ID token.

  • param string $targetAudience The audience for the ID token.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \AuthTokenMiddleware
  • throws \DomainException if no implementation can be obtained.
public static getMiddleware( $scope = NULL, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL, $quotaProject = NULL)
 

Obtains an AuthTokenMiddleware that uses the default FetchAuthTokenInterface implementation to use in this environment.

If supplied, $scope is used to in creating the credentials instance if this does not fallback to the compute engine defaults.

  • param string|string[] $scope the scope of the access request, expressed either as an Array or as a space-delimited String.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • param string $quotaProject specifies a project to bill for access charges associated with the request.
  • return \AuthTokenMiddleware
  • throws \DomainException if no implementation can be obtained.
public static getProxyIdTokenMiddleware( $targetAudience, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 

Obtains an ProxyAuthTokenMiddleware which will fetch an ID token to use in the Authorization header. The middleware is configured with the default FetchAuthTokenInterface implementation to use in this environment.

If supplied, $targetAudience is used to set the "aud" on the resulting ID token.

  • param string $targetAudience The audience for the ID token.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \ProxyAuthTokenMiddleware
  • throws \DomainException if no implementation can be obtained.
public static getSubscriber( $scope = NULL, ?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 
  • deprecated Obtains an AuthTokenSubscriber that uses the default FetchAuthTokenInterface implementation to use in this environment. If supplied, $scope is used to in creating the credentials instance if this does not fallback to the compute engine defaults.
  • param string|string[] $scope the scope of the access request, expressed either as an Array or as a space-delimited String.
  • param callable|null $httpHandler callback which delivers psr7 request
  • param array|null $cacheConfig configuration for the cache when it's present
  • param \CacheItemPoolInterface|null $cache A cache implementation, may be provided if you have one already available for use.
  • return \AuthTokenSubscriber
  • throws \DomainException if no implementation can be obtained.
private static notFound()
 
  • return string
private static onGce(?callable $httpHandler = NULL, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)
 
  • param callable|null $httpHandler
  • param array|null $cacheConfig
  • param \CacheItemPoolInterface|null $cache
  • return bool
© 2025 Bruce Wells