commit | 4554f64b6b0b7213cbe6bf75678cacc28ed28a03 | [log] [tgz] |
---|---|---|
author | Cody Oss <codyoss@google.com> | Mon May 04 09:04:36 2020 -0600 |
committer | Cody Oss <codyoss@google.com> | Tue May 05 16:06:07 2020 +0000 |
tree | ebe836a26485c8f4f5d673eacb0c725c20726914 | |
parent | 14f6709d501ea0b1be187c0b14437aa38bee7619 [diff] |
googleapi: return error details According to aip.dev/193 our api error responses should contain a details field. We should parse this field and add its contents to the error string. Without this some of our error messages are very vague. For example: `googleapi: Error 400: The request has errors`. This is not actionable for users. Also, appears there was an idea of having more error details at one point in time with the Errors field. I don't think this is ever used in practice though. We might want to consider deprecating this field in a future release. Fixes: #473 Change-Id: Iab992a07d3a6a98033ef850f7eb6091815d9e916 Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/55730 Reviewed-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Tyler Bui-Palsulich <tbp@google.com>
$ 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 ...
and using:
package main import ( "net/http" "google.golang.org/api/urlshortener/v1" ) func main() { svc, err := urlshortener.New(http.DefaultClient) // ... }
These are auto-generated Go libraries from the Google Discovery Service's JSON description files of the available “new style” Google APIs.
Due to the auto-generated nature of this collection of libraries, complete APIs or specific versions can appear or go away without notice. As a result, you should always locally vendor any API(s) that your code relies upon.
These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.
If you're working with Google Cloud Platform APIs such as Datastore or Pub/Sub, consider using the Cloud Client Libraries for Go instead. These are the new and idiomatic Go libraries targeted specifically at Google Cloud Platform Services.
The generator itself and the code it produces are beta. Some APIs are alpha/beta, and indicated as such in the import path (e.g., “google.golang.org/api/someapi/v1alpha”).
Application Default Credentials provide a simplified way to obtain credentials for authenticating with Google APIs.
The Application Default Credentials authenticate as the application itself, which make them great for working with Google Cloud APIs like Storage or Datastore. They are the recommended form of authentication when building applications that run on Google Compute Engine or Google App Engine.
Default credentials are provided by the golang.org/x/oauth2/google
package. To use them, add the following import:
import "golang.org/x/oauth2/google"
Some credentials types require you to specify scopes, and service entry points may not inject them. If you encounter this situation you may need to specify scopes as follows:
import ( "context" "golang.org/x/oauth2/google" "google.golang.org/api/compute/v1" ) func main() { // Use oauth2.NoContext if there isn't a good context to pass in. ctx := context.Background() client, err := google.DefaultClient(ctx, compute.ComputeScope) if err != nil { //... } computeService, err := compute.New(client) if err != nil { //... } }
If you need a oauth2.TokenSource
, use the DefaultTokenSource
function:
ts, err := google.DefaultTokenSource(ctx, scope1, scope2, ...) if err != nil { //... } client := oauth2.NewClient(ctx, ts)
See also: golang.org/x/oauth2/google package documentation.