About this codelab
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
- How to search for and select APIs with the Google Cloud API Library and OAuth 2.0 Playground.
- How to view API metrics with the Metrics Explorer and Cloud APIs & Services.
What you'll need
- A Google Account, such as a Gmail account or a Google Workspace account.
- Access to a Cloud Billing account or credit card to use for a free trial.
2. Access APIs from the Cloud Console
- In the Cloud Console, select
Navigation menu > APIs & Services > Enabled APIs & services.
- In the APIs & Services dashboard, click + ENABLE APIS AND SERVICES to see the available APIs.
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.
- In the CATEGORY menu, select Machine learning so that you only see the Machine learning APIs.
- From the Machine learning APIs, select Cloud Natural Language API to see details about this API.
- Click ENABLE to enable this API in the appropriate project.
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.
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.
- Click MANAGE to return to the overview in the previous step.
- 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.
- Select the
analyzeEntitites
method.
You should see a web page with details about the API's methods and a Try this API pane.
- 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. - In the
content:
field, enter the text to analyze.
This example uses Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'.
- In the
type:
field, selectPLAIN_TEXT
.
The Request body should look like this:
For more information about each parameter, refer to the documentation.
- Clear the Google OAuth 2.0 checkbox and then click Execute to get a response with details about the entities.
- 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.
- 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.
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.
- In the Operations section of the
Navigation menu, click Monitoring > Metrics explorer.
- 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.)
- 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.
- To further customize the output, set an aggregator and adjust the time period.
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.
- Open the OAuth 2.0 Playground.
- Click Cloud Natural Language API > https://www.googleapis.com/auth/cloud-language > https://www.googleapis.com/auth/cloud-platform > Authorize APIs.
- If needed, sign in to your Google Account and then click Allow to get an authorization code.
- Click Exchange authorization code for tokens.
You can now make a request of the API.
- 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.
- Enter the Request URI information and add a request body.
You can use the same body that you used in the API Explorer.
- Click Close > Send the request.
You should get results similar to this image:
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:
- Click
Navigation menu > APIs & Services > Credentials.
- Under API Keys, select the key to delete and then click
.
- Similarly, rather than worry about your Service Account Private Key being unprotected, under Service Accounts, select the service account to delete and then click
.
6. Congratulations
Congratulations! You learned how to search for and select Google APIs, and view their metrics.