Aly*_*Aly 5 javascript google-chrome google-translate google-chrome-extension google-translation-api
我正在尝试制作一个使用Google Cloud Translation API的 Chrome 扩展程序。我在 Google 翻译和开发 Chrome 扩展的文档中的许多不同页面上遵循了一堆说明,但我仍然无法弄清楚如何在浏览器中实际使用 API。
我得到的最远的是遵循快速入门指南的一部分,在那里您创建服务帐户,获取服务帐户密钥文件(作为 .json 文件下载),安装并初始化 Google SDK,将GOOGLE_APPLICATION_CREDENTIALS环境变量设置为路径的服务帐户 JSON 密钥文件,然后curl使用gcloud auth application-default print-access-token获取访问令牌作为请求标头的一部分输入的请求:“授权:承载(此处为令牌)”。以这种方式成功检索到翻译。
但是在扩展的环境中,你不能运行这样的命令gcloud,这样就行不通了。我能够使用该gcloud命令手动检索访问令牌,然后将其粘贴到我的 javascript 代码中作为请求标头的一部分(例如xmlHttpRequest.setRequestHeader('Authorization', 'Bearer ya29.c.Elr6BRuTfOtherRandomLetters');)。但是该访问令牌会在 60 分钟后过期,因此无法持续工作。
我还尝试使用identity.getAuthToken方法(在manifest.json按照文档所说的方式更新内容之后),但这不起作用。使用我的服务帐户客户端 ID 作为client_idunder oauth2inmanifest.json导致来自identity.getAuthToken. 另一方面,从 Google 控制台生成 OAuth2 客户端 ID,并使用myoauth2clientid.apps.googleusercontent.comas the client_idunderoauth2和["https://www.googleapis.com/auth/cloud-translation", "https://www.googleapis.com/auth/cloud-platform"]as the scopesunder 会oauth2导致identity.getAuthToken.
从 Google 控制台页面生成 API 密钥,并将其直接用作key我的翻译查询的参数,甚至不起作用:我收到了“请求缺少有效的 API 密钥”。错误。
我真的很茫然。任何帮助都是极好的。
我(第一次发帖者)也经历了与您相同的步骤,但在 @magicnumber\'s 答案的帮助下,我想出了一个解决方案,使用 APIkey 而不是在我的 Chrome 扩展程序中翻译文本(从任何语言到英语)服务帐户密钥文件。
\n\n首先创建一个翻译函数(我在这里使用了 Async -> Await 模式):
\n\n async function translate(strSourceText,strApiKey)\n {\n var url = `https://translation.googleapis.com/language/translate/v2?key=${strApiKey}`\n let response = await fetch(url, {\n method: \'POST\',\n headers : { \n \'Content-Type\': \'application/json\',\n },\n body: JSON.stringify({\n target: "en",\n format: "text",\n q: strSourceText\n }),\n }); \n let data = await response.json()\n return data;\n }\nRun Code Online (Sandbox Code Playgroud)\n\n然后调用 async 函数,该函数将返回一个承诺,例如:
\n\nquery="\xd7\x9b\xd7\x9c\xd7\x91" //Hebrew word for dog \n\ntranslate(query, "YOURVALIDAPIKEYGOESHERE")\n .then(response => console.log(response.data))\n .catch(reason => console.log(reason.message));\nRun Code Online (Sandbox Code Playgroud)\n\nGoogle Translation API 的响应应如下所示:
\n\n{\n "data": {\n "translations": [\n {\n "translatedText": "Dog",\n "detectedSourceLanguage": "iw"\n }\n ]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n最后,为了避免违反内容安全策略,在我的例子中,我必须将翻译 API 域添加到扩展程序的 manifest.json 文件中的权限密钥中:
\n\n"permissions": ["https://translation.googleapis.com/language/translate/v2/*"],\nRun Code Online (Sandbox Code Playgroud)\n
我经历了与您相同的步骤,但也不能依赖 gcloud 进行身份验证。
是什么让我克服了“请求缺少有效的 API 密钥”错误,是使用此处描述的查询语法https://codelabs.developers.google.com/codelabs/cloud-translation-intro/index.html#5
就我而言,我使用了:
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=YOURVALIDAPIKEYHERE"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1900 次 |
| 最近记录: |