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.
Service
External
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(Initializer)
Constructs a new service account credential using the given initializer.
Declaration
public ServiceCredential(ServiceCredential.Initializer initializer)
Parameters
Type | Name | Description |
---|---|---|
Service |
initializer |
Fields
Logger
Logger for this class
Declaration
protected static readonly ILogger Logger
Field Value
Type | Description |
---|---|
ILogger |
Properties
AccessMethod
Gets the method for presenting the access token to the resource server.
Declaration
public IAccessMethod AccessMethod { get; }
Property Value
Type | Description |
---|---|
IAccess |
Clock
Gets the clock used to refresh the token if it expires.
Declaration
public IClock Clock { get; }
Property Value
Type | Description |
---|---|
IClock |
HttpClient
Gets the HTTP client used to make authentication requests to the server.
Declaration
public ConfigurableHttpClient HttpClient { get; }
Property Value
Type | Description |
---|---|
Configurable |
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 |
---|---|
string |
Scopes
Scopes to request during the authorization grant. May be null or empty.
Declaration
public IEnumerable<string> Scopes { get; set; }
Property Value
Type | Description |
---|---|
IEnumerable<string> |
Remarks
If the scopes are pre-granted through the environment, like in GCE where scopes are granted to the VM, scopes set here will be ignored.
Token
Gets the token response which contains the access token.
Declaration
public TokenResponse Token { get; set; }
Property Value
Type | Description |
---|---|
Token |
TokenServerUrl
Gets the token server URL.
Declaration
public string TokenServerUrl { get; }
Property Value
Type | Description |
---|---|
string |
Remarks
May be null for credential types that resolve token endpoints just before obtaining an access token.
This is the case for Impersonated
Methods
BuildCreateHttpClientArgs()
Builds HTTP client creation args from all this credential settings.
These are used for initializing Http
Declaration
protected CreateHttpClientArgs BuildCreateHttpClientArgs()
Returns
Type | Description |
---|---|
Create |
GetAccessTokenForRequestAsync(string, CancellationToken)
Gets an access token to authorize a request. If the existing token expires soon, try to refresh it first.
Get
Declaration
public virtual Task<string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
string | authUri | |
Cancellation |
cancellationToken |
Returns
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)
Parameters
Type | Name | Description |
---|---|---|
string | authUri | The URI the returned token will grant access to. |
Cancellation |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
Task<Access |
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 |
---|---|---|
Handle |
args |
Returns
Initialize(ConfigurableHttpClient)
Initializes a HTTP client after it was created.
Declaration
public void Initialize(ConfigurableHttpClient httpClient)
Parameters
Type | Name | Description |
---|---|---|
Configurable |
httpClient |
InterceptAsync(HttpRequestMessage, CancellationToken)
Declaration
public Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
Http |
request | The HTTP request message. |
Cancellation |
cancellationToken | Cancellation token to cancel the operation. |
Returns
Type | Description |
---|---|
Task |
RequestAccessTokenAsync(CancellationToken)
Requests a new token.
Declaration
public abstract Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)
Parameters
Type | Name | Description |
---|---|---|
Cancellation |
taskCancellationToken | Cancellation token to cancel operation. |