admin管理员组文章数量:1318564
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
2 Answers
Reset to default 4Modification 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 likeAuthorization: "Bearer ###accessToken###"
.B
ofBearer
is the uppercase letter. And please insert a space betweenBearer
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.
本文标签: javascriptHow to make a Google Translate API V3 simple HTTP POST requestStack Overflow
版权声明:本文标题:javascript - How to make a Google Translate API V3 simple HTTP POST request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050027a2418016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论