Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method Credential.refreshToken calls TokenResponseException.from which throws NullPointerException #904

Closed
wonderfly opened this issue Jan 10, 2015 · 12 comments
Assignees
Labels
imported priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@wonderfly
Copy link
Contributor

From KoichiSe...@gmail.com on September 18, 2014 05:36:30

Version of google-api-java-client (e.g. 1.15.0-rc)? 1.18.0-rc Java environment (e.g. Java 6, Android 2.3, App Engine)? Java 1.7.0_55-b13 on Linux Gentoo 3.12.20 Describe the problem. java.lang.NullPointerException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:247)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) How would you expect it to be fixed? Surely there has to be a cutomized runtime exception instead of NPE to figure out the cause.

Original issue: http://code.google.com/p/google-api-java-client/issues/detail?id=904

@wonderfly wonderfly added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. imported priority: p2 Moderately-important priority. Fix may not be included in next release. 6–10 stars labels Jan 10, 2015
@wonderfly wonderfly self-assigned this Jan 10, 2015
@wonderfly
Copy link
Contributor Author

From y.c...@appresso.com on December 02, 2014 04:42:44

This problem also occurs in 1.19.0.

I have tried several error cases and found that this problem occurs in following situations:

  • invalid client_id\client_secret (i.e. strings like "foobar")
  • disabled client_secret (client_secret replace but a new one)
  • deleted application credentials

Just FYI.

@wonderfly
Copy link
Contributor Author

From ben.gilb...@smartsheet.com on December 03, 2014 13:44:42

We run into this error all the time when we're trying to obtain a service account credential using refreshToken(). The token endpoint ( https://accounts.google.com/o/oauth2/token ) properly returns a 401 error if we can't obtain a service account credential for the user, but the TokenResponseException.from() promptly blows up because the 401 response doesn't have any content.

For now we are trapping the NullPointerException explicitly, but I agree with the original bug - the underlying HTTP error should be returned cleanly rather than throwing a NPE.

@wonderfly
Copy link
Contributor Author

From premna...@gmail.com on December 16, 2014 05:40:53

I am trying to connect to the GMAIL using service account to list my emails and I am getting the same error. I am using the 1.19.0 clients. Please advise.

java.lang.NullPointerException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:92)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at TestGmailNew.main(TestGmailNew.java:72)

@wonderfly
Copy link
Contributor Author

From rajesh.s...@gmail.com on December 17, 2014 02:20:55

I am getting the following exception when i access using service account for Prediction API. I am using 1.19.0 java client api. I changed and tried with new p12 keys , but nothing worked.

java.lang.NullPointerException: null
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:92)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.rest.services.PredictionService.authorize(PredictionService.java:241)
at com.rest.services.PredictionService.trainModel(PredictionService.java:163)
at com.rest.controller.PredictionController.train(PredictionController.java:40)

@wonderfly
Copy link
Contributor Author

From KoichiSe...@gmail.com on December 17, 2014 02:29:51

The bug method TokenResponseException is here. https://code.google.com/p/google-oauth-java-client/source/browse/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/TokenResponseException.java Can we submit a fixed file into the sources?

@wonderfly
Copy link
Contributor Author

From rajesh.s...@gmail.com on December 18, 2014 06:38:16

Hi,
I have changed the code the following way and it worked. I changed the PredictionScopes.all() and no more null pointer exception with 1.19.0 version.

credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(OAUTH_EMAIL_ADDRESS)
.setServiceAccountPrivateKeyFromP12File(new File(Prediction.class.getClassLoader().getResource(OAUTH_PRIVATE_KEY_FILE).getPath()))
.setServiceAccountScopes(PredictionScopes.all()).build();
credential.refreshToken();

Regards,
Rajesh.

@wonderfly
Copy link
Contributor Author

From tharshan...@gmail.com on December 22, 2014 17:04:53

Hi,

I get this error after receiving "Authorization Code"

199.74.155.50 - - [22/Dec/2014:19:29:44 +0000] "GET /Hello-JSP/ObtainOAuth2Token HTTP/1.1" 302 -
199.74.155.50 - - [22/Dec/2014:19:29:52 +0000] "GET /Hello-JSP/oauth2callback?code=4/ati4kh_xKKrEWzwH30Gm6uU7UBObn360gUWOOfyW9aI.Ug6pyULC7CcdqPsZ5lQMyhHyuPjmlAI HTTP/1.1" 500 2470
It fails even before the access and refresh tokens were requested from my server.

Thanks

@wonderfly
Copy link
Contributor Author

From g.dilemb...@gmail.com on December 26, 2014 05:18:32

hi i m trying to access google drive using sample DriveSample offered by google.But i got same npe as above:

CONFIG: -------------- RESPONSE --------------
HTTP/1.1 401 Unauthorized
X-XSS-Protection: 1; mode=block
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Alternate-Protocol: 443:quic,p=0.02
Server: GSE
X-Content-Type-Options: nosniff
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Date: Fri, 26 Dec 2014 13:00:11 GMT
Content-Encoding: gzip
Content-Type: application/json

java.lang.NullPointerException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:92)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:545)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:562)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:519)
at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:384)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:418)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.google.api.services.samples.drive.cmdline.DriveSample.insertFile(DriveSample.java:108)
at com.google.api.services.samples.drive.cmdline.DriveSample.main(DriveSample.java:160)

Any hint?

@wonderfly
Copy link
Contributor Author

From KoichiSe...@gmail.com on December 26, 2014 22:23:20

There is clearly a bug in the com.google.api.client.auth.oauth2.TokenResponseException.from method code. https://code.google.com/p/google-oauth-java-client/source/browse/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/TokenResponseException.java It just does not check the response.getContent() return value for null before calling the com.google.api.client.json.JsonObjectParser.parseAndClose method with it. https://code.google.com/p/google-http-java-client/source/browse/google-http-client/src/main/java/com/google/api/client/json/JsonObjectParser.java Only the last method com.google.api.client.json.jackson2.JacksonFactory.createJsonParser does the check for null, which is already too late, and so it throws the NullPointerException. https://code.google.com/p/google-http-java-client/source/browse/google-http-client-jackson2/src/main/java/com/google/api/client/json/jackson2/JacksonFactory.java I guess the code should just check for null in the TokenResponseException.from method itself in the "if" section.

I will most likely get my hands on doing that myself some day soon if the original code developers will not fix it before.

@wonderfly
Copy link
Contributor Author

From wonder...@google.com on December 29, 2014 11:48:07

Status: Duplicate
Mergedinto: 100

@wonderfly
Copy link
Contributor Author

From wonder...@google.com on December 29, 2014 11:53:58

There has been a fix for this issue and it's currently under review because of the holidays. This change will go in to the default branch once it's reviewed, and will be live for all APIs in the next release.

@wonderfly
Copy link
Contributor Author

From jonathan...@gmail.com on January 03, 2015 18:43:03

What is the workaround for this? I'm doing user impersonation with offline access (long-lived tokens), and I'm constantly having to ask users to re-authorize (sort of defeats the point of offline access) because I can't get a new access token with refreshToken().

yoshi-automation added a commit that referenced this issue Mar 24, 2021
Fixes #904

Source-Author: Neenu Shaji <Neenu1995@users.noreply.github.com>
Source-Date: Wed Mar 24 15:50:02 2021 -0400
Source-Repo: googleapis/synthtool
Source-Sha: bb854b6c048619e3be4e8b8ce8ed10aa74ea78ef
Source-Link: googleapis/synthtool@bb854b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

1 participant