Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to bigquery from golang using json keyfile

So I have a json keyfile that looks like this:

{
  "user_agent": null,
  "_scopes": "https://www.googleapis.com/auth/bigquery",
  "token_uri": "https://www.googleapis.com/oauth2/v4/token",
  "refresh_token": null,
  "_service_account_email": "...",
  "assertion_type": null,
  "_kwargs": {},
  "revoke_uri": "https://accounts.google.com/o/oauth2/revoke",
  "_private_key_pkcs8_pem": "-----BEGIN PRIVATE KEY----- ..."
  ...
}

I want to login using this file to bigquery, with Golang. I looked through the examples at https://github.com/GoogleCloudPlatform/google-cloud-go but couldn't find anything related there how to create a new bigquery client using the keyfile. Am I missing something obvious?

In python the quivalent is:

        credentials = ServiceAccountCredentials.from_json_keyfile_name(
                'keyfile.json',
                'https://www.googleapis.com/auth/bigquery')

        ...
like image 917
Max Avatar asked Sep 14 '17 07:09

Max


People also ask

How do I log into BigQuery?

Find BigQuery in the left side menu of the Google Cloud Platform Console, under Big Data. Open your project in the console. If you're new to the console, you may need to sign up for a Google account, access the console, and create a project. Find BigQuery in the left side menu of the console, under Big Data.

How do I access BigQuery with a service account?

Service Account based Authentication Click on Create. Now you should see an option to assign Service Account permissions. Under that you should find a drop down. Choose BigQuery-> BigQuery Admin.


1 Answers

First: the file I posted is the wrong keyfile, for more details, check this answer: ('Unexpected credentials type', None, 'Expected', 'service_account') with oauth2client (Python)

Second: this is the code that worked for me (listing the available datasets):

package main

import (
    "cloud.google.com/go/bigquery"
    "fmt"
    "golang.org/x/net/context"
    "google.golang.org/api/iterator"
    "google.golang.org/api/option"
)

func main() {
    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, "project-name", option.WithCredentialsFile("keyfile.json"))

    if err != nil {
        panic(err.Error())
    }

    it := client.Datasets(ctx)
    for {
        dataset, err := it.Next()
        if err == iterator.Done {
            break
        }
        fmt.Println(dataset.DatasetID)
    }

    println("logged in")
}

That took forever to find out today …

like image 140
Max Avatar answered Oct 19 '22 17:10

Max