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
Implements
Inherited Members
Namespace: Google.Apis.Auth.OAuth2
Assembly: Google.Apis.Auth.dll
Syntax
public class UserCredential : ICredential, IConfigurableHttpClientInitializer, 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. |
| System.String | userId | User identifier. |
| TokenResponse | token | An initial token for the user. |
Fields
Logger
Logger for this class.
Declaration
protected static readonly ILogger Logger
Field Value
| Type | Description |
|---|---|
| Google.Apis.Logging.ILogger |
Properties
Flow
Gets the authorization code flow.
Declaration
public IAuthorizationCodeFlow Flow { get; }
Property Value
| Type | Description |
|---|---|
| IAuthorizationCodeFlow |
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 |
|---|---|
| System.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(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | authUri | The URI the returned token will grant access to. |
| System.Threading.CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.String> | The access token. |
HandleResponseAsync(HandleUnsuccessfulResponseArgs)
Declaration
public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
Parameters
| Type | Name | Description |
|---|---|---|
| Google.Apis.Http.HandleUnsuccessfulResponseArgs | args |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Boolean> |
Initialize(ConfigurableHttpClient)
Declaration
public void Initialize(ConfigurableHttpClient httpClient)
Parameters
| Type | Name | Description |
|---|---|---|
| Google.Apis.Http.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 |
|---|---|---|
| System.Net.Http.HttpRequestMessage | request | |
| System.Threading.CancellationToken | taskCancellationToken |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.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 |
|---|---|---|
| System.Threading.CancellationToken | taskCancellationToken | Cancellation token to cancel an operation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Boolean> |
|
RevokeTokenAsync(CancellationToken)
Asynchronously revokes the token by calling RevokeTokenAsync(String, String, CancellationToken).
Declaration
public Task<bool> RevokeTokenAsync(CancellationToken taskCancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Threading.CancellationToken | taskCancellationToken | Cancellation token to cancel an operation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Boolean> |
|