diff --git a/bigquery/bigquery.go b/bigquery/bigquery.go index 28ea5446f1f..308bb36ccf5 100644 --- a/bigquery/bigquery.go +++ b/bigquery/bigquery.go @@ -16,7 +16,6 @@ package bigquery import ( "context" - "errors" "fmt" "io" "net/http" @@ -25,12 +24,12 @@ import ( "time" "cloud.google.com/go/internal" + "cloud.google.com/go/internal/detect" "cloud.google.com/go/internal/version" gax "github.com/googleapis/gax-go/v2" bq "google.golang.org/api/bigquery/v2" "google.golang.org/api/googleapi" "google.golang.org/api/option" - "google.golang.org/api/transport" ) const ( @@ -83,11 +82,10 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio return nil, fmt.Errorf("bigquery: constructing client: %v", err) } - if projectID == DetectProjectID { - projectID, err = detectProjectID(ctx, opts...) - if err != nil { - return nil, fmt.Errorf("failed to detect project: %v", err) - } + // Handle project autodetection. + projectID, err = detect.ProjectID(ctx, projectID, "", opts...) + if err != nil { + return nil, err } c := &Client{ @@ -110,17 +108,6 @@ func (c *Client) Close() error { return nil } -func detectProjectID(ctx context.Context, opts ...option.ClientOption) (string, error) { - creds, err := transport.Creds(ctx, opts...) - if err != nil { - return "", fmt.Errorf("fetching creds: %v", err) - } - if creds.ProjectID == "" { - return "", errors.New("credentials did not provide a valid ProjectID") - } - return creds.ProjectID, nil -} - // Calls the Jobs.Insert RPC and returns a Job. func (c *Client) insertJob(ctx context.Context, job *bq.Job, media io.Reader) (*Job, error) { call := c.bqs.Jobs.Insert(c.projectID, job).Context(ctx)