Search code examples
androidgoogle-cloud-platformgcloudgoogle-speech-apigoogle-cloud-speech

How multiple users use google cloud speech at the same time


I'm building an app that uses Google Cloud Speech.

I have a Google Service account key in my app, and I use it to call the API.

It works well when used by one user, but does not work when multiple users use it at the same time.

For example, only one user is available or all are unavailable.

The rights of the service account key are project owner.

I think it's a service account key issue... How do I fix it?

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

    @Override
    protected AccessToken doInBackground(Void... voids) {

        final SharedPreferences prefs = mContext.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));
            }
        }

        final InputStream stream = mContext.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(GOOGLE_API_HOSTNAME, GOOGLE_API_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));
        }
    }
}

This code is the code that calls 'credential.json' file on Android and gets 'Access token'.

The server for this app is python and communicates via http.

https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech/Speech

The description in the link above tells you to delegate the authentication to the server.

I want to write that part with python code. What should I do?


Solution

  • In the link you provided in the description, they suggest you to read first the basic authentication concepts document. In your case, use a service account for the Android application.

    I understand that you have already been able to provide end user credentials to a Google Cloud Platform API, as for example Cloud Speech API.

    If you want to authenticate multiple users to your application you should use instead Firebase authentication. The link contains a brief explanation and a tutorial.

    There are several Python client libraries for GCP that you can use, depending on what operations do you want to perform on the server. And regarding Python authentication on the server side, this documentation shows how the authentication for Google Cloud Storage works (have this example in mind as a reference).