google-api-go-client
google-api-go-client copied to clipboard
transport/grpc: support option.WithAPIKey
google.golang.org/ap/option says:
API Keys can only be used for JSON-over-HTTP APIs, including those under the import path google.golang.org/api/....
Not true, though, API keys can be used with gRPC.
Need to use the x-api-key metadata key:
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs("x-api-key", ...))
sorry, reading this now it doesn't look like a doc bug.
https://github.com/googleapis/google-api-go-client/blob/334f1cf68ef2ac2ae0ae06c6423b982b81aa607b/transport/grpc/dial.go#L119-L121
Workaround for "API keys are not supported for gRPC APIs. Remove the WithAPIKey option from your client-creating call."
Append the API key to the context used for the request (not for client creation).
ctx = metadata.AppendToOutgoingContext(ctx, "x-goog-api-key", key)
Then provide some dummy authentication:
option.WithTokenSource(oauth2.StaticTokenSource(&oauth2.Token{}))
Any better fix for this issue ?
@zhoub As of right now, I think this is still the best solution.