GoogleCloudPlatform / android-docs-samples

Apache License 2.0
373 stars 594 forks source link

authenticate on Google Cloud Speech To Text API beta version? #97

Open prokulit06 opened 5 years ago

prokulit06 commented 5 years ago

I tried using the official version released and the authentication works. Then I'll try the the Beta version but it doesn't work. So, whats the problem with this? Is there a new Key in Beta version or is there something wrong of my method?

This is how I implement the Google Cloud Speech To Text API beta version:

implementation group: 'com.google.api.grpc', name: 'grpc-google-cloud-speech-v1p1beta1', version: '0.6.0' implementation group: 'com.google.api.grpc', name: 'proto-google-cloud-speech-v1p1beta1', version: '0.50.0' implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.3.1' When I tried using the Beta version, this is the error that I encountered.

Process: com.example.ezminute, PID: 25333 java.lang.AbstractMethodError: abstract method "java.util.concurrent.ScheduledExecutorService io.grpc.internal.ClientTransportFactory.getScheduledExecutorService()" at io.grpc.internal.CallCredentialsApplyingTransportFactory.getScheduledExecutorService(CallCredentialsApplyingTransportFactory.java:52) at io.grpc.internal.ManagedChannelImpl.(ManagedChannelImpl.java:569) at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:440) at com.example.ezminute.CloudSpeechService$AccessTokenTask.onPostExecute(CloudSpeechService.java:422) at com.example.ezminute.CloudSpeechService$AccessTokenTask.onPostExecute(CloudSpeechService.java:375) at android.os.AsyncTask.finish(AsyncTask.java:695) at android.os.AsyncTask.-wrap1(Unknown Source:0) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) And these is the method that I used in authenticating the Google Cloud Beta Version.

private class AccessTokenTask extends AsyncTask<Void, Void, AccessToken> {

@Override
protected AccessToken doInBackground(Void... voids) {
    final SharedPreferences prefs =
            getSharedPreferences(PREFS, Context.MODE_PRIVATE);
    String tokenValue = prefs.getString(PREF_ACCESS_TOKEN_VALUE, null);
    long expirationTime = prefs.getLong(PREF_ACCESS_TOKEN_EXPIRATION_TIME, -1);

    // Check if the current token is still valid for a while
    if (tokenValue != null && expirationTime > 0) {
        if (expirationTime
                > System.currentTimeMillis() + ACCESS_TOKEN_EXPIRATION_TOLERANCE) {
            return new AccessToken(tokenValue, new Date(expirationTime));
        }
    }

    // ***** WARNING *****
    // In this sample, we load the credential from a JSON file stored in a raw resource
    // folder of this client app. You should never do this in your app. Instead, store
    // the file in your server and obtain an access token from there.
    // *******************
    final InputStream stream = getResources().openRawResource(R.raw.credential);
    try {
        final GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
                .createScoped(SCOPE);
        final AccessToken token = credentials.refreshAccessToken();
        prefs.edit()
                .putString(PREF_ACCESS_TOKEN_VALUE, token.getTokenValue())
                .putLong(PREF_ACCESS_TOKEN_EXPIRATION_TIME,
                        token.getExpirationTime().getTime())
                .apply();
        return token;
    } catch (IOException e) {
        Log.e(TAG, "Failed to obtain access token.", e);
    }
    return null;
}

@Override
protected void onPostExecute(AccessToken accessToken) {
    mAccessTokenTask = null;
    final ManagedChannel channel = new OkHttpChannelProvider()
            .builderForAddress(HOSTNAME, PORT)
            .nameResolverFactory(new DnsNameResolverProvider())
            .intercept(new GoogleCredentialsInterceptor(new GoogleCredentials(accessToken)
                    .createScoped(SCOPE)))
            .build();
    mApi = SpeechGrpc.newStub(channel);

    // Schedule access token refresh before it expires
    if (mHandler != null) {
        mHandler.postDelayed(mFetchAccessTokenRunnable,
                Math.max(accessToken.getExpirationTime().getTime()
                        - System.currentTimeMillis()
                        - ACCESS_TOKEN_FETCH_MARGIN, ACCESS_TOKEN_EXPIRATION_TOLERANCE));
    }
}

}

/**

}

prokulit06 commented 5 years ago

please help me :(

ravinderpayal commented 5 years ago

Hey @prokulit06 Did you find any solution?

viet97 commented 5 years ago

same problem , any solution ???

prokulit06 commented 5 years ago

@viet97 i solved this by upgrading the version of the beta of google speech api you can search that in google and protobuf java and grpc just try upgrade to new latest version

prokulit06 commented 5 years ago

@ravinderpayal yes by upgrading the version on it in grpc speech to text api and protobuf java