英文:
Login to bigquery from golang using json keyfile
问题
我有一个看起来像这样的JSON密钥文件:
{
"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----- ..."
...
}
我想使用这个文件登录到BigQuery,并使用Golang。我查看了https://github.com/GoogleCloudPlatform/google-cloud-go上的示例,但没有找到如何使用密钥文件创建新的BigQuery客户端的相关内容。我是否遗漏了一些明显的东西?
在Python中,相当于以下代码:
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'keyfile.json',
'https://www.googleapis.com/auth/bigquery')
...
英文:
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')
...
答案1
得分: 9
首先:我发布的文件是错误的密钥文件,更多详细信息请查看这个答案:https://stackoverflow.com/questions/38709324/unexpected-credentials-type-none-expected-service-account-with-oauth2/41494815#41494815
其次:以下是对我有效的代码(列出可用的数据集):
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")
}
今天找到这个解决方法花了很长时间...
英文:
First: the file I posted is the wrong keyfile, for more details, check this answer: https://stackoverflow.com/questions/38709324/unexpected-credentials-type-none-expected-service-account-with-oauth2/41494815#41494815
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 …
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论