[英]Authentication in Dialogflow API V2 using C#
我将履行API的.NET Web API项目作为我的Dialogflow代理中的webhook。 在我们的控制器的Post方法中,在收到Dialogflow的请求后,我实现了显式身份验证,如C#的Google Cloud文档中所示。
//jsonFileName is the name of the serviceAccountKey json generated from the Google Cloud Platform that's encrypted internally
public bool AuthExplicit(string projectId, string jsonFileName)
{
try
{
string JsonCredential = DecryptHelper.Decrypt(jsonFileName);
var credential = GoogleCredential.FromJson(JsonCredential).CreateScoped(LanguageServiceClient.DefaultScopes);
var channel = new Grpc.Core.Channel(
LanguageServiceClient.DefaultEndpoint.ToString(),
credential.ToChannelCredentials());
var client = LanguageServiceClient.Create(channel);
AnalyzeSentiment(client);
if (client != null)
{
return true;
}
else
{
return false;
}
}
internal void AnalyzeSentiment(LanguageServiceClient client)
{
var response = client.AnalyzeSentiment(new Document()
{
Content = "Authenticated.",
Type = Document.Types.Type.PlainText
});
var sentiment = response.DocumentSentiment;
string score = $"Score: {sentiment.Score}";
string magnitude = $"Magnitude: {sentiment.Magnitude}";
}
与代码的不同之处在于,在获取客户端之后,当我们调用AnalyzeSentiment()方法时,它不会执行任何操作,并且projectId参数永远不会用于进行身份验证。 GCP文档非常混乱,因为当有一个使用projectId的AuthExplicit()时,它会将它用作存储桶的参数,并且只在控制台上打印它。
它工作正常,直到我们使用不同的代理测试服务帐户密钥。 预期的输出是认证失败,但不知怎的,它仍然通过。
一旦Post方法通过AuthExplicit()方法,它将只返回一个布尔值。 这是验证的正确方法吗? 或者还有什么需要调用?
与代码的不同之处在于,在获取客户端之后,当我们调用AnalyzeSentiment()方法时,它不执行任何操作,
client.AnalyzeSentiment()
是否返回空响应? 通话是否永远挂起?
它工作正常,直到我们使用不同的代理测试服务帐户密钥。
什么是不同的代理人? 一个不同的User-Agent标头?
一旦Post方法通过AuthExplicit()方法,它将只返回一个布尔值。 这是验证的正确方法吗? 或者还有什么需要调用?
“Post方法”指的是什么? 什么是只返回布尔值的'它'?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.