zakariachowdhury / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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 reported on code.google.com by KoichiSe...@gmail.com on 18 Sep 2014 at 12:36

GoogleCodeExporter commented 9 years ago
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.

Original comment by y.c...@appresso.com on 2 Dec 2014 at 12:42

GoogleCodeExporter commented 9 years ago
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.

Original comment by ben.gilb...@smartsheet.com on 3 Dec 2014 at 9:44

GoogleCodeExporter commented 9 years ago
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)

Original comment by premna...@gmail.com on 16 Dec 2014 at 1:40

GoogleCodeExporter commented 9 years ago
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)

Original comment by rajesh.s...@gmail.com on 17 Dec 2014 at 10:20

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

Original comment by KoichiSe...@gmail.com on 17 Dec 2014 at 10:29

GoogleCodeExporter commented 9 years ago
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_FACTO
RY)
                .setServiceAccountId(OAUTH_EMAIL_ADDRESS)
                .setServiceAccountPrivateKeyFromP12File(new File(Prediction.class.getClassLoader().getResource(OAUTH_PRIVATE_KEY_FILE).getPath()))
                .setServiceAccountScopes(PredictionScopes.all()).build();
                credential.refreshToken();

Regards,
Rajesh.

Original comment by rajesh.s...@gmail.com on 18 Dec 2014 at 2:38

GoogleCodeExporter commented 9 years ago
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.Ug6
pyULC7CcdqPsZ5lQMyhHyuPjmlAI HTTP/1.1" 500 2470
 It fails even before the access and refresh tokens were requested from my server.

Thanks

Original comment by tharshan...@gmail.com on 23 Dec 2014 at 1:04

GoogleCodeExporter commented 9 years ago
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?

Original comment by g.dilemb...@gmail.com on 26 Dec 2014 at 1:18

GoogleCodeExporter commented 9 years ago
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-cl
ient/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-clie
nt/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-clie
nt-jackson2/src/main/java/com/google/api/client/json/jackson2/JacksonFactory.jav
a

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.

Original comment by KoichiSe...@gmail.com on 27 Dec 2014 at 6:23

GoogleCodeExporter commented 9 years ago
Moved to: Issue google-oauth-java-client:101

Original comment by wonder...@google.com on 29 Dec 2014 at 7:47