C# Dialogflow V2 api的不工作示例
在C#SDK文档中遇到的问题可在此处找到: 没有对方法ToChannelCredentials()的引用。 我们无法将SDK连接到dialogflow,即使使用空白项目也是如此。此方法是否仍然存在或已弃用C# Dialogflow V2 api的不工作示例,c#,dialogflow-es,C#,Dialogflow Es,在C#SDK文档中遇到的问题可在此处找到: 没有对方法ToChannelCredentials()的引用。 我们无法将SDK连接到dialogflow,即使使用空白项目也是如此。此方法是否仍然存在或已弃用 using Google.Cloud.Dialogflow.V2; using Google.Apis.Auth.OAuth2; using Grpc.Auth; using Grpc.Core; ... GoogleCredential cred = GoogleCredential.Fr
using Google.Cloud.Dialogflow.V2;
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
using Grpc.Core;
...
GoogleCredential cred = GoogleCredential.FromFile("/path/to/credentials.json");
Channel channel = new Channel(
SessionsClient.DefaultEndpoint.Host, SessionsClient.DefaultEndpoint.Port, cred.ToChannelCredentials());
SessionsClient client = SessionsClient.Create(channel);
...
// Shutdown the channel when it is no longer required.
channel.ShutdownAsync().Wait();
您是否尝试过使用服务帐户私钥连接?(Json文件) 遵循以下步骤(C#中的工作示例)
在#7:如何/在何处实例化
\u hostingEnvironment
?@Shiasu sama下的示例中,它被注入到构造函数(DI)中,我在该构造函数中调用DialogflowManagerWhat\u contentRootPath
应设置为?这些用于获取json密钥文件的实际位置,我刚刚注意到管理器中没有使用webRoothPath,因此您可以忽略它,但是_contentRootPath返回包含应用程序内容文件的目录的绝对路径
public class DialogflowManager {
private string _userID;
private string _webRootPath;
private string _contentRootPath;
private string _projectId;
private SessionsClient _sessionsClient;
private SessionName _sessionName;
public DialogflowManager(string userID, string webRootPath, string contentRootPath, string projectId) {
_userID = userID;
_webRootPath = webRootPath;
_contentRootPath = contentRootPath;
_projectId = projectId;
SetEnvironmentVariable();
}
private void SetEnvironmentVariable() {
try {
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", _contentRootPath + "\\Keys\\{THE_DOWNLOADED_JSON_FILE_HERE}.json");
} catch (ArgumentNullException) {
throw;
} catch (ArgumentException) {
throw;
} catch (SecurityException) {
throw;
}
}
private async Task CreateSession() {
// Create client
_sessionsClient = await SessionsClient.CreateAsync();
// Initialize request argument(s)
_sessionName = new SessionName(_projectId, _userID);
}
public async Task < QueryResult > CheckIntent(string userInput, string LanguageCode = "en") {
await CreateSession();
QueryInput queryInput = new QueryInput();
var queryText = new TextInput();
queryText.Text = userInput;
queryText.LanguageCode = LanguageCode;
queryInput.Text = queryText;
// Make the request
DetectIntentResponse response = await _sessionsClient.DetectIntentAsync(_sessionName, queryInput);
return response.QueryResult;
}
}
DialogflowManager dialogflow = new DialogflowManager("{INSERT_USER_ID}",
_hostingEnvironment.WebRootPath,
_hostingEnvironment.ContentRootPath,
"{INSERT_AGENT_ID");
var dialogflowQueryResult = await dialogflow.CheckIntent("{INSERT_USER_INPUT}");