Export data to Google Spreadsheet from Google App Engine (Java version)

Now I do not receive. When I google to find the sample “Export data to Google Spreadsheet from the Google App Engine”, I see many Google Conversion APIs, Google Conversion APIs, Google Spreadsheet APIs and Google Docs APIs. But then all of them are deprecated by Google when I check the Google site? So what is the most recent now, so can I use it?

=====

Now I am using the Google Drive SDK via OAuth2 to create a text file. But I have a problem with this:

this is mistake:

java.lang.NullPointerException
at java.net.URI $ Parser.parse (URI.java data 004)
at java.net.URI. (URI.java∗77)
at com.google.api.client.http.GenericUrl. (GenericUrl.java:100)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload (MediaHttpUploader.java:269)
at com.google.api.services.drive.Drive $ Files $ Insert.executeUnparsed (Drive.java:309)
at com.google.api.services.drive.Drive $ Files $ Insert.execute (Drive.java data31)
at com.company.dashboard.service.DriveService.initDoc (DriveService.java:84)

this is the code:

private GoogleCredential buildGoogleCredential(Credential credential) { try { logger.warning(oauth2Service.getClientCredential().toString()); GoogleCredential googleCredential = new GoogleCredential.Builder() .setClientSecrets(oauth2Service.getClientCredential()) .setTransport(new NetHttpTransport()) .setJsonFactory(new JacksonFactory()).build(); googleCredential.setAccessToken(credential.getAccessToken()); googleCredential.setRefreshToken(credential.getRefreshToken()); return googleCredential; } catch (IOException e) { e.printStackTrace(); } return null; } private Drive buildService(GoogleCredential credential) { return new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential).build(); } public void initDoc(HttpServletRequest req) throws Exception { User user = UserServiceFactory.getUserService().getCurrentUser(); Credential credential = oauth2Service.getStoredCredential( user.getUserId(), (CredentialStore)req.getSession().getServletContext().getAttribute(OAuth2Constant.GOOG_CREDENTIAL_STORE)); if (credential != null) { logger.warning("Using access token: " + credential.getAccessToken()); try { GoogleCredential googleCredential = buildGoogleCredential(credential); Drive service = buildService(googleCredential); if (service == null) { logger.warning("very bad!"); } File body = new File(); body.setTitle("My document"); body.setDescription("A test document"); body.setMimeType("text/plain"); java.io.File fileContent = new java.io.File("document.txt"); FileContent mediaContent = new FileContent("text/plain", fileContent); service.files().insert(body, mediaContent).execute(); //File file = service.files().insert(body, mediaContent).execute(); //System.out.println("File ID: " + file.getId()); } catch (HttpResponseException e) { if (e.getStatusCode() == 401) { logger.warning(e.getMessage()); // Credentials have been revoked. // TODO: Redirect the user to the authorization URL. throw new UnsupportedOperationException(); } } catch (IOException e) { System.out.println("An error occurred: " + e); } } 

oauth2Service.getClientCredential () returns (xxx = client ID and client secret code, not shown here)

{"web": {"client_id": "xxx.apps.googleusercontent.com", "client_secret": "xxx"}}

This is my scope:

 final static List<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/docs", "https://www.googleapis.com/auth/drive.file"); final static String AUTH_RESOURCE_LOC = "/client_secrets.json"; final static String OATH_CALLBACK = "http://localhost:8888/oauth2callback"; 

This line

service.files (). insert (body, mediaContent) .execute ();

throws a NullPointerException. Any idea what went wrong ???

P / S: Credential = com.google.api.client.auth.oauth2.Credential. I have OAuth2, everything works fine. I can get user information without problems, but not the Drive API. the service is not null, as you can see, I put the log as "very bad" and it does not appear. Thrown exception 401 means my Oauth2 is good with scopes.

=======

DARN !!!! Finally solved the problem !!! My code was absolutely right! I just included the wrong API! This should be the Drive API instead of the SDK API for the drive: /

0
source share
1 answer

You can create a csv file from GAE and download it using the disk API using ?convert=true so that it automatically converts to a Google spreadsheet:

https://developers.google.com/drive/v2/reference/files/insert

0
source

Source: https://habr.com/ru/post/1488860/


All Articles