0

I am using google drive rest api to fetch data from drive to my app. Whenever I tried to fetch the data into inputStream, got this error:

W/System.err:  com.fasterxml.jackson.core.JsonParseException: Unexpected character ((CTRL-CHAR, code 137)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
W/System.err:  at [Source: com.google.api.client.http.javanet.NetHttpResponse$SizeValidatingInputStream@2975e41; line: 1, column: 2]

my code snippet:

GoogleCredential googleAccessCredential = new GoogleCredential().setAccessToken(accessToken);
NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, googleAccessCredential)
                                    .setApplicationName("Test")
                                    .build();
inputStream = drive.files().get("fileId").executeMediaAsInputStream();

I searched a lot but didn't get any feasible solution. I tried to change the header but got same error

inputStream = drive.files().get("fileId").setRequestHeaders(new HttpHeaders().setAccept("image/png")).executeAsInputStream();

I also tried with "application/json" and "application/octet-stream" headers.

access token is given hard coded

Simon Das
  • 79
  • 1
  • 4
  • Can you provide the content of the lines that throw you errors? Why are you trying to hardocde the access token? – ziganotschka Jun 24 '20 at 09:10
  • inputStream = drive.files().get("fileId").executeMediaAsInputStream(); this line is giving me the error. The file is a png image. As I want to show images from a specific drive to all the users, I'm giving the access token hard coded. – Simon Das Jun 25 '20 at 11:01
  • Are you aware of the fact that an access token expires after 60 minutes? Maybe it would be easier to share this specific drive with all users of interest and letting them authenticate as themselves? – ziganotschka Jun 25 '20 at 12:16
  • To exclude that the issue is token related: is it only the request `inputStream = drive.files().get("fileId").executeMediaAsInputStream();` that errors, does another request (e.g. `drive.files().list()`) work correctly? – ziganotschka Jun 25 '20 at 12:32
  • Yes, I'm aware of the fact of access token lifetime and I overcome that issue by generating new access token through refresh token when it is needed. Other requests are working fine. Only this request is giving me the error. – Simon Das Jun 25 '20 at 23:45
  • Try [executeMediaAndDownloadTo](https://developers.google.com/drive/api/v3/manage-downloads#download_a_file_stored_on_google_drive) instead, or /and check if there is an incompatibility issue with the java version you are using. – ziganotschka Jun 26 '20 at 07:54
  • Important: GoogleCredential is deprectaed as you can verify [here](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/auth/oauth2/GoogleCredential.html), so on long term you have to come up with a different solution - see [here](https://github.com/googleapis/google-api-java-client/commit/45825c4157eee2a7254817b1aae9aa63ba726726) and [here](https://stackoverflow.com/questions/57972607/what-is-the-alternative-to-the-deprecated-googlecredential) – ziganotschka Jun 26 '20 at 07:54

0 Answers0