April 15, 2017

How to get Google Map API

To use the Google Maps Directions API, you must register your app project on the Google API Console and get a Google API key which you can add to your app or website.

Step 1: Get an API Key from the Google API Console

Click the button below, which guides you through the process of registering a project in the Google API Console, activates the Google Maps Directions API automatically, and generates a generic, unrestricted API key.

Step 2: Add the API key to your application

When loading the Google Maps Directions API, substitute YOUR_API_KEY in the code below with the API key you got from the previous step.

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY

Detailed guide for users of the standard Google Maps Directions API

Follow these steps to get an API key:

Go to the Google API Console.
Create or select a project.
Click Continue to enable the API.
On the Credentials page, get an API key. 
Note: If you have an existing unrestricted API key, or a key with server restrictions, you may use that key.
From the dialog displaying the API key, select Restrict key to set a server restriction on the API key.
In the Key restriction section, select 'IP addresses (web servers, cron jobs, etc.), follow the on-screen instructions to add server IP addresses, then click Save. Read more about restricting API keys.
(Optional) Enable billing. See Usage Limits for more information.

Choosing an authentication method for your application

The section below provides a summary of the various tools and reports that are available to Premium Plan customers, based on the method you choose to authenticate your application.

Authentication using an API key

By using an API key to authenticate your applications, you can:
Manage all of your APIs in the Google API Console.
Access real-time usage data and 30 days of historical usage data for your application in the Google API Console.
View usage reports with more than 30 days of data in the Google Cloud Support Portal.
Authentication using a client ID and digital signature
By using your client ID and digital signature (instead of an API key) to authenticate your application, you can:
Add the channel parameter to requests so you can view more detailed usage reports.
View usage reports with more than 30 days of data in the Google Cloud Support Portal.
Use Maps Analytics tools for the Maps JavaScript API.

Authenticating your application using a client ID and digital signature

Upon purchasing your Google Maps APIs Premium Plan license, you will receive a welcome email from Google that contains your client ID and your private cryptographic key.
Read more about the cryptographic key which you can use to generate a unique digital signature.

You must use both the client ID and unique digital signature to authenticate the Directions API.

The code example below shows you the client and signature parameters to which you must pass your client ID and unique digital signature.

    https://maps.googleapis.com/maps/api/directions/json
      ?origin=Toronto
      &destination=Montreal
      &client=gme-YOUR_CLIENT_ID
      &signature=YOUR_URL_SIGNATURE
Substitute gme-YOUR_CLIENT_ID with the client ID you got in your welcome email. All client IDs begin with a gme- prefix.
Substitute YOUR_URL_SIGNATURE with your unique digital signature. Read about how you can generate a digital signature using your cryptographic key.

Generating a digital signature

Requests to the Directions API by Google Maps APIs Premium Plan customers require a digital signature that you can generate using the private cryptographic key provided to you in your welcome email.

Follow the steps below to generate a digital signature for your request.

Construct the request URL without the signature, making sure to include your client parameter. Note that any non-standard characters will need to be URL-encoded:

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&client=clientID

Note: All Google services require UTF-8 character encoding (which implicitly includes ASCII). If your applications operate using other character sets, make sure they construct URLs using UTF-8 and properly URL-encode them.
Strip off the domain portion of the request, leaving only the path and the query:

/maps/api/directions/json?origin=Toronto&destination=Montreal&client=clientID
Retrieve your private key, which is encoded in a modified Base64 for URLs, and sign the URL above using the HMAC-SHA1 algorithm. You may need to decode this key into its original binary format. Note that in most cryptographic libraries, the resulting signature will be in binary format.

Note: Modified Base64 for URLs replaces the + and / characters of standard Base64 with - and _ respectively, so that these Base64 signatures no longer need to be URL-encoded.
Encode the resulting binary signature using the modified Base64 for URLs to convert this signature into something that can be passed within a URL.
Attach this signature to the URL within a signature parameter:

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&client=clientID&signature=base64signature

Restricting an API key

Google Maps APIs are available via HTTP web services, for web browsers, and Android or iOS apps. APIs in any platform can use a generic (unrestricted) API key. You can optionally add a restriction (for example, IP addresses) to the API key. Once restricted, a key will only work on platforms that support that type of restriction.

To add server restrictions to an existing, generic API key, do the following:

1. Go to the Credentials page of the Google API Console.
2. Select the project that contains the API key you want to edit.
3. On the Credentials page, from the list of API keys, select the name of the API key to edit           the details of the key.
4. In the Key restriction section of the page, select IP addresses (web servers, cron jobs, etc.),       follow the on-screen instructions to add server IP addresses, then click Save.

Troubleshooting authentication issues

If your request is malformed or supplies an invalid signature, the Google Maps Directions API returns an HTTP 403 (Forbidden) error.

To troubleshoot individual URLs, you can use the URL Signing Debugger. It allows you to quickly validate a URL and signature generated by your application.

Alternatively, Google Maps APIs Premium Plan customers can troubleshoot individual URLs by logging in to the Google Cloud Support Portal and selecting Resources > Google Maps APIs Premium Plan online tools > URL Signing Debugger for Web Service and Image APIs.

No comments:

Post a Comment