Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticating for @googlemaps/addressvalidation with an API key #5099

Open
codan84 opened this issue Feb 29, 2024 · 4 comments
Open

Authenticating for @googlemaps/addressvalidation with an API key #5099

codan84 opened this issue Feb 29, 2024 · 4 comments
Assignees

Comments

@codan84
Copy link

codan84 commented Feb 29, 2024

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?

@cedvdb
Copy link

cedvdb commented Apr 4, 2024

@codan84 what did you end up doing ?

@sofisl did you have time to look into this ?

@codan84
Copy link
Author

codan84 commented Apr 5, 2024

I ended up using fetch and hitting your APIs directly

@bshi
Copy link

bshi commented May 17, 2024

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:

@janvennemann
Copy link

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

I can confirm that this works. I was looking for any easy way to move from manual HTTP requests to the new Places SDK and here is how you can create the client:

import { v1 as PlacesApiV1 } from '@googlemaps/places';
import { JWT } from 'google-auth-library';

const { PlacesClient } = PlacesApiV1;
const authClient = new JWT();
authClient.fromAPIKey('my-api-key');
const placesClient = new PlacesClient({
    authClient,
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants