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

    public class UserCredential : ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandler

OAuth 2.0 credential for accessing protected resources using an access token, as well as optionally refreshing
the access token when it expires using a refresh token.

## Inheritance

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

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

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

### UserCredential(IAuthorizationCodeFlow, string, TokenResponse)

    public UserCredential(IAuthorizationCodeFlow flow, string userId, TokenResponse token)

Constructs a new credential instance.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `flow` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Flows.IAuthorizationCodeFlow` Authorization code flow. |
| `userId` | `https://learn.microsoft.com/dotnet/api/system.string` User identifier. |
| `token` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Responses.TokenResponse` An initial token for the user. |

### UserCredential(IAuthorizationCodeFlow, string, TokenResponse, string)

    public UserCredential(IAuthorizationCodeFlow flow, string userId, TokenResponse token, string quotaProjectId)

Constructs a new credential instance.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `flow` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Flows.IAuthorizationCodeFlow` Authorization code flow. |
| `userId` | `https://learn.microsoft.com/dotnet/api/system.string` User identifier. |
| `token` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Responses.TokenResponse` An initial token for the user. |
| `quotaProjectId` | `https://learn.microsoft.com/dotnet/api/system.string` The ID of the project associated to this credential for the purposes of quota calculation and billing. Can be null. |

## Fields

### Logger

    protected static readonly ILogger Logger

Logger for this class.

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

## Properties

### Flow

    public IAuthorizationCodeFlow Flow { get; }

Gets the authorization code flow.

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

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

### Token

    public TokenResponse Token { get; set; }

Gets or sets 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` |   |

### UserId

    public string UserId { get; }

Gets the user identity.

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

## Methods

### GetAccessTokenForRequestAsync(string, CancellationToken)

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

Gets an access token to authorize a request.
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://learn.microsoft.com/dotnet/api/system.string` | The access token. |

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

Handles an abnormal response when sending a HTTP request.
A simple rule must be followed, if you modify the request object in a way that the abnormal response can
be resolved, you must return `true`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `args` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Http.HandleUnsuccessfulResponseArgs` Handle response argument which contains properties such as the request, response, current failed try. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://learn.microsoft.com/dotnet/api/system.boolean` | Whether this handler has made a change that requires the request to be resent. |

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

Default implementation is to try to refresh the access token if there is no access token or if we are 1
minute away from expiration. If token server is unavailable, it will try to use the access token even if
has expired. If successful, it will call [Intercept(HttpRequestMessage, string)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.IAccessMethod#Google_Apis_Auth_OAuth2_IAccessMethod_Intercept_System_Net_Http_HttpRequestMessage_System_String_).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `request` | `https://learn.microsoft.com/dotnet/api/system.net.http.httprequestmessage` |
| `taskCancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` |

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

### RefreshTokenAsync(CancellationToken)

    public Task<bool> RefreshTokenAsync(CancellationToken taskCancellationToken)

Refreshes the token by calling to
[RefreshTokenAsync(string, string, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Flows.IAuthorizationCodeFlow#Google_Apis_Auth_OAuth2_Flows_IAuthorizationCodeFlow_RefreshTokenAsync_System_String_System_String_System_Threading_CancellationToken_).
Then it updates the [TokenResponse](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Responses.TokenResponse) with the new token instance.

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

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

### RevokeTokenAsync(CancellationToken)

    public Task<bool> RevokeTokenAsync(CancellationToken taskCancellationToken)

Asynchronously revokes the token by calling
[RevokeTokenAsync(string, string, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.Flows.IAuthorizationCodeFlow#Google_Apis_Auth_OAuth2_Flows_IAuthorizationCodeFlow_RevokeTokenAsync_System_String_System_String_System_Threading_CancellationToken_).

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

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

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

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