google-auth-library-java icon indicating copy to clipboard operation
google-auth-library-java copied to clipboard

Error 400 when trying to create a token

Open shareefalis opened this issue 6 years ago • 5 comments

Thanks for stopping by to let us know something could be better!

Please include as much information as possible:

Environment details

  • OS: Android 10.0
  • Java version N/A
  • google-auth-library-java version(s): 0.12.0-0.17.1

Steps to reproduce

  1. Upgrade speech to text sample app to latest library google-http-client
#### Stacktrace
09-14 17:38:17.151  2891  3330 E SpeechService: Failed to obtain access token.
09-14 17:38:17.151  2891  3330 E SpeechService: java.io.IOException: Error getting access token for service account: 400 Bad Request
09-14 17:38:17.151  2891  3330 E SpeechService: {
09-14 17:38:17.151  2891  3330 E SpeechService:   "error": "invalid_request",
09-14 17:38:17.151  2891  3330 E SpeechService:   "error_description": "Bad Request"
09-14 17:38:17.151  2891  3330 E SpeechService: }
09-14 17:38:17.151  2891  3330 E SpeechService: 	at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:432)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:157)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at com.google.auth.oauth2.OAuth2Credentials.refreshIfExpired(OAuth2Credentials.java:174)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at SpeechService$AccessTokenTask.doInBackground(SpeechService.java:359)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at SpeechService$AccessTokenTask.doInBackground(SpeechService.java:333)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at android.os.AsyncTask$3.call(AsyncTask.java:378)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
09-14 17:38:17.151  2891  3330 E SpeechService: 	at java.lang.Thread.run(Thread.java:919)

Code snippet

 final InputStream stream = getResources().openRawResource(R.raw.credential);
            try {
                final GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
                        .createScoped(SCOPE);
                credentials.refreshIfExpired();
                final AccessToken token = credentials.getAccessToken();
                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);
            }
Any relevant code snippet to help reproduce the issue.

External references such as API reference guides used

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

Any additional information below

I could not use an older library in the speech to text sample app because It had Apache library that isn't compatible with Android 10.0 and I tried adding credentials.refreshIfExpired();

Following these steps will guarantee the quickest resolution possible.

Thanks!

shareefalis avatar Sep 14 '19 22:09 shareefalis

oauth token retrieval function is broken in Android 10.0 SDK. My work requires to be on latest Android SDK. I had to make a barebone android 9.0 app which contains the token retrieval and stored it in a shared preference.

shareefalis avatar Sep 15 '19 04:09 shareefalis

Can you list out the dependencies and their versions, so we can help investigate?

chingor13 avatar Sep 17 '19 23:09 chingor13

google-auth-library-credentials-0.17.1 google-auth-library-oauth2-http-0.17.1 google-http-client-1.31.0 google-http-client-jackson2-1.31.0 jackson-core-2.9.9 httpclient-4.5.9 httpcore-4.4.11 okhttp-2.7.5 okio-1.6.0 grpc-stub-1.4.0 grpc-protobuf-lite-1.4.0 grpc-okhttp-1.4.0 grpc-core-1.4.0 grpc-context-1.4.0 instrumentation-api-0.4.3 opencensus-contrib-http-util-0.18.0 opencensus-api-0.18.0

shareefalis avatar Sep 17 '19 23:09 shareefalis

For me authentication works with latest http and grpc ( and Kotlin ) but same auth issue (bad request) happens when using gradle:3.5.1.

When trying different versions finally downgrade gradle:3.4.1 helped for me. Challenge to test is that issue happens only when building for "release" - no issue when building and deploy "debug" pkg. Versions working: supportLibraryVersion = '28.0.0' grpcVersion = '1.24.0' google-auth-library-oauth2-http:0.17.2 com.google.protobuf:protobuf-java:3.10.0 gradle:3.4.1

Also if previous version succeeded to authenticate credentials are still valid w/o renewal - so to see issue with "non-working auth version" need to "clear data" or uninstall app.

teronivala avatar Oct 11 '19 05:10 teronivala

Facing the same issue, Everything works in debug build but getting error 400 in the release. Any update on this?

k415hu avatar Jul 10 '20 19:07 k415hu