public class GoogleCredential : object, ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IOidcTokenProvider
Credential for authorizing calls using OAuth 2.0. It is a convenience wrapper that allows handling of different types of credentials (like ServiceAccountCredential, ComputeCredential or UserCredential) in a unified way.
See GetApplicationDefaultAsync(CancellationToken) for the credential retrieval logic.
Implements
ICredential, IConfigurableHttpClientInitializer, ITokenAccessWithHeaders, ITokenAccess, IOidcTokenProviderNamespace
Google.Apis.Auth.OAuth2Assembly
Google.Apis.Auth.dll
Properties
IsCreateScopedRequired
public virtual bool IsCreateScopedRequired { get; }
Returns true
only if this credential type has no scopes by default and requires
a call to CreateScoped(IEnumerable<String>) before use.
Credentials need to have scopes in them before they can be used to access Google services. Some Credential types have scopes built-in, and some don't. This property indicates whether the Credential type has scopes built-in.
- ComputeCredential has scopes built-in. Nothing additional is required.
- UserCredential has scopes built-in, as they were obtained during the consent screen. Nothing additional is required.
- ServiceAccountCredential does not have scopes built-in by default. Caller should invoke CreateScoped(IEnumerable<String>) to add scopes to the credential.
Property Value | |
---|---|
Type | Description |
Boolean |
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 |
UnderlyingCredential
public ICredential UnderlyingCredential { get; }
Gets the underlying credential instance being wrapped.
Property Value | |
---|---|
Type | Description |
ICredential |
Methods
CreateScoped(IEnumerable<String>)
public virtual GoogleCredential CreateScoped(IEnumerable<string> scopes)
If the credential supports scopes, creates a copy with the specified scopes. Otherwise, it returns the same instance.
Parameter | |
---|---|
Name | Description |
scopes | IEnumerable<String> |
Returns | |
---|---|
Type | Description |
GoogleCredential |
CreateScoped(String[])
public GoogleCredential CreateScoped(params string[] scopes)
If the credential supports scopes, creates a copy with the specified scopes. Otherwise, it returns the same instance.
Parameter | |
---|---|
Name | Description |
scopes | String[] |
Returns | |
---|---|
Type | Description |
GoogleCredential |
CreateWithQuotaProject(String)
public virtual GoogleCredential CreateWithQuotaProject(string quotaProject)
Creates a copy of this credential with the specified quota project.
Parameter | |
---|---|
Name | Description |
quotaProject | String The quota project to use for the copy. May be null. |
Returns | |
---|---|
Type | Description |
GoogleCredential | A copy of this credential with QuotaProject set to |
CreateWithUser(String)
public virtual GoogleCredential CreateWithUser(string user)
If the credential supports setting the user, creates a copy with the specified user. Otherwise, it throws . Only Service Credentials support this operation.
Parameter | |
---|---|
Name | Description |
user | String The user to set in the returned credential. |
Returns | |
---|---|
Type | Description |
GoogleCredential | This credential with the user set to |
FromAccessToken(String, IAccessMethod)
public static GoogleCredential FromAccessToken(string accessToken, IAccessMethod accessMethod = null)
Create a GoogleCredential directly from the provided access token. The access token will not be automatically refreshed.
Parameters | |
---|---|
Name | Description |
accessToken | String The access token to use within this credential. |
accessMethod | IAccessMethod Optional. The IAccessMethod to use within this credential.
If |
Returns | |
---|---|
Type | Description |
GoogleCredential | A credential based on the provided access token. |
FromComputeCredential(ComputeCredential)
public static GoogleCredential FromComputeCredential(ComputeCredential computeCredential = null)
Create a GoogleCredential from a ComputeCredential. In general, do not use this method. Call GetApplicationDefault() or GetApplicationDefaultAsync(CancellationToken), which will provide the most suitable credentials for the current platform.
Parameter | |
---|---|
Name | Description |
computeCredential | ComputeCredential Optional. The compute credential to use in the returned GoogleCredential.
If |
Returns | |
---|---|
Type | Description |
GoogleCredential | A GoogleCredential with an underlying ComputeCredential. |
FromFile(String)
public static GoogleCredential FromFile(string path)
Loads credential from the specified file containing JSON credential data.
The file can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
Parameter | |
---|---|
Name | Description |
path | String The path to the credential file. |
Returns | |
---|---|
Type | Description |
GoogleCredential | The loaded credentials. |
FromFileAsync(String, CancellationToken)
public static Task<GoogleCredential> FromFileAsync(string path, CancellationToken cancellationToken)
Loads credential from the specified file containing JSON credential data.
The file can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
Parameters | |
---|---|
Name | Description |
path | String The path to the credential file. |
cancellationToken | CancellationToken Cancellation token for the operation. |
Returns | |
---|---|
Type | Description |
Task<GoogleCredential> | The loaded credentials. |
FromJson(String)
public static GoogleCredential FromJson(string json)
Loads credential from a string containing JSON credential data.
The string can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
Parameter | |
---|---|
Name | Description |
json | String |
Returns | |
---|---|
Type | Description |
GoogleCredential |
FromServiceAccountCredential(ServiceAccountCredential)
public static GoogleCredential FromServiceAccountCredential(ServiceAccountCredential credential)
Creates a GoogleCredential
wrapping a ServiceAccountCredential.
Parameter | |
---|---|
Name | Description |
credential | ServiceAccountCredential |
Returns | |
---|---|
Type | Description |
GoogleCredential |
FromStream(Stream)
public static GoogleCredential FromStream(Stream stream)
Loads credential from stream containing JSON credential data.
The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
Parameter | |
---|---|
Name | Description |
stream | Stream |
Returns | |
---|---|
Type | Description |
GoogleCredential |
FromStreamAsync(Stream, CancellationToken)
public static Task<GoogleCredential> FromStreamAsync(Stream stream, CancellationToken cancellationToken)
Loads credential from stream containing JSON credential data.
The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
Parameters | |
---|---|
Name | Description |
stream | Stream |
cancellationToken | CancellationToken |
Returns | |
---|---|
Type | Description |
Task<GoogleCredential> |
GetApplicationDefault()
public static GoogleCredential GetApplicationDefault()
Synchronously returns the Application Default Credentials which are ambient credentials that identify and authorize the whole application. See GetApplicationDefaultAsync(CancellationToken) for details on application default credentials.
This method will block until the credentials are available (or an exception is thrown). It is highly preferable to call GetApplicationDefaultAsync(CancellationToken) where possible.
Returns | |
---|---|
Type | Description |
GoogleCredential | The application default credentials. |
GetApplicationDefaultAsync()
public static Task<GoogleCredential> GetApplicationDefaultAsync()
Returns the Application Default Credentials which are ambient credentials that identify and authorize the whole application. See GetApplicationDefaultAsync(CancellationToken) for more details.
Returns | |
---|---|
Type | Description |
Task<GoogleCredential> | A task which completes with the application default credentials. |
GetApplicationDefaultAsync(CancellationToken)
public static Task<GoogleCredential> GetApplicationDefaultAsync(CancellationToken cancellationToken)
Returns the Application Default Credentials which are ambient credentials that identify and authorize the whole application.
The ambient credentials are determined as following order:
- The environment variable GOOGLE_APPLICATION_CREDENTIALS is checked. If this variable is specified, it should point to a file that defines the credentials. The simplest way to get a credential for this purpose is to create a service account using the Google Developers Console in the section APIs & Auth, in the sub-section Credentials. Create a service account or choose an existing one and select Generate new JSON key. Set the environment variable to the path of the JSON file downloaded.
- If you have installed the Google Cloud SDK on your machine and have run the command GCloud Auth Login, your identity can be used as a proxy to test code calling APIs from that machine.
- If you are running in Google Compute Engine production, the built-in service account associated with the virtual machine instance will be used.
-
If all previous steps have failed,
InvalidOperationException
is thrown.
Parameter | |
---|---|
Name | Description |
cancellationToken | CancellationToken Cancellation token for the operation. |
Returns | |
---|---|
Type | Description |
Task<GoogleCredential> | A task which completes with the application default credentials. |
If the cancellation token is cancelled while the underlying operation is loading Application Default Credentials, the underlying operation will still be used for any further requests. No actual work is cancelled via this cancellation token; it just allows the returned task to transition to a cancelled state.
GetOidcTokenAsync(OidcTokenOptions, CancellationToken)
public Task<OidcToken> GetOidcTokenAsync(OidcTokenOptions options, CancellationToken cancellationToken = null)
Returns an OIDC token for the given options.
Parameters | |
---|---|
Name | Description |
options | OidcTokenOptions The options to create the token from. |
cancellationToken | CancellationToken The cancellation token that may be used to cancel the request. |
Returns | |
---|---|
Type | Description |
Task<OidcToken> | The OIDC token. |
Explicit Interface Implementations
ITokenAccess.GetAccessTokenForRequestAsync(String, CancellationToken)
Task<string> ITokenAccess.GetAccessTokenForRequestAsync(string authUri, CancellationToken cancellationToken)
Parameters | |
---|---|
Name | Description |
authUri | String |
cancellationToken | CancellationToken |
Returns | |
---|---|
Type | Description |
Task<String> |
ITokenAccessWithHeaders.GetAccessTokenWithHeadersForRequestAsync(String, CancellationToken)
Task<AccessTokenWithHeaders> ITokenAccessWithHeaders.GetAccessTokenWithHeadersForRequestAsync(string authUri, CancellationToken cancellationToken)
Parameters | |
---|---|
Name | Description |
authUri | String |
cancellationToken | CancellationToken |
Returns | |
---|---|
Type | Description |
Task<AccessTokenWithHeaders> |
IConfigurableHttpClientInitializer.Initialize(ConfigurableHttpClient)
void IConfigurableHttpClientInitializer.Initialize(ConfigurableHttpClient httpClient)
Parameter | |
---|---|
Name | Description |
httpClient | ConfigurableHttpClient |