public abstract class ServiceCredential : object, ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandler
This type of Google OAuth 2.0 credential enables access to protected resources using an access token when interacting server to server. For example, a service account credential could be used to access Google Cloud Storage from a web application without a user's involvement.
ServiceAccountCredential
inherits from this class in order to support Service Account. More
details available at: https://developers.google.com/accounts/docs/OAuth2ServiceAccount. ComputeCredential is another example for a class that inherits from this class in order to support Compute credentials. For more information about Compute authentication, see: https://cloud.google.com/compute/docs/authentication.
Implements
ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandlerNamespace
Google.Apis.Auth.OAuth2Assembly
Google.Apis.Auth.dll
Constructors
ServiceCredential(ServiceCredential.Initializer)
public ServiceCredential(ServiceCredential.Initializer initializer)
Constructs a new service account credential using the given initializer.
Parameter | |
---|---|
Name | Description |
initializer | ServiceCredential.Initializer |
Fields
Logger
protected static readonly ILogger Logger
Logger for this class
Field Value | |
---|---|
Type | Description |
ILogger |
Properties
AccessMethod
public IAccessMethod AccessMethod { get; }
Gets the method for presenting the access token to the resource server.
Property Value | |
---|---|
Type | Description |
IAccessMethod |
Clock
public IClock Clock { get; }
Gets the clock used to refresh the token if it expires.
Property Value | |
---|---|
Type | Description |
IClock |
HttpClient
public ConfigurableHttpClient HttpClient { get; }
Gets the HTTP client used to make authentication requests to the server.
Property Value | |
---|---|
Type | Description |
ConfigurableHttpClient |
QuotaProject
public string QuotaProject { get; }
The ID of the project associated to this credential for the purposes of quota calculation and billing. May be null.
Property Value | |
---|---|
Type | Description |
String |
Token
public TokenResponse Token { get; set; }
Gets the token response which contains the access token.
Property Value | |
---|---|
Type | Description |
TokenResponse |
TokenServerUrl
public string TokenServerUrl { get; }
Gets the token server URL.
Property Value | |
---|---|
Type | Description |
String |
Methods
GetAccessTokenForRequestAsync(String, CancellationToken)
public virtual Task<string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = null)
Gets an access token to authorize a request. If the existing token expires soon, try to refresh it first. GetAccessTokenForRequestAsync(String, CancellationToken)
Parameters | |
---|---|
Name | Description |
authUri | String |
cancellationToken | CancellationToken |
Returns | |
---|---|
Type | Description |
Task<String> |
GetAccessTokenWithHeadersForRequestAsync(String, CancellationToken)
public Task<AccessTokenWithHeaders> GetAccessTokenWithHeadersForRequestAsync(string authUri = null, CancellationToken cancellationToken = null)
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 Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
Decorates unsuccessful responses, returns true if the response gets modified. See IHttpUnsuccessfulResponseHandler for more information.
Parameter | |
---|---|
Name | Description |
args | HandleUnsuccessfulResponseArgs |
Returns | |
---|---|
Type | Description |
Task<Boolean> |
Initialize(ConfigurableHttpClient)
public void Initialize(ConfigurableHttpClient httpClient)
Initializes a HTTP client after it was created.
Parameter | |
---|---|
Name | Description |
httpClient | ConfigurableHttpClient |
InterceptAsync(HttpRequestMessage, CancellationToken)
public Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Parameters | |
---|---|
Name | Description |
request | HttpRequestMessage |
cancellationToken | CancellationToken |
Returns | |
---|---|
Type | Description |
Task |
RequestAccessTokenAsync(CancellationToken)
public abstract Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)
Requests a new token.
Parameter | |
---|---|
Name | Description |
taskCancellationToken | CancellationToken Cancellation token to cancel operation. |
Returns | |
---|---|
Type | Description |
Task<Boolean> |
|