# Class ServiceCredential (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.ServiceCredential)
- [1.69.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.69.0/Google.Apis.Auth.OAuth2.ServiceCredential)
- [1.68.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.68.0/Google.Apis.Auth.OAuth2.ServiceCredential)
- [1.60.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.60.0/Google.Apis.Auth.OAuth2.ServiceCredential)
- [1.59.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.59.0/Google.Apis.Auth.OAuth2.ServiceCredential)
- [1.55.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.55.0/Google.Apis.Auth.OAuth2.ServiceCredential)
- [1.50.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/1.50.0/Google.Apis.Auth.OAuth2.ServiceCredential)

    public abstract class ServiceCredential : ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandler

This type of Google OAuth 2.0 credential enables access to protected resources using an access token when
interacting server to server. For example, a service account credential could be used to access Google Cloud
Storage from a web application without a user's involvement.


[ServiceAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential) inherits from this class in order to support Service Accounts. More
details available at: <https://developers.google.com/accounts/docs/OAuth2ServiceAccount>.
[ComputeCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ComputeCredential) is another example of a class that inherits from this
class in order to support Compute credentials. For more information about Compute authentication, see:
<https://cloud.google.com/compute/docs/authentication>.


[ExternalAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ExternalAccountCredential) inherits from this class to support both Workload Identity Federation
and Workforce Identity Federation. You can read more about these topics in
<https://cloud.google.com/iam/docs/workload-identity-federation> and
<https://cloud.google.com/iam/docs/workforce-identity-federation> respectively.
Note that in the case of Workforce Identity Federation, the external account does not represent a service account
but a user account, so, the fact that [ExternalAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ExternalAccountCredential) inherits from [ServiceCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential)
might be construed as misleading. In reality [ServiceCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential) is not tied to a service account
in terms of implementation, only in terms of name. For instance, a better name for this class might have been NoUserFlowCredential, and
in that sense, it's correct that [ExternalAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ExternalAccountCredential) inherits from [ServiceCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential)
even when representing a Workforce Identity Federation account.

<br />

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> ServiceCredential

## Implements

[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), [IHttpUnsuccessfulResponseHandler](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.IHttpUnsuccessfulResponseHandler)

## Inherited Members

[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)

## Derived Types

[ComputeCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ComputeCredential) [ExternalAccountAuthorizedUserCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ExternalAccountAuthorizedUserCredential) [ExternalAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ExternalAccountCredential) [ImpersonatedCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ImpersonatedCredential) [ServiceAccountCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceAccountCredential)

## 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

### ServiceCredential(Initializer)

    public ServiceCredential(ServiceCredential.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.ServiceCredentialhttps://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential.Initializer` |

## Fields

### Logger

    protected static readonly ILogger Logger

Logger for this class

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `Google.Apis.Logging.ILogger` |   |

## Properties

### AccessMethod

    public IAccessMethod AccessMethod { get; }

Gets the method for presenting the access token to the resource server.

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

### Clock

    public IClock Clock { get; }

Gets the clock used to refresh the token if it expires.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Util.IClock` |   |

### HttpClient

    public ConfigurableHttpClient HttpClient { get; }

Gets the HTTP client used to make authentication requests to the server.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.ConfigurableHttpClient` |   |

### QuotaProject

    public string QuotaProject { get; }

The ID of the project associated to this credential for the purposes of
quota calculation and billing. May be null.

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

### Scopes

    public IEnumerable<string> Scopes { get; set; }

Scopes to request during the authorization grant. May be null or empty.

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

**Remarks** If the scopes are pre-granted through the environment, like in GCE where scopes are granted to the VM,
scopes set here will be ignored.

### Token

    public TokenResponse Token { get; set; }

Gets the token response which contains the access token.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Responses.TokenResponse` |   |

### TokenServerUrl

    public string TokenServerUrl { get; }

Gets the token server URL.

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

**Remarks** May be null for credential types that resolve token endpoints just before obtaining an access token.
This is the case for [ImpersonatedCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ImpersonatedCredential) where the [SourceCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ImpersonatedCredential#Google_Apis_Auth_OAuth2_ImpersonatedCredential_SourceCredential)
is a [ComputeCredential](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ComputeCredential).

## Methods

### BuildCreateHttpClientArgs()

    protected CreateHttpClientArgs BuildCreateHttpClientArgs()

Builds HTTP client creation args from all this credential settings.
These are used for initializing [HttpClient](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ServiceCredential#Google_Apis_Auth_OAuth2_ServiceCredential_HttpClient).

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

### GetAccessTokenForRequestAsync(string, CancellationToken)

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

Gets an access token to authorize a request. If the existing token expires soon, try to refresh it first.
[GetAccessTokenForRequestAsync(string, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.ITokenAccess#Google_Apis_Auth_OAuth2_ITokenAccess_GetAccessTokenForRequestAsync_System_String_System_Threading_CancellationToken_)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `authUri` | `https://learn.microsoft.com/dotnet/api/system.string` |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` |

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

### GetAccessTokenWithHeadersForRequestAsync(string, CancellationToken)

    public Task<AccessTokenWithHeaders> GetAccessTokenWithHeadersForRequestAsync(string authUri = null, CancellationToken cancellationToken = default)

Gets an access token to authorize a request.
The token might be accompanied by extra information that should be sent
in the form of headers.
Implementations should handle automatic refreshes of the token
if they are supported.
The `authUri` might be required by some credential types
(e.g. the JWT access token) while other credential types
migth just ignore it.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `authUri` | `https://learn.microsoft.com/dotnet/api/system.string` The URI the returned token will grant access to. |
| `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://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.AccessTokenWithHeaders` | The access token with headers if any. |

### HandleResponseAsync(HandleUnsuccessfulResponseArgs)

    public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)

Decorates unsuccessful responses, returns true if the response gets modified.
See IHttpUnsuccessfulResponseHandler for more information.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `args` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.HandleUnsuccessfulResponseArgs` |

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

### Initialize(ConfigurableHttpClient)

    public void Initialize(ConfigurableHttpClient httpClient)

Initializes a HTTP client after it was created.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `httpClient` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.ConfigurableHttpClient` |

### InterceptAsync(HttpRequestMessage, CancellationToken)

    public Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)

Invoked before the request is being sent.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `request` | `https://learn.microsoft.com/dotnet/api/system.net.http.httprequestmessage` The HTTP request message. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` Cancellation token to cancel the operation. |

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

### RequestAccessTokenAsync(CancellationToken)

    public abstract Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)

Requests a new token.

| **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. |

## 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_)