简体   繁体   中英

Authentication in Dialogflow API V2 using C#

I have .NET Web API Project for the fulfillment API as our webhook in my Dialogflow agent. 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#.

//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. 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.

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. 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,

Does client.AnalyzeSentiment() return an empty response? 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?

Once the Post method goes through the AuthExplicit() method, it would only return a boolean. Is this the right way to authenticate? Or is there something else needed to invoke?

What does 'the Post method' refer to? What is the 'it' that would only return a boolean?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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