google-api-go-client icon indicating copy to clipboard operation
google-api-go-client copied to clipboard

transport/grpc: support option.WithAPIKey

Open broady opened this issue 6 years ago • 2 comments

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", ...))

broady avatar Sep 20 '19 22:09 broady

sorry, reading this now it doesn't look like a doc bug.

broady avatar May 18 '20 21:05 broady

https://github.com/googleapis/google-api-go-client/blob/334f1cf68ef2ac2ae0ae06c6423b982b81aa607b/transport/grpc/dial.go#L119-L121

broady avatar May 18 '20 21:05 broady

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{}))

broady avatar Oct 25 '22 01:10 broady

Any better fix for this issue ?

zhoub avatar May 08 '23 12:05 zhoub

@zhoub As of right now, I think this is still the best solution.

codyoss avatar May 08 '23 14:05 codyoss