ACCESS_TOKEN_SCOPE_INSUFFICIENT when calling google.apps.sheets.v4.SpreadsheetsService.GetValues in Golang

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

ACCESS_TOKEN_SCOPE_INSUFFICIENT when calling google.apps.sheets.v4.SpreadsheetsService.GetValues in Golang

问题

我正在尝试运行一个简单的Go程序,调用Google Sheets API:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/api/sheets/v4"
  6. )
  7. func main() {
  8. ctx := context.Background()
  9. srv, err := sheets.NewService(ctx)
  10. if err != nil {
  11. panic(err)
  12. }
  13. resp, err := srv.Spreadsheets.Values.Get("1HG_cmqALkquc0ud8Pm7GKOhR5IpPSj20q_fFkHBaM5M", "A1").Do()
  14. if err != nil {
  15. panic(err)
  16. }
  17. fmt.Println(fmt.Sprintf("%+v", resp.Values))
  18. }

但是当我尝试运行它时,我得到了以下错误:

  1. $ rm /home/johan/.config/gcloud/application_default_credentials.json
  2. $ gcloud auth application-default login
  3. Your browser has been opened to visit:
  4. https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fsqlservice.login+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=W...s&access_type=offline&code_challenge=q...4&code_challenge_method=S256
  5. Credentials saved to file: [/home/johan/.config/gcloud/application_default_credentials.json]
  6. These credentials will be used by any library that requests Application Default Credentials (ADC).
  7. Quota project "questions-279902" was added to ADC which can be used by Google client libraries for billing and quota. Note that some services may still bill the project owning the resource.
  8. $ go run sheets_api.go
  9. panic: googleapi: Error 403: Request had insufficient authentication scopes.
  10. Details:
  11. [
  12. {
  13. "@type": "type.googleapis.com/google.rpc.ErrorInfo",
  14. "domain": "googleapis.com",
  15. "metadata": {
  16. "method": "google.apps.sheets.v4.SpreadsheetsService.GetValues",
  17. "service": "sheets.googleapis.com"
  18. },
  19. "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
  20. }
  21. ]
  22. More details:
  23. Reason: insufficientPermissions, Message: Insufficient Permission
  24. goroutine 1 [running]:
  25. main.main()
  26. /home/johan/20q/cmd/labelserver/sheets_api.go:20 +0x194
  27. exit status 2

我创建了这个Google表格,所以我的用户应该有访问权限。

我在这里做错了什么?

英文:

I am trying to run a simple go program calling the Google Sheets API:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/api/sheets/v4"
  6. )
  7. func main() {
  8. ctx := context.Background()
  9. srv, err := sheets.NewService(ctx)
  10. if err != nil {
  11. panic(err)
  12. }
  13. resp, err := srv.Spreadsheets.Values.Get("1HG_cmqALkquc0ud8Pm7GKOhR5IpPSj20q_fFkHBaM5M", "A1").Do()
  14. if err != nil {
  15. panic(err)
  16. }
  17. fmt.Println(fmt.Sprintf("%+v", resp.Values))
  18. }

But when I try to run it, I get:

  1. $ rm /home/johan/.config/gcloud/application_default_credentials.json
  2. $ gcloud auth application-default login
  3. Your browser has been opened to visit:
  4. https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fsqlservice.login+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=W...s&access_type=offline&code_challenge=q...4&code_challenge_method=S256
  5. Credentials saved to file: [/home/johan/.config/gcloud/application_default_credentials.json]
  6. These credentials will be used by any library that requests Application Default Credentials (ADC).
  7. Quota project "questions-279902" was added to ADC which can be used by Google client libraries for billing and quota. Note that some services may still bill the project owning the resource.
  8. $ go run sheets_api.go
  9. panic: googleapi: Error 403: Request had insufficient authentication scopes.
  10. Details:
  11. [
  12. {
  13. "@type": "type.googleapis.com/google.rpc.ErrorInfo",
  14. "domain": "googleapis.com",
  15. "metadata": {
  16. "method": "google.apps.sheets.v4.SpreadsheetsService.GetValues",
  17. "service": "sheets.googleapis.com"
  18. },
  19. "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
  20. }
  21. ]
  22. More details:
  23. Reason: insufficientPermissions, Message: Insufficient Permission
  24. goroutine 1 [running]:
  25. main.main()
  26. /home/johan/20q/cmd/labelserver/sheets_api.go:20 +0x194
  27. exit status 2

I created this Google Sheet, so my user should have access to it.

What am I doing wrong here?

答案1

得分: 1

你可以在Go中这样限定你的令牌:

  1. ...
  2. func main() {
  3. ctx := context.Background()
  4. cred, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/spreadsheets.readonly")
  5. srv, err := sheets.NewService(ctx, option.WithCredentials(cred))
  6. ...

然而,你可能会遇到后续的错误。我写了一篇文章,提到了使用Cloud Build时的Sheet访问问题(基于Python,因为大多数开发者使用Python,但我是Go开发者,如果需要,我可以帮你使用Go解决问题)。

这篇文章介绍了不同的令牌限定方式,其中一种方式将适用于你的底层环境。

英文:

You can scope your token like that in Go

  1. ...
  2. func main() {
  3. ctx := context.Background()
  4. cred, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/spreadsheets.readonly")
  5. srv, err := sheets.NewService(ctx, option.WithCredentials(cred))
  6. ...

However, you could (should?) have subsequent errors. I wrote that article that mention that Sheet access issue with Cloud Build (based on Python, because most of the dev use Python, but I'm a go developer, I will be able to help you in Go if you need).

The article present different way to scope a token, one of them will work for you, according to the underlying environment.

huangapple
  • 本文由 发表于 2022年11月6日 21:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/74336078.html
匿名

发表评论

匿名网友

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

确定