Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

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

Open
prokulit06 opened this issue Apr 4, 2019 · 5 comments
Open

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

prokulit06 opened this issue Apr 4, 2019 · 5 comments

Comments

@prokulit06
Copy link

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));
    }
}

}

/**

  • Authenticates the gRPC channel using the specified {@link GoogleCredentials}.
    */
    private static class GoogleCredentialsInterceptor implements ClientInterceptor {

    private final Credentials mCredentials;

    private Metadata mCached;

    private Map<String, List> mLastMetadata;

    GoogleCredentialsInterceptor(Credentials credentials) {
    mCredentials = credentials;
    }

    @OverRide
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
    final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
    next.newCall(method, callOptions)) {
    @OverRide
    protected void checkedStart(Listener responseListener, Metadata headers)
    throws StatusException {
    Metadata cachedSaved;
    URI uri = serviceUri(next, method);
    synchronized (this) {
    Map<String, List> latestMetadata = getRequestMetadata(uri);
    if (mLastMetadata == null || mLastMetadata != latestMetadata) {
    mLastMetadata = latestMetadata;
    mCached = toHeaders(mLastMetadata);
    }
    cachedSaved = mCached;
    }
    headers.merge(cachedSaved);
    delegate().start(responseListener, headers);
    }
    };
    }

    /**

    • Generate a JWT-specific service URI. The URI is simply an identifier with enough
    • information for a service to know that the JWT was intended for it. The URI will
    • commonly be verified with a simple string equality check.
      */
      private URI serviceUri(Channel channel, MethodDescriptor method)
      throws StatusException {
      String authority = channel.authority();
      if (authority == null) {
      throw Status.UNAUTHENTICATED
      .withDescription("Channel has no authority")
      .asException();
      }
      // Always use HTTPS, by definition.
      final String scheme = "https";
      final int defaultPort = 443;
      String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName());
      URI uri;
      try {
      uri = new URI(scheme, authority, path, null, null);
      } catch (URISyntaxException e) {
      throw Status.UNAUTHENTICATED
      .withDescription("Unable to construct service URI for auth")
      .withCause(e).asException();
      }
      // The default port must not be present. Alternative ports should be present.
      if (uri.getPort() == defaultPort) {
      uri = removePort(uri);
      }
      return uri;
      }

    private URI removePort(URI uri) throws StatusException {
    try {
    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), -1 /* port */,
    uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
    throw Status.UNAUTHENTICATED
    .withDescription("Unable to construct service URI after removing port")
    .withCause(e).asException();
    }
    }

    private Map<String, List> getRequestMetadata(URI uri) throws StatusException {
    try {
    return mCredentials.getRequestMetadata(uri);
    } catch (IOException e) {
    throw Status.UNAUTHENTICATED.withCause(e).asException();
    }
    }

    private static Metadata toHeaders(Map<String, List> metadata) {
    Metadata headers = new Metadata();
    if (metadata != null) {
    for (String key : metadata.keySet()) {
    Metadata.Key headerKey = Metadata.Key.of(
    key, Metadata.ASCII_STRING_MARSHALLER);
    for (String value : metadata.get(key)) {
    headers.put(headerKey, value);
    }
    }
    }
    return headers;
    }

}

@prokulit06
Copy link
Author

please help me :(

@ravinderpayal
Copy link

Hey @prokulit06 Did you find any solution?

@viet97
Copy link

viet97 commented Oct 7, 2019

same problem , any solution ???

@prokulit06
Copy link
Author

@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
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants