Authenticating for @googlemaps/addressvalidation with an API key
codan84 opened this issue · comments
According to the docs/examples of Google Address Validation we can use an API key to authenticate:
curl -X POST -d '{
"address": {
"regionCode": "US",
"locality": "Mountain View",
"addressLines": ["1600 Amphitheatre Pkwy"]
}
}' \
-H 'Content-Type: application/json' \
"https://addressvalidation.googleapis.com/v1:validateAddress?key=API_KEY"
https://developers.google.com/maps/documentation/address-validation/requests-validate-address
However, I can't see any reference/docs as to how I can use said API key when using SDK: https://www.npmjs.com/package/@googlemaps/addressvalidation
Nor can I see any reference to that in https://github.com/googleapis/google-auth-library-nodejs
Any clues? Or am I restricted to making http calls (fetch, axios etc) from my node app in order to achieve this?
I ended up using fetch and hitting your APIs directly
Nor can I see any reference to that in https://github.com/googleapis/google-auth-library-nodejs
I haven't tested this myself yet, but tracing through some of the dependencies that the client library pulls in, you can use google-auth-library to create a JWT
AuthClient instance using [fromAPIKey(apiKey: string, options?: AuthClientOptions)
](https://github.com/googleapis/google-auth-library-nodejs/blob/7dbedff829be093dad0a2da32f0ab12862d473bc/src/auth/googleauth.ts#L812]
This can then be injected into AddressValidationClient to override the default auth mechanisms.
const options = {
authClient: fromAPIKey("my-api-key"),
};
const apiClient = new AddressValidationClient(options);
See also:
- https://github.com/googleapis/google-cloud-node/blob/main/packages/google-maps-addressvalidation/src/v1/address_validation_client.ts#L105
- https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#authentication-options
- https://googleapis.dev/nodejs/google-auth-library/latest/interfaces/GoogleAuthOptions.html#authClient