1
votes

I'm a newbie for Stack overflow. Please forgive me if I do anything against the rules here.

I am trying to use Google Cloud Translation API and I followed exactly the steps described here in the QuickStart.

When I ran the curl command, the response indicates that I don't have a valid API key. Please see below the exact response I received.

{

  "error": {

    "code": 403,

    "message": "The request is missing a valid API key.",

    "errors": [

      {

        "message": "The request is missing a valid API key.",

        "domain": "global",

        "reason": "forbidden"

      }

    ],

    "status": "PERMISSION_DENIED"

  }

}

I looked at the link again, but I believe none of the steps in the link mentions about API key. The link mentions about service account key. And I set the path to service account private key on environment variable properly and no error is reported about it.

Can anyone help me out for how I am supposed to set API key?

Thanks in advance.

Env: Win Powershell 5.1.18362.752

1

1 Answers

2
votes

The documentation you are following suggest the use of a service account to authenticate with the Translation API. In order to do that you need to follow the following steps:

I have tested and it worked.

curl -s -X POST -H "Content-Type: application/json"     -H "Authorization: Bearer "$(gcloud auth application-default 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"

On other hand, an API Key can be used in the request as shown here, but it requires to generate it beforehand.

curl -s -X POST -H "Content-Type: application/json" \ \
    --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?key=XXXXXXXXXXXXXXXXXXXXXXXXXX"