简体   繁体   English

Dialogflow API v2 集成问题

[英]Dialogflow API v2 integration issue

I am using Dialogflow v1 on android but I want to use V2 because it provides more features and because the V1 will be deprecated on October 23, 2019. However I can't integrate API V2 to my codes .我在 android 上使用Dialogflow v1 ,但我想使用V2,因为它提供了更多功能,并且 V1 将于 2019 年 10 月 23 日弃用。但是我无法将 API V2 集成到我的代码中 Here is the V1 code:这是V1代码:

private void initChatbot() {
    final AIConfiguration config = new AIConfiguration("Here put Client access token",
            AIConfiguration.SupportedLanguages.English,
            AIConfiguration.RecognitionEngine.System);
    aiDataService = new AIDataService(this, config);
    customAIServiceContext = AIServiceContextBuilder.buildFromSessionId(uuid);// helps to create new session whenever app restarts
    aiRequest = new AIRequest();
}

Here is the V2 code:这是V2代码:

private void initV2Chatbot() {
    try {
        InputStream stream = getResources().openRawResource(R.raw.test_agent_credentials);
        GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
        String projectId = ((ServiceAccountCredentials)credentials).getProjectId();

        SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
        SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
        sessionsClient = SessionsClient.create(sessionsSettings);
        session = SessionName.of(projectId, uuid);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

When I wrote this code for V2 got this error:当我为V2编写此代码时出现此错误:

error: cannot find symbol variable test_agent_credentials

I actually don't know what is test_agent_credentials and why I should use it.我实际上不知道什么是 test_agent_credentials 以及为什么我应该使用它。 Can anybody tell me where I should put my console.cloud.google Dialogflow İntegration Key Id?有人能告诉我应该把我的 console.cloud.google Dialogflow İntegration Key Id 放在哪里吗?

Here is the Gradle:这是 Gradle:

apply plugin: 'com.android.application'

android {
    buildToolsVersion "28.0.3"
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.tyagiabhinav.dialogflowchat"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    repositories {
        mavenCentral()
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    // Java V2
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/INDEX.LIST'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.apis:google-api-services-oauth2:v1-rev145-1.25.0'


    // Dialogflow SDK dependencies
    implementation 'ai.api:sdk:2.0.7@aar'
    implementation 'ai.api:libai:1.6.12'

    // Java V2
    implementation 'com.google.cloud:google-cloud-dialogflow:0.67.0-alpha'
    // for Remote Procedure Call to avoid "No functional channel service provider found" error while creating SessionsClient
    implementation 'io.grpc:grpc-okhttp:1.15.1'
}

R.raw.test_agent_credentials refers to the test_agent_credentials.json file that is presumed to be in the raw resource folder. R.raw.test_agent_credentials是指假定位于原始资源文件夹中的test_agent_credentials.json文件。 This JSON file contains the full credentials file that you downloaded when you created the Dialogflow Integration service account .此 JSON 文件包含您在创建 Dialogflow 集成服务帐户时下载的完整凭据文件。

Ignore the section about using the key.忽略有关使用密钥的部分。 It uses the gloud command to generate the key to use in the auth header.它使用gloud命令生成要在 auth 标头中使用的密钥。 You're doing this with the GoogleCredentials.fromStream() call.您正在通过GoogleCredentials.fromStream()调用执行此操作。

The important part was from step 10 before where you save the JSON key file and make it available to Android as a raw resource.重要的部分是在步骤 10 之前保存 JSON 密钥文件并将其作为原始资源提供给 Android 的部分。 See this information about Android resources and, in particular, the section about raw resources.请参阅有关 Android 资源的此信息,特别是有关原始资源的部分。 Raw resources should be placed under the "resource" folder in a sub-folder called "raw".原始资源应放置在名为“raw”的子文件夹中的“resource”文件夹下。 The name of the file you save there should match the part of the resource name after R.raw without the "json" extension.您保存在那里的文件的名称应该与R.raw之后没有“json”扩展名的资源名称部分匹配。

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

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