From 5961fb29bb56a6995ceb616d81147225a16793aa Mon Sep 17 00:00:00 2001 From: Anu Date: Fri, 12 Apr 2019 14:30:09 -0700 Subject: [PATCH 1/3] Create Output as JSON Java sample --- docs/outputJson/outputJson.java | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 docs/outputJson/outputJson.java diff --git a/docs/outputJson/outputJson.java b/docs/outputJson/outputJson.java new file mode 100644 index 00000000..3c9977ed --- /dev/null +++ b/docs/outputJson/outputJson.java @@ -0,0 +1,74 @@ +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.api.services.docs.v1.Docs; +import com.google.api.services.docs.v1.DocsScopes; +import com.google.api.services.docs.v1.model.Document; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.security.GeneralSecurityException; +import java.util.Collections; +import java.util.List; + +public class DocsQuickstart { + private static final String APPLICATION_NAME = "Google Docs API Document Contents"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static final String TOKENS_DIRECTORY_PATH = "tokens"; + private static final String DOCUMENT_ID = "YOUR_DOCUMENT_ID"; + + /** + * Global instance of the scopes required by this quickstart. If modifying these scopes, delete + * your previously saved tokens/ folder. + */ + private static final List SCOPES = + Collections.singletonList(DocsScopes.DOCUMENTS_READONLY); + + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; + + /** + * Creates an authorized Credential object. + * + * @param HTTP_TRANSPORT The network HTTP Transport. + * @return An authorized Credential object. + * @throws IOException If the credentials.json file cannot be found. + */ + private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) + throws IOException { + // Load client secrets. + InputStream in = DocsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); + GoogleClientSecrets credentials = + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); + + // Build flow and trigger user authorization request. + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials, SCOPES) + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) + .setAccessType("offline") + .build(); + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + } + + public static void main(String... args) throws IOException, GeneralSecurityException { + // Build a new authorized API client service. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Docs docsService = + new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) + .setApplicationName(APPLICATION_NAME) + .build(); + + Document response = docsService.documents().get(DOCUMENT_ID).execute(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + System.out.println(gson.toJson(response)); + } +} From 894cd916c3193e0d11bd5383ac52be576362f44d Mon Sep 17 00:00:00 2001 From: asrivas Date: Sun, 11 Aug 2019 22:39:14 -0400 Subject: [PATCH 2/3] Make OutputJSON runnable --- docs/outputJSON/build.gradle | 18 ++++ docs/outputJSON/settings.gradle | 1 + docs/outputJSON/src/main/java/OutputJSON.java | 88 +++++++++++++++++++ docs/outputJson/outputJson.java | 74 ---------------- 4 files changed, 107 insertions(+), 74 deletions(-) create mode 100644 docs/outputJSON/build.gradle create mode 100644 docs/outputJSON/settings.gradle create mode 100644 docs/outputJSON/src/main/java/OutputJSON.java delete mode 100644 docs/outputJson/outputJson.java diff --git a/docs/outputJSON/build.gradle b/docs/outputJSON/build.gradle new file mode 100644 index 00000000..401c8edd --- /dev/null +++ b/docs/outputJSON/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'java' +apply plugin: 'application' + +mainClassName = 'OutputJSON' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 +version = '1.0' + +repositories { + mavenCentral() +} + +dependencies { + compile 'com.google.api-client:google-api-client:1.23.0' + compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' + compile 'com.google.apis:google-api-services-docs:v1-rev20190128-1.28.0' + compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1' +} diff --git a/docs/outputJSON/settings.gradle b/docs/outputJSON/settings.gradle new file mode 100644 index 00000000..53938545 --- /dev/null +++ b/docs/outputJSON/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'outputJSON' diff --git a/docs/outputJSON/src/main/java/OutputJSON.java b/docs/outputJSON/src/main/java/OutputJSON.java new file mode 100644 index 00000000..625af774 --- /dev/null +++ b/docs/outputJSON/src/main/java/OutputJSON.java @@ -0,0 +1,88 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.api.services.docs.v1.Docs; +import com.google.api.services.docs.v1.DocsScopes; +import com.google.api.services.docs.v1.model.Document; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.security.GeneralSecurityException; +import java.util.Collections; +import java.util.List; + +public class OutputJSON { + private static final String APPLICATION_NAME = "Google Docs API Document Contents"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static final String TOKENS_DIRECTORY_PATH = "tokens"; + private static final String DOCUMENT_ID = "YOUR_DOCUMENT_ID"; + + /** + * Global instance of the scopes required by this sample. If modifying these scopes, delete + * your previously saved tokens/ folder. + */ + private static final List SCOPES = + Collections.singletonList(DocsScopes.DOCUMENTS_READONLY); + + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; + + /** + * Creates an authorized Credential object. + * + * @param HTTP_TRANSPORT The network HTTP Transport. + * @return An authorized Credential object. + * @throws IOException If the credentials.json file cannot be found. + */ + private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) + throws IOException { + // Load client secrets. + InputStream in = OutputJSON.class.getResourceAsStream(CREDENTIALS_FILE_PATH); + GoogleClientSecrets credentials = + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); + + // Build flow and trigger user authorization request. + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials, SCOPES) + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) + .setAccessType("offline") + .build(); + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + } + + public static void main(String... args) throws IOException, GeneralSecurityException { + // Build a new authorized API client service. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Docs docsService = + new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) + .setApplicationName(APPLICATION_NAME) + .build(); + + Document response = docsService.documents().get(DOCUMENT_ID).execute(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + System.out.println(gson.toJson(response)); + } +} diff --git a/docs/outputJson/outputJson.java b/docs/outputJson/outputJson.java deleted file mode 100644 index 3c9977ed..00000000 --- a/docs/outputJson/outputJson.java +++ /dev/null @@ -1,74 +0,0 @@ -import com.google.api.client.auth.oauth2.Credential; -import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; -import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; -import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; -import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.client.util.store.FileDataStoreFactory; -import com.google.api.services.docs.v1.Docs; -import com.google.api.services.docs.v1.DocsScopes; -import com.google.api.services.docs.v1.model.Document; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.security.GeneralSecurityException; -import java.util.Collections; -import java.util.List; - -public class DocsQuickstart { - private static final String APPLICATION_NAME = "Google Docs API Document Contents"; - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); - private static final String TOKENS_DIRECTORY_PATH = "tokens"; - private static final String DOCUMENT_ID = "YOUR_DOCUMENT_ID"; - - /** - * Global instance of the scopes required by this quickstart. If modifying these scopes, delete - * your previously saved tokens/ folder. - */ - private static final List SCOPES = - Collections.singletonList(DocsScopes.DOCUMENTS_READONLY); - - private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; - - /** - * Creates an authorized Credential object. - * - * @param HTTP_TRANSPORT The network HTTP Transport. - * @return An authorized Credential object. - * @throws IOException If the credentials.json file cannot be found. - */ - private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) - throws IOException { - // Load client secrets. - InputStream in = DocsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); - GoogleClientSecrets credentials = - GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); - - // Build flow and trigger user authorization request. - GoogleAuthorizationCodeFlow flow = - new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials, SCOPES) - .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) - .setAccessType("offline") - .build(); - LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); - return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); - } - - public static void main(String... args) throws IOException, GeneralSecurityException { - // Build a new authorized API client service. - final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - Docs docsService = - new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) - .setApplicationName(APPLICATION_NAME) - .build(); - - Document response = docsService.documents().get(DOCUMENT_ID).execute(); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - System.out.println(gson.toJson(response)); - } -} From c7a706eec7674b6d9fb27fba22b98a6c8caa3945 Mon Sep 17 00:00:00 2001 From: asrivas Date: Sun, 11 Aug 2019 23:05:25 -0400 Subject: [PATCH 3/3] Add class level comment and region tags --- docs/outputJSON/src/main/java/OutputJSON.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/outputJSON/src/main/java/OutputJSON.java b/docs/outputJSON/src/main/java/OutputJSON.java index 625af774..d64769ad 100644 --- a/docs/outputJSON/src/main/java/OutputJSON.java +++ b/docs/outputJSON/src/main/java/OutputJSON.java @@ -34,6 +34,10 @@ import java.util.Collections; import java.util.List; +// [START docs_output_json] +/** + * OutputJSON prints the JSON representation of a Google Doc. + */ public class OutputJSON { private static final String APPLICATION_NAME = "Google Docs API Document Contents"; private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); @@ -86,3 +90,4 @@ public static void main(String... args) throws IOException, GeneralSecurityExcep System.out.println(gson.toJson(response)); } } +// [END docs_output_json]