Nic*_*pes 5 c# asp.net authentication google-translate google-cloud-platform
我在C#中运行Google Translate API.在我的计算机上本地运行下一个代码工作,但在服务器上联机它会引发以下错误:
using Google.Cloud.Translation.V2;
TranslationClient client = TranslationClient.Create();
var response = client.TranslateText(sentence, targetLanguage, sourceLanguage: sourceLanguage);
Run Code Online (Sandbox Code Playgroud)
"应用程序默认凭据不可用.如果在Google Compute Engine中运行,则可以使用它们.否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向定义凭据的文件.请参阅https://developers.google.com/accounts/docs/application-default-credentials有关详细信息."
本地只需安装Cloud SDK Installer即可完成所有设置,无需在代码中进行身份验证.在服务器上,我应该使用OAuth 2.0或服务帐户密钥吗?
有人可以帮我解决这个问题吗?
编辑:有人可以向我确认是否有必要访问本地服务器以在命令行中运行命令,如https://cloud.google.com/storage/docs/authentication?这将是非常荒谬的,而不仅仅是编写代码.例如,Youtube API不需要本地访问.
liv*_*ove 12
按照指示获取json文件:
https://cloud.google.com/translate/docs/reference/libraries
然后首先运行此代码:
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "c:\mypath\myfile.json");
Run Code Online (Sandbox Code Playgroud)
对于我的问题,为了避免服务器上的本地设置,最简单的答案是使用下面描述的翻译 API 的第三个选项:使用 API 密钥。这意味着只需向链接中具有 API 密钥的端点进行简单的 POST。
https://cloud.google.com/docs/authentication/#getting_credentials_for_server-centric_flow https://cloud.google.com/docs/authentication/api-keys
| 归档时间: |
|
| 查看次数: |
4356 次 |
| 最近记录: |