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.
Implements
ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandlerNamespace
Google.Apis.Auth.OAuth2Assembly
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(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 | |
---|---|
Name | Description |
authUri | String 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)
public async 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 | |
---|---|
Name | Description |
authUri | String 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)
public async 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 |
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 | |
---|---|
Name | Description |
httpClient | ConfigurableHttpClient |
InterceptAsync(HttpRequestMessage, CancellationToken)
public async 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 async 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 |
Task<Boolean> |
|
RevokeTokenAsync(CancellationToken)
public async 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 |
Task<Boolean> |
|