title: Cloud Speech API return code=UNAUTHENTICATED, cause =java.io.IOException: Error getting access token for service account:
body: <p>I use Google Cloud Speech API(StreamingRecognize via gRPC) for speech recognizing in my app. I got the problem with authenticate on Android devices API level &lt; 23. </p>
  
  <pre><code>V/NativeCrypto: SSL handshake aborted: ssl=0x5b0ed2d0: Failure in SSL library, usually a protocol error
                                                                   error:10000095:SSL routines:OPENSSL_internal:ERROR_PARSING_EXTENSION (third_party/openssl/boringssl/src/ssl/t1_lib.c:2336 0x5b0fcd50:0x00000001)
                                                                   error:100000be:SSL routines:OPENSSL_internal:PARSE_TLSEXT (third_party/openssl/boringssl/src/ssl/handshake_client.c:893 0x5aca7d31:0x00000000)
  
  V/NativeCrypto: SSL shutdown failed: ssl=0x5b0ed2d0: Failure in SSL library, usually a protocol error
                                                                   error:100000fa:SSL routines:OPENSSL_internal:SHUTDOWN_WHILE_IN_INIT (third_party/openssl/boringssl/src/ssl/ssl_lib.c:703 0x5aca7d31:0x00000000)
  
  V/NativeCrypto: SSL handshake aborted: ssl=0x58523fb8: Failure in SSL library, usually a protocol error
                                                                   error:1000042e:SSL routines:OPENSSL_internal:TLSV1_ALERT_PROTOCOL_VERSION (third_party/openssl/boringssl/src/ssl/tls_record.c:469 0x5b258bd8:0x00000001)
  
  V/NativeCrypto: SSL shutdown failed: ssl=0x58523fb8: Failure in SSL library, usually a protocol error
                                                                   error:100000fa:SSL routines:OPENSSL_internal:SHUTDOWN_WHILE_IN_INIT (third_party/openssl/boringssl/src/ssl/ssl_lib.c:703 0x5aca7d31:0x00000000)
  
  Status{code=UNAUTHENTICATED, description=null, cause=java.io.IOException: Error getting access token for service account: }
  </code></pre>
  
  <p>I don't get this error everytime, but only on specific case. I have two activity. Let's name A and B. </p>
  
  <p>Activity A has a button, which starts Activity B.
  Activity B communicates with Google Cloud Speech API and show results in TextView.</p>
  
  <p>I get this error after following steps(on Samsung GT-P5110 (Android 4.2.2)):</p>
  
  <ol>
  <li>Start Activity A -> press Button to start Activity B.</li>
  </ol>
  
  <p>// In first time everything is Okay. I receive result from server and show this.</p>
  
  <ol start="2">
  <li><p>Close Activity B (press Back Button or call onBackPressed or finish).</p></li>
  <li><p>Press button to start Activity B.</p></li>
  </ol>
  
  <p>// In this moment I get error, which was show above.</p>
  
  <p>I get this error on Lenovo A1000 (API 5.0.1) and Xiaomi Mi4c (5.0.1) after:</p>
  
  <ol>
  <li><p>Start Activity A -> press Button to start Activity B.</p></li>
  <li><p>Close Activity B (press Back Button or call onBackPressed or finish).</p></li>
  <li><p>Press button to start Activity B.</p></li>
  </ol>
  
  <p>// Everything is Okay</p>
  
  <ol start="4">
  <li><p>Close Activity B (press Back Button or call onBackPressed or finish).</p></li>
  <li><p>Minimize app .</p></li>
  <li><p>After 5-6 or more minutes go back to Activity A.</p></li>
  <li><p>Start Activity B.</p></li>
  </ol>
  
  <p>// In this moment I always get this error.</p>
  
  <p>After restart application everything is Okay, but error appears after reopen Activity B.</p>
  
  <p>Who knows how I can solve this problem?</p>
  
  <p>I use Play Services Dynamic Security Provider (<a href="https://github.com/grpc/grpc-java/blob/master/SECURITY.md#tls-on-android" rel="nofollow">It is required for gRPC</a>).
  I installed last version Google Play Services (9.6.83). It doesn't resolve the problem. </p>
  
  <p>I create ManagedChannel by following code:</p>
  
  <pre><code>GoogleCredentials creds = GoogleCredentials.fromStream(credentials);
      creds = creds.createScoped(OAUTH2_SCOPES);
      OkHttpChannelProvider provider = new OkHttpChannelProvider();
      OkHttpChannelBuilder builder = provider.builderForAddress(host, port);
  ManagedChannel channel = builder.intercept(new ClientAuthInterceptor(creds, Executors
              .newSingleThreadExecutor
                      ()))
              .build();
  
  private static final List&lt;String&gt; OAUTH2_SCOPES =
          Arrays.asList("https://www.googleapis.com/auth/cloud-platform");
  </code></pre>
  
  <p>Create SpeechGrpc.SpeechStub :</p>
  
  <pre><code>SpeechGrpc.SpeechStub mSpeechClient = SpeechGrpc.newStub(channel);
  </code></pre>
  
  <p>build.gradle (Module: app)</p>
  
  <pre><code>apply plugin: 'com.android.application'
  apply plugin: 'com.google.protobuf'
  
  protobuf {
  protoc {
      artifact = 'com.google.protobuf:protoc:3.0.0'
  }
  plugins {
      javalite {
          artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
      }
      grpc {
          artifact = 'io.grpc:protoc-gen-grpc-java:1.0.1'
      }
  }
  generateProtoTasks {
      all().each { task -&gt;
          task.plugins {
              javalite {}
              grpc {
                  // Options added to --grpc_out
                  option 'lite'
              }
          }
      }
  
  }
  }
  android {
  compileSdkVersion 24
  buildToolsVersion "24.0.2"
  
  dexOptions {
      javaMaxHeapSize "2048M"
  }
  
  defaultConfig {
      applicationId "someAppID"
      minSdkVersion 15
      targetSdkVersion 24
      versionCode 1
      versionName "1.0.10"
      multiDexEnabled true
  }
  buildTypes {
      release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
  }
  }
  
  def grpcVersion = '1.0.1'
  
  dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'com.google.android.gms:play-services:9.6.0'
    compile "io.grpc:grpc-okhttp:${grpcVersion}"
    compile "io.grpc:grpc-protobuf:${grpcVersion}"
    compile "io.grpc:grpc-stub:${grpcVersion}"
    compile "io.grpc:grpc-auth:${grpcVersion}"
    compile ('com.google.auth:google-auth-library-oauth2-http:0.3.0'){
        exclude module: 'httpclient'
    }
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.itextpdf:itextg:5.5.9'
    compile 'com.android.support:recyclerview-v7:24.2.1'
  }
  </code></pre>
  
  <p>build.gradle (Project)</p>
  
  <pre><code>buildscript {
  repositories {
      jcenter()
  }
  dependencies {
      classpath 'com.android.tools.build:gradle:2.2.0'
      classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
  
      // NOTE: Do not place your application dependencies here; they belong
      // in the individual module build.gradle files
  }
  }
  allprojects {
  repositories {
  //        mavenLocal()
      jcenter()
  }
  }
  
  task clean(type: Delete) {
  delete rootProject.buildDir
  }
  </code></pre>
  
  <p>thank you for your time :)</p>