googleapis/google-api-go-client

GitHub: googleapis/google-api-go-client

Google 官方维护的 Go 语言客户端库,为开发者提供访问各类 Google APIs 的自动生成接口。

Stars: 4459 | Forks: 1368

# Google APIs Go 版客户端库 ## 快速开始 ``` $ go get google.golang.org/api/tasks/v1 $ go get google.golang.org/api/moderator/v1 $ go get google.golang.org/api/urlshortener/v1 ... etc ... ``` 并使用: ``` package main import ( "context" "net/http" "google.golang.org/api/urlshortener/v1" ) func main() { ctx := context.Background() svc, err := urlshortener.NewService(ctx) // ... } ``` * 如需更详细的教程,请参阅[入门指南](https://github.com/google/google-api-go-client/blob/main/GettingStarted.md)。 * 如需查看示例,请参阅[示例目录](https://github.com/google/google-api-go-client/tree/main/examples)。 * 如需获取支持,请使用 [golang-nuts](https://groups.google.com/group/golang-nuts) 邮件列表。 ## 状态 [![Go 参考](https://pkg.go.dev/google.golang.org/api)](https://pkg.go.dev/google.golang.org/api) 这些库是根据 Google Discovery Service 的 JSON 描述文件自动生成的 Go 库。 由于这些库是自动生成的,因此在不同版本之间可能包含破坏性更改。正因如此,生成器本身及其生成的代码均被视为 beta 版本。 这些客户端库由 Google 官方支持。但是,这些库已被视为完成状态,目前处于维护模式。这意味着我们将解决严重的 Bug 和安全问题,但不会添加任何新功能。 如果您正在使用 Google Cloud Platform API(例如 Datastore 或 Pub/Sub),请改用 [Go 版 Cloud 客户端库](https://github.com/googleapis/google-cloud-go)。这些是专为 Google Cloud Platform 服务量身定制的全新且符合 Go 语言习惯的库。 ## 授权 默认情况下,每个 API 都将使用 [Google 应用默认凭证](https://developers.google.com/identity/protocols/application-default-credentials) 作为调用 API endpoint 时使用的授权凭证。这将允许您的应用程序在许多环境中运行,而无需进行显式配置。 ``` // import "google.golang.org/api/sheets/v4" client, err := sheets.NewService(ctx) ``` 要使用 [JSON 密钥文件](https://cloud.google.com/iam/docs/managing-service-account-keys) 进行授权,请将 [`option.WithAuthCredentialsFile`](https://pkg.go.dev/google.golang.org/api/option#WithAuthCredentialsFile) 传递给所需包的 `NewService` 函数。您还必须指定凭证类型。例如,要使用服务账号密钥文件: ``` client, err := sheets.NewService(ctx, option.WithAuthCredentialsFile(option.ServiceAccount, "path/to/keyfile.json")) ``` 同样,您可以直接通过 [`option.WithAuthCredentialsJSON`](https://pkg.go.dev/google.golang.org/api/option#WithAuthCredentialsJSON) 使用 JSON 凭证: ``` // where jsonKey is a []byte containing the JSON key client, err := sheets.NewService(ctx, option.WithAuthCredentialsJSON(option.ServiceAccount, jsonKey)) ``` 较旧的 `option.WithCredentialsFile` 和 `option.WithCredentialsJSON` 函数由于存在潜在的安全风险已被弃用。 您可以使用 [`golang.org/x/oauth2`](https://pkg.go.dev/golang.org/x/oauth2) 包创建 `oauth2.TokenSource`,从而对授权进行更多控制。 然后将 [`option.WithTokenSource`](https://pkg.go.dev/google.golang.org/api/option#WithTokenSource) 传递给 `NewService` 函数: ``` tokenSource := ... svc, err := sheets.NewService(ctx, option.WithTokenSource(tokenSource)) ``` ## 更多信息 有关所有生成的客户端的更多信息,请通读我们的 [包文档](https://pkg.go.dev/google.golang.org/api#section-documentation)。
标签:EVTX分析, 日志审计