This page shows how to make a Google Cloud Translation API request with
curl.
Before you begin
-
Set up a GCP Console project.
Set up a project
Click to:
- Create or select a project.
- Enable the Cloud Translation
API
for that project.
- Create a service account.
- Download a private key as JSON.
You can view and manage these resources at any time in the
GCP Console.
-
Install and initialize the Cloud SDK.
-
Provide authentication credentials to your application code by
running the following command.
Replace
[PATH] with the location of the JSON file that contains your
credentials.
gcloud auth activate-service-account --key-file=[PATH]
Make a Translation API request
Make a Translation API Request using a REST method call to the
v2
translate method.
-
Use
curl to make a request to the
https://translation.googleapis.com/language/translate/v2
endpoint.
The curl command includes JSON with the text to be
translated (q), the language to translate from
(source), and the language to translate to
(target).
The source and target languages are identified using the
iso-639-1
codes. The source language is English (en) and the target language is
Spanish (es). The format of the query is noted as "text" for plain text.
The sample curl command uses the gcloud auth print-access-token command to get an authentication token.
curl -s -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer "$(gcloud auth print-access-token) \
--data "{
'q': 'The Great Pyramid of Giza (also known as the Pyramid of Khufu or the
Pyramid of Cheops) is the oldest and largest of the three pyramids in
the Giza pyramid complex.',
'source': 'en',
'target': 'es',
'format': 'text'
}" "https://translation.googleapis.com/language/translate/v2"
You should see a response similar to the following:
{
"data": {
"translations": [
{
"translatedText": "La Gran Pirámide de Giza (también conocida como la
Pirámide de Khufu o la Pirámide de Keops) es la más antigua y más
grande de las tres pirámides en el complejo de la pirámide de Giza."
}
]
}
}
Congratulations! You've sent your first request to the Cloud Translation API!
Clean up
To avoid unnecessary Google Cloud Platform charges, use the
GCP Console to delete your project if you do not need it.
No comments:
Post a Comment