geank / google-api-java-client

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

GoogleJsonResponseException does not build GoogleJsonError for 401 errors #898

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-api-java-client (e.g. 1.15.0-rc)?
1.19.0 and 1.17.0-rc

Java environment (e.g. Java 6, Android 2.3, App Engine)?
Oracle Java(TM) SE Runtime Environment (build 1.8.0_11-b12), and Oracle 
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)

Describe the problem.
The Android Publisher API can return a variety of 40x error codes as 
appropriate, and in each case I can catch a `GoogleJsonResponseException` which 
has a `GoogleJsonError` member, and calling `e.getDetails().getMessage()` works 
as expected.

When performing an operation which returns an HTTP 401 error code, 
`e.getDetails()` returns `null`.  Calling `e.getStatusCode()` or 
`e.getMessage()` are still possible, but "401 Unauthorized" is not as helpful 
as having the full `GoogleJsonError` details.

I confirmed (using Charles HTTP proxy) that the JSON is correct for the HTTP 
401 response:

{ "error": { "errors": [{ "domain": "androidpublisher", "reason": "permissionDenied", "message": "The current user has insufficient permissions to perform the requested operation." }], "code": 401, "message": "The current user has insufficient permissions to perform the requested operation." } }


If I use Charles proxy to alter only the response status code from 401 to 400, 
the resulting exception contains the corresponding `GoogleJsonError` as 
expected.

How would you expect it to be fixed?
Although I understand from the Javadoc that `getDetails()` can return `null` 
(e.g. if no JSON HTTP response body is returned), I would expect the 
`GoogleJsonError` is correctly populated in all cases where valid Google API 
error JSON is present.

Original issue reported on code.google.com by chris@orr.me.uk on 19 Aug 2014 at 1:19

GoogleCodeExporter commented 9 years ago
Hi, we recently uncovered this issue internally but just haven't updated the 
documentation yet.

You are getting a null response with 401 when doing a POST/PUT using the 
NetHttpTransport. This is a working an intended (or a bug I would say) of the 
underlining un.net.www.protocol.http.HttpURLConnection class 
(http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/s
un/net/www/protocol/http/HttpURLConnection.java#1633). It closes/clears the 
connection immediately when receiving a 401/407 in streaming mode. 
NetHttpResponse.getContent() 
(https://code.google.com/p/google-http-java-client/source/browse/google-http-cli
ent/src/main/java/com/google/api/client/http/javanet/NetHttpResponse.java#75) 
would always fail in these cases because both the inputStream and errorStream 
are empty.

There's not much we can do on this matter, but an easy work around is to use 
ApacheHttpTransport. 
(https://code.google.com/p/google-http-java-client/source/browse/google-http-cli
ent/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java)

Original comment by wonder...@google.com on 22 Aug 2014 at 6:14

GoogleCodeExporter commented 9 years ago

Original comment by wonder...@google.com on 29 Oct 2014 at 10:17