question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Oauth2.tokeninfo IllegalArgumentException: key error

See original GitHub issue

When attempting to call Oauth2.tokeninfo() with an access token from a service key, an exception is sometimes thrown, with no clear pattern.

Code:

    private val transport = GoogleNetHttpTransport.newTrustedTransport()
    private val jsonFactory = JacksonFactory.getDefaultInstance()
...
        val credential = GoogleCredential.fromStream(privateKeyStream)
                .createScoped(Collections.singleton("https://www.googleapis.com/auth/userinfo.email"))
        val oauth = Oauth2.Builder(transport, jsonFactory, credential)
                .setApplicationName("SomeAppName")
                .build()
        val tokenInfo = oauth.tokeninfo()
                .setAccessToken(credential.accessToken)
                // This is throwing the exception
                .execute()

Much of the time, execute returns properly and tokenInfo contains the requested data. Sometimes, however, the following exception is thrown (internal package names have been replaced with foo.bar):

java.lang.IllegalArgumentException: key error
	at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:889)
	at com.google.api.client.json.JsonParser.parse(JsonParser.java:382)
	at com.google.api.client.json.JsonParser.parse(JsonParser.java:336)
	at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:166)
	at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:148)
	at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:117)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
	at com.foo.bar.GoogleAuth.validateServiceCredential(GoogleAuth.kt:104)
	at com.foo.bar.GoogleAuthTest.testServiceKey(GoogleAuthTest.kt:40)
	at com.foo.bar.GoogleAuthTest.validateTokenTest(GoogleAuthTest.kt:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: expected primitive class, but got: class com.google.api.client.googleapis.json.GoogleJsonError
	at com.google.api.client.util.Data.parsePrimitiveValue(Data.java:453)
	at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:859)
	... 37 more

Note that while this stack trace was thrown from a test case, the same behavior is seen when running it “normally”

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
chingor13commented, Aug 24, 2018

1.25.0 is released!

0reactions
yonilernercommented, Sep 21, 2018

@chingor13 Im not sure this is actually fixed. I’ve upgraded to https://mvnrepository.com/artifact/com.google.apis/google-api-services-oauth2/v2-rev141-1.25.0 and now I’m getting a different, but similar, error:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

	at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

Its pretty much the same thing; it randomly throws this error, but retrying once or twice always ends up working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using JWT, OAuth, and Separate Resource and Auth Servers
I tried spring oauth and I came across the same error : ... The resource server decrypts the token with the public key,...
Read more >
OAuth2 Autoconfig - Spring
Configuring both JWT and JWK properties will cause an error. Only one of security.oauth2.resource.jwt.key-uri (or security.oauth2.resource.jwt.key-value ) ...
Read more >
JWS + JWK in a Spring Security OAuth2 Application - Baeldung
All we have to do is indicate how the service has to validate the access token it receives, like what public key it...
Read more >
Authorization Errors | Device Access - Google Developers
When attempting to get an access or refresh token, you will get an "Invalid client" error if you provide an incorrect OAuth 2.0...
Read more >
Spring Boot OAuth2 Social Login with Google, Facebook, and ...
If the OAuth2 callback results in an error, Spring security will invoke ... the response in the form of a generic map of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found