# Class ServiceAccountCredential (1.73.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [1.73.0 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential)
- [1.69.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.69.0/Google.Apis.Auth.OAuth2.ServiceAccountCredential)
- [1.68.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.68.0/Google.Apis.Auth.OAuth2.ServiceAccountCredential)
- [1.60.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.60.0/Google.Apis.Auth.OAuth2.ServiceAccountCredential)
- [1.59.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.59.0/Google.Apis.Auth.OAuth2.ServiceAccountCredential)
- [1.55.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.55.0/Google.Apis.Auth.OAuth2.ServiceAccountCredential)
- [1.50.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.50.0/Google.Apis.Auth.OAuth2.ServiceAccountCredential)

    public class ServiceAccountCredential : ServiceCredential, IHttpUnsuccessfulResponseHandler, IOidcTokenProvider, ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IBlobSigner

Google OAuth 2.0 credential for accessing protected resources using an access token. The Google OAuth 2.0
Authorization Server supports server-to-server interactions such as those between a web application and Google
Cloud Storage. The requesting application has to prove its own identity to gain access to an API, and an
end-user doesn't have to be involved.


Take a look in <https://developers.google.com/accounts/docs/OAuth2ServiceAccount> for more details.


Since version 1.9.3, service account credential also supports JSON Web Token access token scenario.
In this scenario, instead of sending a signed JWT claim to a token server and exchanging it for
an access token, a locally signed JWT claim bound to an appropriate URI is used as an access token
directly.
See [GetAccessTokenForRequestAsync(string, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential#Google_Apis_Auth_OAuth2_ServiceAccountCredential_GetAccessTokenForRequestAsync_System_String_System_Threading_CancellationToken_) for explanation when JWT access token
is used and when regular OAuth2 token is used.

<br />

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> [ServiceCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential) \> ServiceAccountCredential

## Implements

[IHttpUnsuccessfulResponseHandler](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.IHttpUnsuccessfulResponseHandler), [IOidcTokenProvider](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.IOidcTokenProvider), [ICredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ICredential), [IConfigurableHttpClientInitializer](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.IConfigurableHttpClientInitializer), [ITokenAccessWithHeaders](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ITokenAccessWithHeaders), [ITokenAccess](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ITokenAccess), [IHttpExecuteInterceptor](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.IHttpExecuteInterceptor), [IBlobSigner](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.IBlobSigner)

## Inherited Members

[ServiceCredential.Logger](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Logger) [ServiceCredential.TokenServerUrl](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_TokenServerUrl) [ServiceCredential.Clock](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Clock) [ServiceCredential.AccessMethod](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_AccessMethod) [ServiceCredential.HttpClient](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_HttpClient) [ServiceCredential.Scopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Scopes) [ServiceCredential.Token](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Token) [ServiceCredential.QuotaProject](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_QuotaProject) [ServiceCredential.BuildCreateHttpClientArgs()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_BuildCreateHttpClientArgs) [ServiceCredential.Initialize(ConfigurableHttpClient)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Initialize_Google_Apis_Http_ConfigurableHttpClient_) [ServiceCredential.InterceptAsync(HttpRequestMessage, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_InterceptAsync_System_Net_Http_HttpRequestMessage_System_Threading_CancellationToken_) [ServiceCredential.HandleResponseAsync(HandleUnsuccessfulResponseArgs)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_HandleResponseAsync_Google_Apis_Http_HandleUnsuccessfulResponseArgs_) [ServiceCredential.GetAccessTokenWithHeadersForRequestAsync(string, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_GetAccessTokenWithHeadersForRequestAsync_System_String_System_Threading_CancellationToken_) [object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Apis.Auth.OAuth2](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2)

## Assembly

Google.Apis.Auth.dll

## Constructors

### ServiceAccountCredential(Initializer)

    public ServiceAccountCredential(ServiceAccountCredential.Initializer initializer)

Constructs a new service account credential using the given initializer.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `initializer` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredentialhttps://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer` |

## Fields

### UnixEpoch

    protected static readonly DateTime UnixEpoch

Unix epoch as a `DateTime`

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.datetime` |   |

## Properties

### Id

    public string Id { get; }

Gets the service account ID (typically an e-mail address).

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### Key

    public RSA Key { get; }

Gets the key which is used to sign the request, as specified in
<https://developers.google.com/accounts/docs/OAuth2ServiceAccount#computingsignature>.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.security.cryptography.rsa` |   |

### KeyId

    public string KeyId { get; }

Gets the key id of the key which is used to sign the request.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### ProjectId

    public string ProjectId { get; }

The project ID associated with this credential.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### UniverseDomain

    public string UniverseDomain { get; }

The universe domain this credential belongs to. Won't be null.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### UseJwtAccessWithScopes

    public bool UseJwtAccessWithScopes { get; }

Gets the flag indicating whether Self-Signed JWT should be used when OAuth scopes are set.
This flag will be ignored if this credential has [User](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential#Google_Apis_Auth_OAuth2_ServiceAccountCredential_User) set, meaning
it is used with domain-wide delegation. Self-Signed JWTs won't be used in that case.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` |   |

### User

    public string User { get; }

Gets the email address of the user the application is trying to impersonate in the service account flow
or `null`.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

## Methods

### CreateSignature(byte\[\])

    public string CreateSignature(byte[] data)

Creates a base64 encoded signature for the SHA-256 hash of the specified data.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `data` | `System.Byte` The data to hash and sign. Must not be null. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The base-64 encoded signature. |

### FromServiceAccountData(Stream)

    public static ServiceAccountCredential FromServiceAccountData(Stream credentialData)

Creates a new [ServiceAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential) instance from JSON credential data.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `credentialData` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The stream from which to read the JSON key data for a service account. Must not be null. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential` | The credentials parsed from the service account key data. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.invalidoperationexception` | The `credentialData` does not contain valid JSON service account key data. |

### GetAccessTokenForRequestAsync(string, CancellationToken)

    public override Task<string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default)

Gets an access token to authorize a request.
An OAuth2 access token obtained from [TokenServerUrl](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_TokenServerUrl) will be returned
in the following two cases:

1. If this credential has [Scopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Scopes) associated, but [UseJwtAccessWithScopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential#Google_Apis_Auth_OAuth2_ServiceAccountCredential_UseJwtAccessWithScopes) is false;
2. If this credential is used with domain-wide delegation, that is, the [User](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential#Google_Apis_Auth_OAuth2_ServiceAccountCredential_User) is set; Otherwise, a locally signed JWT will be returned. The signed JWT will contain a "scope" claim with the scopes in [Scopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Scopes) if there are any, otherwise it will contain an "aud" claim with `authUri`. A cached token is used if possible and the token is only refreshed once it's close to its expiry.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `authUri` | `https://learn.microsoft.com/dotnet/api/system.string` The URI the returned token will grant access to. Should be specified if no [Scopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_Scopes) have been specified for the credential. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` The cancellation token. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://learn.microsoft.com/dotnet/api/system.string` | The access token. |

**Overrides** [ServiceCredential.GetAccessTokenForRequestAsync(string, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_GetAccessTokenForRequestAsync_System_String_System_Threading_CancellationToken_)

### GetOidcTokenAsync(OidcTokenOptions, CancellationToken)

    public Task<OidcToken> GetOidcTokenAsync(OidcTokenOptions options, CancellationToken cancellationToken = default)

Returns an OIDC token for the given options.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `options` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.OidcTokenOptions` The options to create the token from. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` The cancellation token that may be used to cancel the request. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.OidcToken` | The OIDC token. |

### RequestAccessTokenAsync(CancellationToken)

    public override Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)

Requests a new token as specified in
<https://developers.google.com/accounts/docs/OAuth2ServiceAccount#makingrequest>.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `taskCancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` Cancellation token to cancel operation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if a new token was received successfully. |

**Overrides** [ServiceCredential.RequestAccessTokenAsync(CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_RequestAccessTokenAsync_System_Threading_CancellationToken_)

### SignBlobAsync(byte\[\], CancellationToken)

    public Task<string> SignBlobAsync(byte[] blob, CancellationToken cancellationToken = default)

Returns the base64 encoded signature of the given blob.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `blob` | `System.Byte` The blob to sign. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` The cancellation token. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://learn.microsoft.com/dotnet/api/system.string` | The base64 encoded signature. |

### ToGoogleCredential()

    public GoogleCredential ToGoogleCredential()

Creates a [GoogleCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential) from this credential.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential` |   |

### WithUseJwtAccessWithScopes(bool)

    public ServiceAccountCredential WithUseJwtAccessWithScopes(bool useJwtAccessWithScopes)

Constructs a new instance of the [ServiceAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential) but with the
given [UseJwtAccessWithScopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential#Google_Apis_Auth_OAuth2_ServiceAccountCredential_UseJwtAccessWithScopes) value.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `useJwtAccessWithScopes` | `https://learn.microsoft.com/dotnet/api/system.boolean` A flag preferring use of self-signed JWTs over OAuth tokens when OAuth scopes are explicitly set. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential` | A new instance of the [ServiceAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential) but with the given [UseJwtAccessWithScopes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential#Google_Apis_Auth_OAuth2_ServiceAccountCredential_UseJwtAccessWithScopes) value. |

## Extension Method

[Utilities.ThrowIfNull\<T\>(T, string)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Util.Utilities#Google_Apis_Util_Utilities_ThrowIfNull__1___0_System_String_)