1.55.4 (latest) 1.55.3 1.52.0 1.50.1 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 GCECredentials.

GCECredentials supports authorization on Google Compute Engine.

It can be used to authorize requests using the AuthTokenMiddleware, but will
only succeed if being run on GCE:

```php
use Google\Auth\Credentials\GCECredentials;
use Google\Auth\Middleware\AuthTokenMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

$gce = new GCECredentials();
$middleware = new AuthTokenMiddleware($gce);
$stack = HandlerStack::create();
$stack->push($middleware);

$client = new Client([
   'handler' => $stack,
   'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
   'auth' => 'google_auth'
]);

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

## Namespace

Google \\ Auth \\ Credentials

## Implements

[SignBlobInterface](https://docs.cloud.google.com/php/docs/reference/auth/latest/SignBlobInterface.md.txt) [ProjectIdProviderInterface](https://docs.cloud.google.com/php/docs/reference/auth/latest/ProjectIdProviderInterface.md.txt) [GetQuotaProjectInterface](https://docs.cloud.google.com/php/docs/reference/auth/latest/GetQuotaProjectInterface.md.txt)

## Methods

### __construct

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `iam` | `Google\Auth\Iam|null` \[optional\] An IAM instance. |
| `scope` | `string|string[]` \[optional\] the scope of the access request, expressed either as an array or as a space-delimited string. |
| `targetAudience` | `string` \[optional\] The audience for the ID token. |
| `quotaProject` | `string` \[optional\] Specifies a project to bill for access charges associated with the request. |
| `serviceAccountIdentity` | `string` \[optional\] Specify a service account identity name to use instead of "default". |
| `universeDomain` | `string|null` \[optional\] Specify a universe domain to use instead of fetching one from the metadata server. |
| `enableRegionalAccessBoundary` | `bool` Lookup and include the regional access boundary header. |

### fetchAuthToken

Implements FetchAuthTokenInterface#fetchAuthToken.

Fetches the auth tokens from the GCE metadata host if it is available.
If $httpHandler is not specified a the default HttpHandler is used.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `httpHandler` | `callable|null` callback which delivers psr7 request |
| `headers` | `array` \[optional\] Headers to be inserted into the token endpoint request present. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array` | { A set of auth related metadata, based on the token type. @type string $access_token for access tokens @type int $expires_in for access tokens @type string $token_type for access tokens @type string $id_token for ID tokens } |

### getCacheKey

Returns the Cache Key for the credential token.

The format for the cache key is:
TokenURI

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### getLastReceivedToken

Returns an associative array with the token and
expiration time.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array|null` |   |

### getClientName

Get the client name from GCE metadata.

Subsequent calls will return a cached value.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `httpHandler` | `callable|null` callback which delivers psr7 request |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### getProjectId

Fetch the default Project ID from compute engine.

Returns null if called outside GCE.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `httpHandler` | `callable|null` Callback which delivers psr7 request |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string|null` |   |

### getUniverseDomain

Fetch the default universe domain from the metadata server.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `httpHandler` | `callable|null` Callback which delivers psr7 request |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### updateMetadata

Updates metadata with the authorization token.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `metadata` | `array` metadata hashmap |
| `authUri` | `string` optional auth uri |
| `httpHandler` | `callable|null` callback which delivers psr7 request |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array` | updated metadata hashmap |

### getQuotaProject

Get the quota project used for this API request

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string|null` |   |

### setIsOnGce

Set whether or not we've already checked the GCE environment.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `isOnGce` | `bool` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `void` |   |

### static::getTokenUri

The full uri for accessing the default token.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `serviceAccountIdentity` | `string` \[optional\] Specify a service account identity name to use instead of "default". |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### static::getClientNameUri

The full uri for accessing the default service account.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `serviceAccountIdentity` | `string` \[optional\] Specify a service account identity name to use instead of "default". |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### static::onAppEngineFlexible

Determines if this an App Engine Flexible instance, by accessing the
GAE_INSTANCE environment variable.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `bool` | true if this an App Engine Flexible Instance, false otherwise |

### static::onGce

Determines if this a GCE instance, by accessing the expected metadata
host.

If $httpHandler is not specified a the default HttpHandler is used.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `httpHandler` | `callable|null` callback which delivers psr7 request |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `bool` | True if this a GCEInstance, false otherwise |

## Constants

### cacheKey

```php
Value: 'GOOGLE_AUTH_PHP_GCE'
```

### METADATA_IP

```php
Value: '169.254.169.254'
```
The metadata IP address on appengine instances.

The IP is used instead of the domain 'metadata' to avoid slow responses
when not on Compute Engine.

### TOKEN_URI_PATH

```php
Value: 'v1/instance/service-accounts/default/token'
```
The metadata path of the default token.

### ID_TOKEN_URI_PATH

```php
Value: 'v1/instance/service-accounts/default/identity'
```
The metadata path of the default id token.

### CLIENT_ID_URI_PATH

```php
Value: 'v1/instance/service-accounts/default/email'
```
The metadata path of the client ID.

### PROJECT_ID_URI_PATH

```php
Value: 'v1/project/project-id'
```
The metadata path of the project ID.

### UNIVERSE_DOMAIN_URI_PATH

```php
Value: 'v1/universe/universe-domain'
```
The metadata path of the project ID.

### FLAVOR_HEADER

```php
Value: 'Metadata-Flavor'
```
The header whose presence indicates GCE presence.

### MAX_COMPUTE_PING_TRIES

```php
Value: 3
```
Note: the explicit `timeout` and `tries` below is a workaround. The underlying
issue is that resolving an unknown host on some networks will take
20-30 seconds; making this timeout short fixes the issue, but
could lead to false negatives in the event that we are on GCE, but
the metadata resolution was particularly slow. The latter case is
"unlikely" since the expected 4-nines time is about 0.5 seconds.

This allows us to limit the total ping maximum timeout to 1.5 seconds
for developer desktop scenarios.

### COMPUTE_PING_CONNECTION_TIMEOUT_S

```php
Value: 0.5
```