Documentation ¶
Overview ¶
Package google provides support for making OAuth2 authorized and authenticated HTTP requests to Google APIs. It supports the Web server flow, client-side credentials, service accounts, Google Compute Engine service accounts, and Google App Engine service accounts.
For more information, please read https://developers.google.com/accounts/docs/OAuth2 and https://developers.google.com/accounts/docs/application-default-credentials.
Index ¶
- Constants
- Variables
- func AppEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource
- func ComputeTokenSource(account string) oauth2.TokenSource
- func ConfigFromJSON(jsonKey []byte, scope ...string) (*oauth2.Config, error)
- func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error)
- func DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error)
- func JWTConfigFromJSON(jsonKey []byte, scope ...string) (*jwt.Config, error)
- type SDKConfig
Constants ¶
const JWTTokenURL = "https://accounts.google.com/o/oauth2/token"
JWTTokenURL is Google's OAuth 2.0 token URL to use with the JWT flow.
Variables ¶
var Endpoint = oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
Endpoint is Google's OAuth 2.0 endpoint.
Functions ¶
func AppEngineTokenSource ¶
func AppEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource
AppEngineTokenSource returns a token source that fetches tokens issued to the current App Engine application's service account. If you are implementing a 3-legged OAuth 2.0 flow on App Engine that involves user accounts, see oauth2.Config instead.
The provided context must have come from appengine.NewContext.
func ComputeTokenSource ¶
func ComputeTokenSource(account string) oauth2.TokenSource
ComputeTokenSource returns a token source that fetches access tokens from Google Compute Engine (GCE)'s metadata server. It's only valid to use this token source if your program is running on a GCE instance. If no account is specified, "default" is used. Further information about retrieving access tokens from the GCE metadata server can be found at https://cloud.google.com/compute/docs/authentication.
func ConfigFromJSON ¶ added in v1.0.0
ConfigFromJSON uses a Google Developers Console client_credentials.json file to construct a config. client_credentials.json can be downloadable from https://console.developers.google.com, under "APIs & Auth" > "Credentials". Download the Web application credentials in the JSON format and provide the contents of the file as jsonKey.
func DefaultClient ¶ added in v1.0.0
DefaultClient returns an HTTP Client that uses the DefaultTokenSource to obtain authentication credentials.
This client should be used when developing services that run on Google App Engine or Google Compute Engine and use "Application Default Credentials."
For more details, see: https://developers.google.com/accounts/docs/application-default-credentials
func DefaultTokenSource ¶ added in v1.0.0
DefaultTokenSource is a token source that uses "Application Default Credentials".
It looks for credentials in the following places, preferring the first location found:
- A JSON file whose path is specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable.
- A JSON file in a location known to the gcloud command-line tool. On Windows, this is %APPDATA%/gcloud/application_default_credentials.json. On other systems, $HOME/.config/gcloud/application_default_credentials.json.
- On Google App Engine it uses the appengine.AccessToken function.
- On Google Compute Engine, it fetches credentials from the metadata server. (In this final case any provided scopes are ignored.)
For more details, see: https://developers.google.com/accounts/docs/application-default-credentials
func JWTConfigFromJSON ¶
JWTConfigFromJSON uses a Google Developers service account JSON key file to read the credentials that authorize and authenticate the requests. Create a service account on "Credentials" page under "APIs & Auth" for your project at https://console.developers.google.com to download a JSON key file.
Types ¶
type SDKConfig ¶ added in v1.0.0
type SDKConfig struct {
// contains filtered or unexported fields
}
An SDKConfig provides access to tokens from an account already authorized via the Google Cloud SDK.
func NewSDKConfig ¶ added in v1.0.0
NewSDKConfig creates an SDKConfig for the given Google Cloud SDK account. If account is empty, the account currently active in Google Cloud SDK properties is used. Google Cloud SDK credentials must be created by running `gcloud auth` before using this function. The Google Cloud SDK is available at https://cloud.google.com/sdk/.
func (*SDKConfig) Client ¶ added in v1.0.0
Client returns an HTTP client using Google Cloud SDK credentials to authorize requests. The token will auto-refresh as necessary. The underlying http.RoundTripper will be obtained using the provided context. The returned client and its Transport should not be modified.
func (*SDKConfig) Scopes ¶ added in v1.0.0
Scopes are the OAuth 2.0 scopes the current account is authorized for.
func (*SDKConfig) TokenSource ¶ added in v1.0.0
func (c *SDKConfig) TokenSource(ctx context.Context) oauth2.TokenSource
TokenSource returns an oauth2.TokenSource that retrieve tokens from Google Cloud SDK credentials using the provided context. It will returns the current access token stored in the credentials, and refresh it when it expires, but it won't update the credentials with the new access token.