【发布时间】:2018-06-14 04:58:30
【问题描述】:
我正在开发一个使用 Google Natural Language Processing API 解析来自用户的不同输入的 MVC Web 应用程序。
我已经成功使用并实现了 API 操作,只要我在本地计算机上运行应用程序,一切正常。但是,一旦我发布版本并将其上传到服务器上,我就会在调用 API 方法(例如,AnalyzeSentiment)时收到以下错误:
Status(StatusCode=Unauthenticated, Detail="Getting metadata from plugin failed with error: Exception occured in metadata credentials plugin.")
借助来自帖子的答案:Google Datastore authentication issue - C# 我能够进一步了解错误的详细信息(使用 gRCP):
An error occurred while sending the request.
Stacktrace: at Google.Apis.Http.ConfigurableMessageHandler.<SendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.Requests.TokenRequestExtenstions.<ExecuteAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.ServiceAccountCredential.<RequestAccessTokenAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.ServiceCredential.<GetAccessTokenForRequestAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.ServiceAccountCredential.<GetAccessTokenForRequestAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Grpc.Auth.GoogleAuthInterceptors.<>c__DisplayClass2_0.<<FromCredential>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Grpc.Core.Internal.NativeMetadataCredentialsPlugin.<GetMetadataAsync>d__11.MoveNext()
这似乎是一个身份验证问题,所以我仔细检查了 jsonKey 文件,这很好。请注意,我已使用代码在环境变量中设置凭据:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", jsonPath);
并使用以下方法进行验证:
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS")
调用如下:
private AnalyzeSentimentResponse AnalyzeSentiment(string statement)
{
GrpcEnvironment.SetLogger(new MyLogger());
var client = LanguageServiceClient.Create();
var response = client.AnalyzeSentiment(new Document()
{
Content = statement,
Type = Document.Types.Type.PlainText
});
return response;
}
无法弄清楚为什么当我在本地计算机上运行它时它工作正常,而当它部署在服务器上时它却失败了。该服务器也没有任何限制。
结果:
GoogleCredential.GetApplicationDefaultAsync().Result.UnderlyingCredential.GetType()
是:
Google.Apis.Auth.OAuth2.ServiceAccountCredential
注意:服务器是我们自己的(Windows Server 2012R2)
【问题讨论】:
-
您是否检查了运行MVC应用程序的用户是否有权调用SetEnvironmentVariable?根据该页面,如果您没有调用它的权限,您将获得 SecurityException。
-
请注意,链接到问题中的用户不会通知他们。希望我们可以解决这个问题......
-
您能否记录
GoogleCredential.GetApplicationDefaultAsync().Result.UnderlyingCredential.GetType()的结果并在问题中报告?您要在 Google Cloud Platform 中部署它的服务器,还是在其他地方(例如 Azure 或您自己的计算机上)? -
@NightOwl888:我认为没有权限问题,正如您在问题中看到的那样,设置变量后,我也使用 Get 方法获取它。返回了有效值。
-
好的,看起来应该没问题。特别是,它可以正确加载服务帐户。下一个潜在问题是您的服务器是否位于对 HTTP2 不友好的防火墙后面。您是否能够在您的服务器上运行一个简单的控制台应用程序来尝试从比 ASP.NET 应用程序更简单的环境中进行诊断?
标签: c# asp.net-mvc nlp google-cloud-platform google-authentication