【发布时间】:2021-04-11 21:53:53
【问题描述】:
我知道documentation exists... 这该死的神秘和复杂,典型的过度工程。它必须更简单。
我不想使用 3rd 方库...我想要一个漂亮的 vanilla js 提取。
我正在尝试使用nodejs...
let url = `https://translation.googleapis.com/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, come here!"],
mimeType: "text/plain",
},
});
let result = await response.json();
console.log(result);
得到这个错误:
{ 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.com/identity/sign-in/web/devconsole-project.',
status: 'UNAUTHENTICATED' }
}
有没有人知道正确的混合物来完成这个?
这是一个 V2 工作请求:
let url = `https://translation.googleapis.com/language/translate/v2?key=${API_KEY}&format=text&source=de&target=en&q=${encodeURIComponent(query)}`;
【问题讨论】:
-
如果您不使用承载,则需要删除 Authorization 标头
-
我正在使用节点。
标签: javascript node.js http google-api