Login to bigquery from golang using json keyfile

huangapple go评论128阅读模式
英文:

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:

  1. {
  2. "user_agent": null,
  3. "_scopes": "https://www.googleapis.com/auth/bigquery",
  4. "token_uri": "https://www.googleapis.com/oauth2/v4/token",
  5. "refresh_token": null,
  6. "_service_account_email": "...",
  7. "assertion_type": null,
  8. "_kwargs": {},
  9. "revoke_uri": "https://accounts.google.com/o/oauth2/revoke",
  10. "_private_key_pkcs8_pem": "-----BEGIN PRIVATE KEY----- ..."
  11. ...
  12. }

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:

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

答案1

得分: 9

首先:我发布的文件是错误的密钥文件,更多详细信息请查看这个答案:https://stackoverflow.com/questions/38709324/unexpected-credentials-type-none-expected-service-account-with-oauth2/41494815#41494815

其次:以下是对我有效的代码(列出可用的数据集):

  1. package main
  2. import (
  3. "cloud.google.com/go/bigquery"
  4. "fmt"
  5. "golang.org/x/net/context"
  6. "google.golang.org/api/iterator"
  7. "google.golang.org/api/option"
  8. )
  9. func main() {
  10. ctx := context.Background()
  11. client, err := bigquery.NewClient(ctx, "project-name", option.WithCredentialsFile("keyfile.json"))
  12. if err != nil {
  13. panic(err.Error())
  14. }
  15. it := client.Datasets(ctx)
  16. for {
  17. dataset, err := it.Next()
  18. if err == iterator.Done {
  19. break
  20. }
  21. fmt.Println(dataset.DatasetID)
  22. }
  23. println("logged in")
  24. }

今天找到这个解决方法花了很长时间...

英文:

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):

  1. package main
  2. import (
  3. "cloud.google.com/go/bigquery"
  4. "fmt"
  5. "golang.org/x/net/context"
  6. "google.golang.org/api/iterator"
  7. "google.golang.org/api/option"
  8. )
  9. func main() {
  10. ctx := context.Background()
  11. client, err := bigquery.NewClient(ctx, "project-name", option.WithCredentialsFile("keyfile.json"))
  12. if err != nil {
  13. panic(err.Error())
  14. }
  15. it := client.Datasets(ctx)
  16. for {
  17. dataset, err := it.Next()
  18. if err == iterator.Done {
  19. break
  20. }
  21. fmt.Println(dataset.DatasetID)
  22. }
  23. println("logged in")
  24. }

That took forever to find out today …

huangapple
  • 本文由 发表于 2017年9月14日 15:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/46213370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定