Show / Hide Table of Contents

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
System.Object
ServiceCredential
ComputeCredential
ServiceAccountCredential
Implements
ICredential
Google.Apis.Http.IConfigurableHttpClientInitializer
ITokenAccess
Google.Apis.Http.IHttpExecuteInterceptor
Google.Apis.Http.IHttpUnsuccessfulResponseHandler
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Google.Apis.Auth.OAuth2
Assembly: Google.Apis.Auth.dll
Syntax
public abstract class ServiceCredential : ICredential, IConfigurableHttpClientInitializer, 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

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>

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>

true if a new token was received successfully.

Implements

ICredential
Google.Apis.Http.IConfigurableHttpClientInitializer
ITokenAccess
Google.Apis.Http.IHttpExecuteInterceptor
Google.Apis.Http.IHttpUnsuccessfulResponseHandler
Back to top