Copyright 2015 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package google

import (
	
	
	
	
	
	
	
	

	
	
)
Credentials holds Google credentials, including "Application Default Credentials". For more details, see: https://developers.google.com/accounts/docs/application-default-credentials
type Credentials struct {
	ProjectID   string // may be empty
	TokenSource oauth2.TokenSource
JSON contains the raw bytes from a JSON credentials file. This field may be nil if authentication is provided by the environment and not with a credentials file, e.g. when code is running on Google Cloud Platform.
	JSON []byte
}
DefaultCredentials is the old name of Credentials. Deprecated: use Credentials instead.
DefaultClient returns an HTTP Client that uses the DefaultTokenSource to obtain authentication credentials.
func ( context.Context,  ...string) (*http.Client, error) {
	,  := DefaultTokenSource(, ...)
	if  != nil {
		return nil, 
	}
	return oauth2.NewClient(, ), nil
}
DefaultTokenSource returns the token source for "Application Default Credentials". It is a shortcut for FindDefaultCredentials(ctx, scope).TokenSource.
func ( context.Context,  ...string) (oauth2.TokenSource, error) {
	,  := FindDefaultCredentials(, ...)
	if  != nil {
		return nil, 
	}
	return .TokenSource, nil
}
FindDefaultCredentials searches for "Application Default Credentials". It looks for credentials in the following places, preferring the first location found: 1. A JSON file whose path is specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable. 2. 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. 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses the appengine.AccessToken function. 4. On Google Compute Engine, Google App Engine standard second generation runtimes (>= Go 1.11), and Google App Engine flexible environment, it fetches credentials from the metadata server.
First, try the environment variable.
	const  = "GOOGLE_APPLICATION_CREDENTIALS"
	if  := os.Getenv();  != "" {
		,  := readCredentialsFile(, , )
		if  != nil {
			return nil, fmt.Errorf("google: error getting credentials using %v environment variable: %v", , )
		}
		return , nil
	}
Second, try a well-known file.
	 := wellKnownFile()
	if ,  := readCredentialsFile(, , );  == nil {
		return , nil
	} else if !os.IsNotExist() {
		return nil, fmt.Errorf("google: error getting credentials using well-known file (%v): %v", , )
	}
Third, if we're on a Google App Engine standard first generation runtime (<= Go 1.9) use those credentials. App Engine standard second generation runtimes (>= Go 1.11) and App Engine flexible use ComputeTokenSource and the metadata server.
Fourth, if we're on Google Compute Engine, an App Engine standard second generation runtime, or App Engine flexible, use the metadata server.
	if metadata.OnGCE() {
		,  := metadata.ProjectID()
		return &DefaultCredentials{
			ProjectID:   ,
			TokenSource: ComputeTokenSource("", ...),
		}, nil
	}
None are found; return helpful error.
	const  = "https://developers.google.com/accounts/docs/application-default-credentials"
	return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", )
}
CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can represent either a Google Developers Console client_credentials.json file (as in ConfigFromJSON) or a Google Developers service account key file (as in JWTConfigFromJSON).
func ( context.Context,  []byte,  ...string) (*Credentials, error) {
	var  credentialsFile
	if  := json.Unmarshal(, &);  != nil {
		return nil, 
	}
	,  := .tokenSource(, append([]string(nil), ...))
	if  != nil {
		return nil, 
	}
	return &DefaultCredentials{
		ProjectID:   .ProjectID,
		TokenSource: ,
		JSON:        ,
	}, nil
}

func () string {
	const  = "application_default_credentials.json"
	if runtime.GOOS == "windows" {
		return filepath.Join(os.Getenv("APPDATA"), "gcloud", )
	}
	return filepath.Join(guessUnixHomeDir(), ".config", "gcloud", )
}

func ( context.Context,  string,  []string) (*DefaultCredentials, error) {
	,  := ioutil.ReadFile()
	if  != nil {
		return nil, 
	}
	return CredentialsFromJSON(, , ...)