Search for and select Google APIs

1. Before you begin

This codelab shows you how to search for and select Google APIs, and view API metrics.

Prerequisites

  • Ability to navigate the Google Cloud Console.

What you'll learn

What you'll need

2. Access APIs from the Cloud Console

  1. In the Cloud Console, select 5854836f11d5d97b.png Navigation menu > APIs & Services > Enabled APIs & services.

The Navigation menu in the Cloud Console showing "APIs & Services" and "Enabled APIs & services" selected.

  1. In the APIs & Services dashboard, click + ENABLE APIS AND SERVICES to see the available APIs.

The APIs & Services dashboard showing the ENABLE APIS AND SERVICES option.

From the APIs & Services page, there are a number of ways that you can search for APIs. If you know the name of the API, you can enter it in the Search for APIs & Services field at the top of the page. You can also filter APIs by category with the filters at the side of the page.

A list of APIs resulting from the Search for APIs & Services field.

  1. In the CATEGORY menu, select Machine learning so that you only see the Machine learning APIs.

APIs filtered by CATEGORY to show only Machine learning APIs.

  1. From the Machine learning APIs, select Cloud Natural Language API to see details about this API.
  2. Click ENABLE to enable this API in the appropriate project.

Cloud Natural Language API pane showing ENABLE and TRY THIS API buttons.

When you enable the API, it displays the API's Overview page. You can see the metrics for your usage of this API, which has very little information because you recently enabled the API.

The selected API's Overview page showing metrics for the API.

In a future codelab, you learn more about how to create credentials, but if you're curious now, click CREATE CREDENTIALS and then answer the questions presented.

  1. Click MANAGE to return to the overview in the previous step.

Overview page for the Cloud Natural Language API.

  1. To try the API (even before you enable it), click TRY THIS API.

This usually brings you to a page in the documentation for the API. For some APIs, however, there's no immediate way to try the API because it has many different methods. That is the case for the Cloud Natural Language API.

The Cloud Natural Language API's reference page.

  1. Select the analyzeEntitites method.

You should see a web page with details about the API's methods and a Try this API pane.

Page for the analyzeEntitites method showing the method's details and a Try this API pane.

  1. To try a sample query, add a document object to the Request body field with these parameters. Click the + icons to expand the field options.
  2. In the content: field, enter the text to analyze.

This example uses Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'.

  1. In the type: field, select PLAIN_TEXT.

The Request body should look like this:

Request body pane showing request body parameters.

For more information about each parameter, refer to the documentation.

  1. Clear the Google OAuth 2.0 checkbox and then click Execute to get a response with details about the entities.

200 response with details about the entities.

  1. Clear the API key checkbox and then click Execute again.

When you execute the query this time, you get an error like this one because the API requires credentials to run.

403 response for request missing credentials.

  1. Select the Google OAuth 2.0 checkbox and then click Execute again.

OAuth 2.0 provides the API permissions on your account and shows you a warning.

OAuth 2.0 warning that Google APIs Explorer wants access to your Google account.

If you allow access, you get the same response as you did with API keys. If you use the API in an app and you need to grant access to individuals with knowledge of their credentials, you need to use OAuth 2.0. If it's enough for the app to access the API for the user, you can use API keys instead.

3. View your API metrics with the Metrics Explorer

You can see the history of calls for any single API from the API Overview page, as shown in step 6 of Access APIs from the Cloud Console. However, to see more metrics for all API usage, use the Metrics Explorer.

  1. In the Operations section of the f6b6844bf5688982.png Navigation menu, click Monitoring > Metrics explorer.

The Operations section of the  Navigation menu showing the Monitoring and Metrics explorer options selected.

  1. In the Resource type menu, select Consumed API as the type of metric. (If you don't see it, clear the Only show active checkbox.)

ca850c4295ef4ec9.png

  1. In the Metric menu, select one of the individual metrics for APIs, such as Request Count, to see how many APIs were called in the given period.

Metrics Explorer showing Consumed APIs metrics for Request Count.

  1. To further customize the output, set an aggregator and adjust the time period. Metrics Explorer showing Consumed APIs metrics for Request Count with an aggregator and time period specified.

4. Use the OAuth 2.0 Playground to try APIs

You can also use the OAuth 2.0 Playground to try Google APIs. With the OAuth 2.0 Playground, you can select an API, authorize it, and then send requests and see the responses in the OAuth 2.0 Playground's console window.

  1. Open the OAuth 2.0 Playground.

OAuth 2.0 Playground showing a listing of APIs you can try.

  1. Click Cloud Natural Language API > https://www.googleapis.com/auth/cloud-language > https://www.googleapis.com/auth/cloud-platform > Authorize APIs.

OAuth 2.0 Playground showing the Cloud Natural Language APIs selected.

  1. If needed, sign in to your Google Account and then click Allow to get an authorization code.
  2. Click Exchange authorization code for tokens.

Exchange authorization code for tokens pane from where you can click Exchange authorization code for tokens.

You can now make a request of the API.

  1. Click List possible operations to examine all the possible API calls.

You previously opened the information page for the Natural Language API analyzeEntities document, so you can see that the URL for the HTTP request is:

https://language.googleapis.com/v1beta2/documents:analyzeEntities

This is a POST request.

Configure request to API pane, from where you can select the HTTP method, register the request URI, enter the request body, and send the request.

  1. Enter the Request URI information and add a request body.

You can use the same body that you used in the API Explorer.

Request body pane.

  1. Click Close > Send the request.

You should get results similar to this image:

OAuth 2.0 Request/Response pane after posting the request.

Feel free to experiment with other APIs in the OAuth 2.0 Playground.

5. Clean up

It's bad practice to have an unrestricted API key for projects. If someone gets access to it, that person can use it with no further need for authentication.

To delete this API key:

  1. Click f6b6844bf5688982.png Navigation menu > APIs & Services > Credentials.
  2. Under API Keys, select the key to delete and then click 247adf2e1d1eae4b.png.
  3. Similarly, rather than worry about your Service Account Private Key being unprotected, under Service Accounts, select the service account to delete and then click 247adf2e1d1eae4b.png.

6. Congratulations

Congratulations! You learned how to search for and select Google APIs, and view their metrics.