英文:
ACCESS_TOKEN_SCOPE_INSUFFICIENT when calling google.apps.sheets.v4.SpreadsheetsService.GetValues in Golang
问题
我正在尝试运行一个简单的Go程序,调用Google Sheets API:
package main
import (
"context"
"fmt"
"google.golang.org/api/sheets/v4"
)
func main() {
ctx := context.Background()
srv, err := sheets.NewService(ctx)
if err != nil {
panic(err)
}
resp, err := srv.Spreadsheets.Values.Get("1HG_cmqALkquc0ud8Pm7GKOhR5IpPSj20q_fFkHBaM5M", "A1").Do()
if err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("%+v", resp.Values))
}
但是当我尝试运行它时,我得到了以下错误:
$ rm /home/johan/.config/gcloud/application_default_credentials.json
$ gcloud auth application-default login
Your browser has been opened to visit:
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
Credentials saved to file: [/home/johan/.config/gcloud/application_default_credentials.json]
These credentials will be used by any library that requests Application Default Credentials (ADC).
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.
$ go run sheets_api.go
panic: googleapi: Error 403: Request had insufficient authentication scopes.
Details:
[
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"domain": "googleapis.com",
"metadata": {
"method": "google.apps.sheets.v4.SpreadsheetsService.GetValues",
"service": "sheets.googleapis.com"
},
"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
}
]
More details:
Reason: insufficientPermissions, Message: Insufficient Permission
goroutine 1 [running]:
main.main()
/home/johan/20q/cmd/labelserver/sheets_api.go:20 +0x194
exit status 2
我创建了这个Google表格,所以我的用户应该有访问权限。
我在这里做错了什么?
英文:
I am trying to run a simple go program calling the Google Sheets API:
package main
import (
"context"
"fmt"
"google.golang.org/api/sheets/v4"
)
func main() {
ctx := context.Background()
srv, err := sheets.NewService(ctx)
if err != nil {
panic(err)
}
resp, err := srv.Spreadsheets.Values.Get("1HG_cmqALkquc0ud8Pm7GKOhR5IpPSj20q_fFkHBaM5M", "A1").Do()
if err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("%+v", resp.Values))
}
But when I try to run it, I get:
$ rm /home/johan/.config/gcloud/application_default_credentials.json
$ gcloud auth application-default login
Your browser has been opened to visit:
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
Credentials saved to file: [/home/johan/.config/gcloud/application_default_credentials.json]
These credentials will be used by any library that requests Application Default Credentials (ADC).
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.
$ go run sheets_api.go
panic: googleapi: Error 403: Request had insufficient authentication scopes.
Details:
[
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"domain": "googleapis.com",
"metadata": {
"method": "google.apps.sheets.v4.SpreadsheetsService.GetValues",
"service": "sheets.googleapis.com"
},
"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
}
]
More details:
Reason: insufficientPermissions, Message: Insufficient Permission
goroutine 1 [running]:
main.main()
/home/johan/20q/cmd/labelserver/sheets_api.go:20 +0x194
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中这样限定你的令牌:
...
func main() {
ctx := context.Background()
cred, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/spreadsheets.readonly")
srv, err := sheets.NewService(ctx, option.WithCredentials(cred))
...
然而,你可能会遇到后续的错误。我写了一篇文章,提到了使用Cloud Build时的Sheet访问问题(基于Python,因为大多数开发者使用Python,但我是Go开发者,如果需要,我可以帮你使用Go解决问题)。
这篇文章介绍了不同的令牌限定方式,其中一种方式将适用于你的底层环境。
英文:
You can scope your token like that in Go
...
func main() {
ctx := context.Background()
cred, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/spreadsheets.readonly")
srv, err := sheets.NewService(ctx, option.WithCredentials(cred))
...
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论