最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a Google Translate API V3 simple HTTP POST request? - Stack Overflow

programmeradmin0浏览0评论

I am aware that documentation exists... It's just so damn cryptic and convoluted, typical overengineering. It has to be simpler.

I don't want to use 3rd party libraries... I want a beautiful vanilla js fetch.

I am trying the following with nodejs...

let url = `:translateText?key=API_KEY`;

let response = await fetch(url, {
    method: "POST",
    withCredentials: true,
    credentials: "include",
    headers: {
        Authorization: "bearer",         
        "Content-Type": "application/json",
    },
    body: {
        sourceLanguageCode: "en",
        targetLanguageCode: "ru",
        contents: ["Dr. Watson, e here!"],
        mimeType: "text/plain",
    },
});

let result = await response.json();

console.log(result);

And getting this error:

{ error:
   { code: 401,
     message:
      'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See .',
     status: 'UNAUTHENTICATED' } 
}

Does anyone know the right concoction to pull this off?

Here's a V2 working request:

let url = `=${API_KEY}&format=text&source=de&target=en&q=${encodeURIComponent(query)}`;

I am aware that documentation exists... It's just so damn cryptic and convoluted, typical overengineering. It has to be simpler.

I don't want to use 3rd party libraries... I want a beautiful vanilla js fetch.

I am trying the following with nodejs...

let url = `https://translation.googleapis./v3/projects/PROJECT_ID:translateText?key=API_KEY`;

let response = await fetch(url, {
    method: "POST",
    withCredentials: true,
    credentials: "include",
    headers: {
        Authorization: "bearer",         
        "Content-Type": "application/json",
    },
    body: {
        sourceLanguageCode: "en",
        targetLanguageCode: "ru",
        contents: ["Dr. Watson, e here!"],
        mimeType: "text/plain",
    },
});

let result = await response.json();

console.log(result);

And getting this error:

{ error:
   { code: 401,
     message:
      'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google./identity/sign-in/web/devconsole-project.',
     status: 'UNAUTHENTICATED' } 
}

Does anyone know the right concoction to pull this off?

Here's a V2 working request:

let url = `https://translation.googleapis./language/translate/v2?key=${API_KEY}&format=text&source=de&target=en&q=${encodeURIComponent(query)}`;
Share Improve this question edited Apr 11, 2021 at 22:11 Sir Rubberduck asked Apr 11, 2021 at 21:53 Sir RubberduckSir Rubberduck 1 2
  • You need to remove the Authorization header, if you're not using a bearer – blex Commented Apr 11, 2021 at 21:58
  • 1 I am using node. – Sir Rubberduck Commented Apr 11, 2021 at 22:00
Add a ment  | 

2 Answers 2

Reset to default 4

Modification points:

  • At Google APIs, unfortunately, the API key cannot be used for the POST method. It seems that this is the current specification of Google side. So in your situation, it is required to use the access token.

  • Unfortunately, I cannot understand about Authorization: "bearer". If the access token is used, please set like Authorization: "Bearer ###accessToken###". B of Bearer is the uppercase letter. And please insert a space between Bearer and ###accessToken###. Please be careful this.

  • Please sent the JSON object as the string value.

  • From the official document in your question, the sample curl mand is as follows.

      curl -X POST \
      -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
      -H "Content-Type: application/json; charset=utf-8" \
      -d @request.json \
      https://translation.googleapis./v3/projects/project-number-or-id:translateText
    

When above points are reflected to your script, it bees as follows.

Modified script:

Please set your access token.

let url = "https://translation.googleapis./v3/projects/PROJECT_ID:translateText";  // Modified
let response = await fetch(url, {
  method: "POST",
  headers: {
    "Authorization": "Bearer ###accessToken###",  // Modified
    "Content-Type": "application/json",
  },
  body: JSON.stringify({  // Modified
    sourceLanguageCode: "en",
    targetLanguageCode: "ru",
    contents: ["Dr. Watson, e here!"],
    mimeType: "text/plain",
  }),
});

let result = await response.json();

console.log(result);

Reference:

  • Translating text (Advanced)

I had a similar problem even with v2 of that API. Using the header X-goog-api-key instead of including the key in the body works so far, however.

发布评论

评论列表(0)

  1. 暂无评论