我这周第一次收到这个错误,这真的让我很困惑。情况是这样的:
附注- 使用此设置大约 4 个月,没有遇到此问题。有什么变化吗?
令人沮丧的是,使用 Python 仍然可以在 GCS 中列出存储桶:
python3 -c 'from google.cloud.storage import Client; print(list(Client().list_buckets()))'
但是 Golang 没有:
package main
import (
"context"
"fmt"
"log"
"cloud.google.com/go/storage"
)
func main() {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
it := client.Buckets(ctx, "surfey")
fmt.Println("Buckets:")
for {
attrs, err := it.Next()
if err != nil {
log.Printf("Failed to list buckets: %v", err)
break
}
fmt.Println(attrs.Name)
}
}
$ ./gobuckets 桶: 2024/05/15 21:42:37 无法列出存储桶:获取“https://storage.googleapis.com/storage/v1/b?alt=json&pageToken=&prefix=&prettyPrint=false&project=surfey&projection=full”:oauth2: “invalid_grant”“reauth 相关错误 (invalid_rapt)”“https://support.google.com/a/answer/9368756”
不过,我发现可以通过将 Google Workspace 管理中心中的重新身份验证政策设置为从不要求重新身份验证来解决该问题...这感觉是一个非常糟糕的解决方案。
不幸的是,我不能简单地运行
gcloud auth application-default login
,因为正如我所说,我在 Google Cloud Shell 上,从技术上讲,它作为 Google 计算实例运行,因此这给了我一个警告:
n@cloudshell:~$ gcloud auth application-default login
You are running on a Google Compute Engine virtual machine.
The service credentials associated with this virtual machine
will automatically be used by Application Default
Credentials, so it is not necessary to use this command.
If you decide to proceed anyway, your user credentials may be visible
to others with access to this virtual machine. Are you sure you want
to authenticate with your personal account?
Do you want to continue (Y/n)?
这是一个需要解决的复杂问题,已经浪费了我很多时间。我想知道...
像往常一样^1我在这里充分描述后发现了问题。但是,嘿,如果您遇到同样的问题,那么我希望这对您有帮助:
一路上我曾使用过
gcloud auth application-default login
并且有一个~/.config/gcloud/application_default_credentials.json
! 🤦我跑了rm -rf ~/.config/gcloud
,问题就消失了。
现在我正在尝试弄清楚如何检测正在使用哪些凭据,因为显然 Python 没有使用这些凭据。