Class AuthorizationCodeFlow
Thread-safe OAuth 2.0 authorization code flow that manages and persists end-user credentials.
This is designed to simplify the flow in which an end-user authorizes the application to access their protected data, and then the application has access to their data based on an access token and a refresh token to refresh that access token when it expires.
Inherited Members
Namespace: Google.Apis.Auth.OAuth2.Flows
Assembly: Google.Apis.Auth.dll
Syntax
public class AuthorizationCodeFlow : IAuthorizationCodeFlow, IDisposable
Constructors
AuthorizationCodeFlow(Initializer)
Constructs a new flow using the initializer's properties.
Declaration
public AuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer)
Parameters
Type | Name | Description |
---|---|---|
AuthorizationCodeFlow.Initializer | initializer |
Properties
AccessMethod
Gets the method for presenting the access token to the resource server.
Declaration
public IAccessMethod AccessMethod { get; }
Property Value
Type | Description |
---|---|
IAccessMethod |
AuthorizationServerUrl
Gets the authorization code server URL.
Declaration
public string AuthorizationServerUrl { get; }
Property Value
Type | Description |
---|---|
string |
ClientSecrets
Gets the client secrets which includes the client identifier and its secret.
Declaration
public ClientSecrets ClientSecrets { get; }
Property Value
Type | Description |
---|---|
ClientSecrets |
Clock
Gets the clock.
Declaration
public IClock Clock { get; }
Property Value
Type | Description |
---|---|
IClock |
DataStore
Gets the data store used to store the credentials.
Declaration
public IDataStore DataStore { get; }
Property Value
Type | Description |
---|---|
IDataStore |
HttpClient
Gets the HTTP client used to make authentication requests to the server.
Declaration
public ConfigurableHttpClient HttpClient { get; }
Property Value
Type | Description |
---|---|
ConfigurableHttpClient |
Scopes
Gets the scopes which indicate the API access your application is requesting.
Declaration
public IEnumerable<string> Scopes { get; }
Property Value
Type | Description |
---|---|
IEnumerable<string> |
TokenServerUrl
Gets the token server URL.
Declaration
public string TokenServerUrl { get; }
Property Value
Type | Description |
---|---|
string |
Methods
CreateAuthorizationCodeRequest(string)
Creates an authorization code request with the specified redirect URI.
Declaration
public virtual AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
Parameters
Type | Name | Description |
---|---|---|
string | redirectUri |
Returns
Type | Description |
---|---|
AuthorizationCodeRequestUrl |
CreateAuthorizationCodeTokenRequest(string, string, string)
Creates a AuthorizationCodeTokenRequest for the given parameters.
Declaration
protected AuthorizationCodeTokenRequest CreateAuthorizationCodeTokenRequest(string userId, string code, string redirectUri)
Parameters
Type | Name | Description |
---|---|---|
string | userId | |
string | code | |
string | redirectUri |
Returns
Type | Description |
---|---|
AuthorizationCodeTokenRequest |
DeleteTokenAsync(string, CancellationToken)
Asynchronously deletes the user's token using the flow's Google.Apis.Util.Store.IDataStore.
Declaration
public Task DeleteTokenAsync(string userId, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | User identifier. |
CancellationToken | taskCancellationToken | Cancellation token to cancel operation. |
Returns
Type | Description |
---|---|
Task |
Dispose()
Declaration
public void Dispose()
ExchangeCodeForTokenAsync(string, AuthorizationCodeTokenRequest, CancellationToken)
Executes authorizationCodeTokenReq
and stores and returns the received token.
Declaration
protected Task<TokenResponse> ExchangeCodeForTokenAsync(string userId, AuthorizationCodeTokenRequest authorizationCodeTokenReq, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | |
AuthorizationCodeTokenRequest | authorizationCodeTokenReq | |
CancellationToken | taskCancellationToken |
Returns
Type | Description |
---|---|
Task<TokenResponse> |
ExchangeCodeForTokenAsync(string, string, string, CancellationToken)
Asynchronously exchanges an authorization code for an access token.
Declaration
public Task<TokenResponse> ExchangeCodeForTokenAsync(string userId, string code, string redirectUri, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | User identifier. |
string | code | Authorization code received from the authorization server. |
string | redirectUri | Redirect URI which is used in the token request. |
CancellationToken | taskCancellationToken | Cancellation token to cancel operation. |
Returns
Type | Description |
---|---|
Task<TokenResponse> | Token response which contains the access token. |
FetchTokenAsync(string, TokenRequest, CancellationToken)
Retrieve a new token from the server using the specified request.
Declaration
public Task<TokenResponse> FetchTokenAsync(string userId, TokenRequest request, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | User identifier. |
TokenRequest | request | Token request. |
CancellationToken | taskCancellationToken | Cancellation token to cancel operation. |
Returns
Type | Description |
---|---|
Task<TokenResponse> | Token response with the new access token. |
LoadTokenAsync(string, CancellationToken)
Asynchronously loads the user's token using the flow's Google.Apis.Util.Store.IDataStore.
Declaration
public Task<TokenResponse> LoadTokenAsync(string userId, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | User identifier |
CancellationToken | taskCancellationToken | Cancellation token to cancel operation |
Returns
Type | Description |
---|---|
Task<TokenResponse> | Token response |
RefreshTokenAsync(string, string, CancellationToken)
Asynchronously refreshes an access token using a refresh token.
Declaration
public Task<TokenResponse> RefreshTokenAsync(string userId, string refreshToken, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | User identifier. |
string | refreshToken | Refresh token which is used to get a new access token. |
CancellationToken | taskCancellationToken | Cancellation token to cancel operation. |
Returns
Type | Description |
---|---|
Task<TokenResponse> | Token response which contains the access token and the input refresh token. |
RevokeTokenAsync(string, string, CancellationToken)
Asynchronously revokes the specified token. This method disconnects the user's account from the OAuth 2.0 application. It should be called upon removing the user account from the site.
Declaration
public virtual Task RevokeTokenAsync(string userId, string token, CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
string | userId | User identifier. |
string | token | Access token to be revoked. |
CancellationToken | taskCancellationToken | Cancellation token to cancel operation. |
Returns
Type | Description |
---|---|
Task |
|
Remarks
If revoking the token succeeds, the user's credential is removed from the data store and the user MUST authorize the application again before the application can access the user's private resources.
ShouldForceTokenRetrieval()
Indicates if a new token needs to be retrieved and stored regardless of normal circumstances.
Declaration
public virtual bool ShouldForceTokenRetrieval()
Returns
Type | Description |
---|---|
bool |