我正在尝试找到一种在 Google App Engine 上使用 Go 在 this example 之后动态创建项目的方法. 但是,文档在如何继续对用户进行身份验证方面不是很清楚(至少对我而言),Go 示例甚至说:
// TODO: Fill required fields.
我还查看了 Go Client Library Documentation这让我更加困惑。
有没有人以前使用过这个 API 或找到了在 App Engine 中以编程方式创建项目的方法?
最佳答案
当您通过调用 cloudresourcemanager.New(hc) 初始化 API 时,您会传递一个 http 客户端,在本例中,该客户端使用系统的默认凭据进行身份验证。这些凭据来自 App Engine 或 Compute Engine 的默认服务帐户(虽然不确定第二个),具体取决于您所在的环境。如果您不在 Google Cloud 中,则可以按照示例中的说明进行操作:
// 2. This sample uses Application Default Credentials for authentication.
// If not already done, install the gcloud CLI from
// https://cloud.google.com/sdk/ and run
// 'gcloud beta auth application-default login'
如果您这样做,默认凭据将从您用于登录命令 gcloud beta auth application-default login 的用户那里获取。另一种可能性是通过调用 JWTConfigFromJson 1 来更改代码以生成凭据。 ,这将从 json 文件中显式加载您的凭据。这里2您可以查看如何使用此方法初始化 http 客户端的示例。
关于google-app-engine - 在 Go 中以编程方式创建 Google App Engine 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39609357/