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')
...
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.
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.
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 …
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With