Dialogflow es 使用Google.api.DialogFlow.V2验证请求

Dialogflow es 使用Google.api.DialogFlow.V2验证请求,dialogflow-es,google-api-client,Dialogflow Es,Google Api Client,我已经将我的C#DialogFlow客户端从Google.Cloud.DialogFlow.V2升级到Google.api.DialogFlow.V2 然而,当连接到DialogFlow时,我一直收到一个401错误 这是我的密码: Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", cloudKeyFile); var response = new Google.Apis.Dialogflow.v2.Di

我已经将我的C#DialogFlow客户端从Google.Cloud.DialogFlow.V2升级到Google.api.DialogFlow.V2 然而,当连接到DialogFlow时,我一直收到一个401错误

这是我的密码:

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", cloudKeyFile);
var response = new 
  Google.Apis.Dialogflow.v2.DialogflowService().Projects.Agent.Sessions.DetectIntent(
            new Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2DetectIntentRequest
            {
                QueryInput = new Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2QueryInput
                {
                    Text = new Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2TextInput
                    {
                        Text = queryText,
                        LanguageCode = languageCode
                    }
                }
            },
            $"projects/{ProjectId}/agent/sessions/{sessionId}")
            .Execute();
错误:

Google.api.Requests.RequestError 请求缺少所需的身份验证凭据。预期的OAuth 2访问令牌、登录cookie或其他有效身份验证凭据。看见[401]

注意:cloudKeyFile是一个有效的auth2密钥文件,可与以前的框架一起使用。(Google.Cloud.DialogFlow.V2)

有人能指导我做什么吗


Thnx-prevance

Dialogflow的API身份验证从v1大幅更改为v2。您需要使用具有适当作用域的OAuth2或OAuth2服务帐户(
https://www.googleapis.com/auth/cloud-platform
)和角色(
Dialogflow API Admin
Dialogflow API Client
,或
Dialogflow API Reader

资料来源:


Ok通过编程方式向请求添加cred找到了解决方案,如下所示:

var creds = GoogleCredential.FromFile(cloudKeyFile);
var scopedCreds = creds.CreateScoped(DialogflowService.Scope.CloudPlatform);
var response = new DialogflowService(new BaseClientService.Initializer
        {
            HttpClientInitializer = scopedCreds,
            ApplicationName = ProjectId
        }).Projects.Agent.Sessions.DetectIntent(
            new GoogleCloudDialogflowV2DetectIntentRequest
            {
                QueryInput = new GoogleCloudDialogflowV2QueryInput
                {
                    Text = new GoogleCloudDialogflowV2TextInput
                    {
                        Text = queryText,
                        LanguageCode = languageCode
                    }
                }
            },
            $"projects/{ProjectId}/agent/sessions/{sessionId}")
            .Execute();
注意-请记住添加相关的作用域标志

带来清晰的示例:

根据Godsayer的回答,我在使用c#webclient时通过几行代码成功地实现了这一点

首先,确保您已经创建了一个Google服务帐户,并授予它DialogFlow的相关权限。我需要获得意图、话语等,因此授予它DialogFlow API管理

然后,在服务帐户中,我创建了一个新的Json密钥并将其下载,并将其存储在我应用程序的本地目录中

然后,我在visual studio中安装了Google.api.Dialogflow.v2 nuget包

在我的控制台应用程序中,我添加了以下代码行,我成功了

using Google.Apis.Auth.OAuth2;
using Google.Apis.Dialogflow.v2;

var credentials = GoogleCredential.FromFile(@"C:\pathtofile\abc123.json");
var scopedCredentials = credentials.CreateScoped(DialogflowService.Scope.CloudPlatform);
    _oAuthToken = scopedCredentials.UnderlyingCredential.GetAccessTokenForRequestAsync().Result;
WebClient webclient = new WebClient();
webclient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
webclient.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {_oAuthToken}");

其他答案对我来说很有用,但不起作用,可能是因为API改变了。这就是我的工作原理:

            var dialogFlowConfigurationBytes = BlobManager.GetBytesByBlobUrl("your json path"); // get bytes from file using BlobManager class (utility class created by me, you could get the stream directly)

            var credentials = GoogleCredential.FromStream(new MemoryStream(dialogFlowConfigurationBytes));
            Channel channel = new Channel(IntentsClient.DefaultEndpoint.Host, IntentsClient.DefaultEndpoint.Port, credentials.ToChannelCredentials());
            var client = SessionsClient.Create(channel);

            foreach (var text in texts)
            {
                var response = client.DetectIntent(
                    session: new SessionName(dialogFlowConfiguration.ProjectId, sessionId),
                    queryInput: new QueryInput()
                    {
                        Text = new TextInput()
                        {
                            Text = text,
                            LanguageCode = languageCode
                        }
                    }
                );
            }

Thanx,但您指的是“非官方的”Google.Cloud.DialogFlow.V2 lib,它通过使用Enviromen变量身份验证工作得很好-如您提到的第一个链接所述。我需要关于全新的“官方”lib Google.api.Dialogflow.v2的帮助,它显然以另一种方式进行身份验证。。。