简体   繁体   English

使用C#在Dialogflow API V2中进行身份验证

[英]Authentication in Dialogflow API V2 using C#

I have .NET Web API Project for the fulfillment API as our webhook in my Dialogflow agent. 我将履行API的.NET Web API项目作为我的Dialogflow代理中的webhook。 In our Post method of the controller, after getting the request from Dialogflow, I implement the explicit authentication as shown in the Google Cloud documentation for C#. 在我们的控制器的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}";
        }

The difference with the code is that after getting the client, when we call the AnalyzeSentiment() method, it doesn't do anything, and the projectId parameter is never used to authenticate. 与代码的不同之处在于,在获取客户端之后,当我们调用AnalyzeSentiment()方法时,它不会执行任何操作,并且projectId参数永远不会用于进行身份验证。 GCP docs are quite confusing, since when there is an AuthExplicit() that uses projectId, it uses it as a parameter for the buckets and only prints this on the console. GCP文档非常混乱,因为当有一个使用projectId的AuthExplicit()时,它会将它用作存储桶的参数,并且只在控制台上打印它。

It works fine, until we test the service account key with a different agent. 它工作正常,直到我们使用不同的代理测试服务帐户密钥。 Expected output is that authentication would fail, but somehow it still passes. 预期的输出是认证失败,但不知怎的,它仍然通过。

Once the Post method goes through the AuthExplicit() method, it would only return a boolean. 一旦Post方法通过AuthExplicit()方法,它将只返回一个布尔值。 Is this the right way to authenticate? 这是验证的正确方法吗? Or is there something else needed to invoke? 或者还有什么需要调用?

The difference with the code is that after getting the client, when we call the AnalyzeSentiment() method, it doesn't do anything, 与代码的不同之处在于,在获取客户端之后,当我们调用AnalyzeSentiment()方法时,它不执行任何操作,

Does client.AnalyzeSentiment() return an empty response? client.AnalyzeSentiment()是否返回空响应? Does the call hang forever? 通话是否永远挂起?

It works fine, until we test the service account key with a different agent. 它工作正常,直到我们使用不同的代理测试服务帐户密钥。

What is a different agent? 什么是不同的代理人? A different User-Agent header? 一个不同的User-Agent标头?

Once the Post method goes through the AuthExplicit() method, it would only return a boolean. 一旦Post方法通过AuthExplicit()方法,它将只返回一个布尔值。 Is this the right way to authenticate? 这是验证的正确方法吗? Or is there something else needed to invoke? 或者还有什么需要调用?

What does 'the Post method' refer to? “Post方法”指的是什么? What is the 'it' that would only return a boolean? 什么是只返回布尔值的'它'?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM