Show / Hide Table of Contents

Class UserCredential

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
Implements
ICredential
IConfigurableHttpClientInitializer
ITokenAccessWithHeaders
ITokenAccess
IHttpExecuteInterceptor
IHttpUnsuccessfulResponseHandler
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: Google.Apis.Auth.OAuth2
Assembly: Google.Apis.Auth.dll
Syntax
public class UserCredential : ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandler

Constructors

UserCredential(IAuthorizationCodeFlow, string, TokenResponse)

Constructs a new credential instance.

Declaration
public UserCredential(IAuthorizationCodeFlow flow, string userId, TokenResponse token)
Parameters
Type Name Description
IAuthorizationCodeFlow flow

Authorization code flow.

string userId

User identifier.

TokenResponse token

An initial token for the user.

UserCredential(IAuthorizationCodeFlow, string, TokenResponse, string)

Constructs a new credential instance.

Declaration
public UserCredential(IAuthorizationCodeFlow flow, string userId, TokenResponse token, string quotaProjectId)
Parameters
Type Name Description
IAuthorizationCodeFlow flow

Authorization code flow.

string userId

User identifier.

TokenResponse token

An initial token for the user.

string quotaProjectId

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

Fields

Logger

Logger for this class.

Declaration
protected static readonly ILogger Logger
Field Value
Type Description
ILogger

Properties

Flow

Gets the authorization code flow.

Declaration
public IAuthorizationCodeFlow Flow { get; }
Property Value
Type Description
IAuthorizationCodeFlow

QuotaProject

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

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

Token

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

Declaration
public TokenResponse Token { get; set; }
Property Value
Type Description
TokenResponse

UserId

Gets the user identity.

Declaration
public string UserId { get; }
Property Value
Type Description
string

Methods

GetAccessTokenForRequestAsync(string, 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.

Declaration
public virtual Task<string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default)
Parameters
Type Name Description
string authUri

The URI the returned token will grant access to.

CancellationToken cancellationToken

The cancellation token.

Returns
Type Description
Task<string>

The access token.

GetAccessTokenWithHeadersForRequestAsync(string, 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.

Declaration
public Task<AccessTokenWithHeaders> GetAccessTokenWithHeadersForRequestAsync(string authUri = null, CancellationToken cancellationToken = default)
Parameters
Type Name Description
string authUri

The URI the returned token will grant access to.

CancellationToken cancellationToken

The cancellation token.

Returns
Type Description
Task<AccessTokenWithHeaders>

The access token with headers if any.

HandleResponseAsync(HandleUnsuccessfulResponseArgs)

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.

Declaration
public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
Parameters
Type Name Description
HandleUnsuccessfulResponseArgs args

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

Returns
Type Description
Task<bool>

Whether this handler has made a change that requires the request to be resent.

Initialize(ConfigurableHttpClient)

Initializes a HTTP client after it was created.

Declaration
public void Initialize(ConfigurableHttpClient httpClient)
Parameters
Type Name Description
ConfigurableHttpClient httpClient

InterceptAsync(HttpRequestMessage, CancellationToken)

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

Declaration
public Task InterceptAsync(HttpRequestMessage request, CancellationToken taskCancellationToken)
Parameters
Type Name Description
HttpRequestMessage request
CancellationToken taskCancellationToken
Returns
Type Description
Task

RefreshTokenAsync(CancellationToken)

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

Declaration
public Task<bool> RefreshTokenAsync(CancellationToken taskCancellationToken)
Parameters
Type Name Description
CancellationToken taskCancellationToken

Cancellation token to cancel an operation.

Returns
Type Description
Task<bool>

true if the token was refreshed.

RevokeTokenAsync(CancellationToken)

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

Declaration
public Task<bool> RevokeTokenAsync(CancellationToken taskCancellationToken)
Parameters
Type Name Description
CancellationToken taskCancellationToken

Cancellation token to cancel an operation.

Returns
Type Description
Task<bool>

true if the token was revoked successfully.

Implements

ICredential
Google.Apis.Http.IConfigurableHttpClientInitializer
ITokenAccessWithHeaders
ITokenAccess
Google.Apis.Http.IHttpExecuteInterceptor
Google.Apis.Http.IHttpUnsuccessfulResponseHandler
In this article
Back to top Generated by DocFX
OSZAR »