Class UserCredential (1.60.0)

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

Namespace

GoogleApisAuthOAuth2

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 IAuthorizationCodeFlow

Authorization code flow.

userId string

User identifier.

token 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 IAuthorizationCodeFlow

Authorization code flow.

userId string

User identifier.

token TokenResponse

An initial token for the user.

quotaProjectId 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
ILogger

Properties

Flow

public IAuthorizationCodeFlow Flow { get; }

Gets the authorization code flow.

Property Value
Type Description
IAuthorizationCodeFlow

QuotaProject

public string QuotaProject { get; }
Property Value
Type Description
string

Token

public TokenResponse Token { get; set; }

Gets or sets the token response which contains the access token.

Property Value
Type Description
TokenResponse

UserId

public string UserId { get; }

Gets the user identity.

Property Value
Type Description
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 string

The URI the returned token will grant access to.

cancellationToken CancellationToken

The cancellation token.

Returns
Type Description
Taskstring

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 string

The URI the returned token will grant access to.

cancellationToken CancellationToken

The cancellation token.

Returns
Type Description
TaskAccessTokenWithHeaders

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 HandleUnsuccessfulResponseArgs

Handle response argument which contains properties such as the request, response, current failed try.

Returns
Type Description
Taskbool

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

Parameters
Name Description
request HttpRequestMessage
taskCancellationToken CancellationToken
Returns
Type Description
Task

RefreshTokenAsync(CancellationToken)

public Task<bool> RefreshTokenAsync(CancellationToken taskCancellationToken)

Refreshes the token by calling to RefreshTokenAsync(string, string, CancellationToken). Then it updates the TokenResponse with the new token instance.

Parameter
Name Description
taskCancellationToken CancellationToken

Cancellation token to cancel an operation.

Returns
Type Description
Taskbool

true if the token was refreshed.

RevokeTokenAsync(CancellationToken)

public Task<bool> RevokeTokenAsync(CancellationToken taskCancellationToken)

Asynchronously revokes the token by calling RevokeTokenAsync(string, string, CancellationToken).

Parameter
Name Description
taskCancellationToken CancellationToken

Cancellation token to cancel an operation.

Returns
Type Description
Taskbool

true if the token was revoked successfully.

Extension Method