Class UserCredential (1.55.0)

public class UserCredential : IGoogleCredential, 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

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
NameDescription
flowIAuthorizationCodeFlow

Authorization code flow.

userIdString

User identifier.

tokenTokenResponse

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
NameDescription
flowIAuthorizationCodeFlow

Authorization code flow.

userIdString

User identifier.

tokenTokenResponse

An initial token for the user.

quotaProjectIdString

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

Properties

Flow

public IAuthorizationCodeFlow Flow { get; }

Gets the authorization code flow.

Property Value
TypeDescription
IAuthorizationCodeFlow

QuotaProject

public string QuotaProject { get; }
Property Value
TypeDescription
String

Token

public TokenResponse Token { get; set; }

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

Property Value
TypeDescription
TokenResponse

UserId

public string UserId { get; }

Gets the user identity.

Property Value
TypeDescription
String

Methods

GetAccessTokenForRequestAsync(String, CancellationToken)

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

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
NameDescription
authUriString

The URI the returned token will grant access to.

cancellationTokenCancellationToken

The cancellation token.

Returns
TypeDescription
Task<String>

The access token.

GetAccessTokenWithHeadersForRequestAsync(String, CancellationToken)

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

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
NameDescription
authUriString

The URI the returned token will grant access to.

cancellationTokenCancellationToken

The cancellation token.

Returns
TypeDescription
Task<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
NameDescription
argsHandleUnsuccessfulResponseArgs

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

Returns
TypeDescription
Task<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
NameDescription
httpClientConfigurableHttpClient

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
NameDescription
requestHttpRequestMessage
taskCancellationTokenCancellationToken
Returns
TypeDescription
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
NameDescription
taskCancellationTokenCancellationToken

Cancellation token to cancel an operation.

Returns
TypeDescription
Task<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).

Parameter
NameDescription
taskCancellationTokenCancellationToken

Cancellation token to cancel an operation.

Returns
TypeDescription
Task<Boolean>

true if the token was revoked successfully.

Extension Method