为什么 Google Cloud Shell / Golang 上出现“reauth 相关错误 (invalid_rapt)”?

问题描述 投票:0回答:1

我这周第一次收到这个错误,这真的让我很困惑。情况是这样的:

  1. 登录 Google Workspace 帐号(自定义域名/不是 Gmail.com)并且...
  2. 使用 Google Cloud Shell ...
  3. 构建 Golang 项目......
  4. 调用 Google Cloud API(例如 Google Cloud Storage)...
  5. 即使没有进行身份验证,也会收到可怕的 invalid_rapt 🤔

附注- 使用此设置大约 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 管理中心中的重新身份验证政策设置为从不要求重新身份验证来解决该问题...这感觉是一个非常糟糕的解决方案。

Reauthentication policy: Never require reauthentication

不幸的是,我不能简单地运行

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

这是一个需要解决的复杂问题,已经浪费了我很多时间。我想知道...

  • 为什么Python可以工作而Golang却不能(使用默认客户端)?
  • 当我不使用本地凭据时,我实际上如何重新进行身份验证?
  • 我可以将什么“应用程序”标记为受信任,这样我就不需要进行全面的“无需重新身份验证”配置?
google-cloud-platform google-oauth google-cloud-shell
1个回答
0
投票

像往常一样^1我在这里充分描述后发现了问题。但是,嘿,如果您遇到同样的问题,那么我希望这对您有帮助:

一路上我使用过

gcloud auth application-default login
并且有一个
~/.config/gcloud/application_default_credentials.json
! 🤦我跑了
rm -rf ~/.config/gcloud
,问题就消失了。

现在我正在尝试弄清楚如何检测正在使用哪些凭据,因为显然 Python 没有使用这些凭据。

© www.soinside.com 2019 - 2024. All rights reserved.