Class ServiceCredential
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.
Inheritance
Implements
Inherited Members
Namespace: Google.Apis.Auth.OAuth2
Assembly: Google.Apis.Auth.dll
Syntax
public abstract class ServiceCredential : ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IHttpExecuteInterceptor, IHttpUnsuccessfulResponseHandler
Constructors
ServiceCredential(ServiceCredential.Initializer)
Constructs a new service account credential using the given initializer.
Declaration
public ServiceCredential(ServiceCredential.Initializer initializer)
Parameters
| Type | Name | Description |
|---|---|---|
| ServiceCredential.Initializer | initializer |
Fields
Logger
Logger for this class
Declaration
protected static readonly ILogger Logger
Field Value
| Type | Description |
|---|---|
| Google.Apis.Logging.ILogger |
Properties
AccessMethod
Gets the method for presenting the access token to the resource server.
Declaration
public IAccessMethod AccessMethod { get; }
Property Value
| Type | Description |
|---|---|
| IAccessMethod |
Clock
Gets the clock used to refresh the token if it expires.
Declaration
public IClock Clock { get; }
Property Value
| Type | Description |
|---|---|
| Google.Apis.Util.IClock |
HttpClient
Gets the HTTP client used to make authentication requests to the server.
Declaration
public ConfigurableHttpClient HttpClient { get; }
Property Value
| Type | Description |
|---|---|
| Google.Apis.Http.ConfigurableHttpClient |
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 |
|---|---|
| System.String |
Token
Gets the token response which contains the access token.
Declaration
public TokenResponse Token { get; set; }
Property Value
| Type | Description |
|---|---|
| TokenResponse |
TokenServerUrl
Gets the token server URL.
Declaration
public string TokenServerUrl { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
Methods
GetAccessTokenForRequestAsync(String, CancellationToken)
Gets an access token to authorize a request. If the existing token expires soon, try to refresh it first. GetAccessTokenForRequestAsync(String, CancellationToken)
Declaration
public virtual Task<string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | authUri | |
| System.Threading.CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.String> |
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(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<AccessTokenWithHeaders> | The access token with headers if any. |
HandleResponseAsync(HandleUnsuccessfulResponseArgs)
Decorates unsuccessful responses, returns true if the response gets modified. See IHttpUnsuccessfulResponseHandler for more information.
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)
Declaration
public Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Net.Http.HttpRequestMessage | request | |
| System.Threading.CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
RequestAccessTokenAsync(CancellationToken)
Requests a new token.
Declaration
public abstract Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Threading.CancellationToken | taskCancellationToken | Cancellation token to cancel operation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Boolean> |
|