|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import com.google.api.client.auth.oauth2.Credential; |
| 16 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 17 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 18 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 19 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 20 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 21 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 22 | +import com.google.api.client.json.JsonFactory; |
| 23 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 24 | +import com.google.api.client.util.store.FileDataStoreFactory; |
| 25 | +import com.google.api.services.docs.v1.Docs; |
| 26 | +import com.google.api.services.docs.v1.DocsScopes; |
| 27 | +import com.google.api.services.docs.v1.model.Document; |
| 28 | +import com.google.gson.Gson; |
| 29 | +import com.google.gson.GsonBuilder; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.io.InputStreamReader; |
| 33 | +import java.security.GeneralSecurityException; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +// [START docs_output_json] |
| 38 | +/** |
| 39 | + * OutputJSON prints the JSON representation of a Google Doc. |
| 40 | + */ |
| 41 | +public class OutputJSON { |
| 42 | + private static final String APPLICATION_NAME = "Google Docs API Document Contents"; |
| 43 | + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
| 44 | + private static final String TOKENS_DIRECTORY_PATH = "tokens"; |
| 45 | + private static final String DOCUMENT_ID = "YOUR_DOCUMENT_ID"; |
| 46 | + |
| 47 | + /** |
| 48 | + * Global instance of the scopes required by this sample. If modifying these scopes, delete |
| 49 | + * your previously saved tokens/ folder. |
| 50 | + */ |
| 51 | + private static final List<String> SCOPES = |
| 52 | + Collections.singletonList(DocsScopes.DOCUMENTS_READONLY); |
| 53 | + |
| 54 | + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates an authorized Credential object. |
| 58 | + * |
| 59 | + * @param HTTP_TRANSPORT The network HTTP Transport. |
| 60 | + * @return An authorized Credential object. |
| 61 | + * @throws IOException If the credentials.json file cannot be found. |
| 62 | + */ |
| 63 | + private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) |
| 64 | + throws IOException { |
| 65 | + // Load client secrets. |
| 66 | + InputStream in = OutputJSON.class.getResourceAsStream(CREDENTIALS_FILE_PATH); |
| 67 | + GoogleClientSecrets credentials = |
| 68 | + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
| 69 | + |
| 70 | + // Build flow and trigger user authorization request. |
| 71 | + GoogleAuthorizationCodeFlow flow = |
| 72 | + new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials, SCOPES) |
| 73 | + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) |
| 74 | + .setAccessType("offline") |
| 75 | + .build(); |
| 76 | + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); |
| 77 | + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String... args) throws IOException, GeneralSecurityException { |
| 81 | + // Build a new authorized API client service. |
| 82 | + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
| 83 | + Docs docsService = |
| 84 | + new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) |
| 85 | + .setApplicationName(APPLICATION_NAME) |
| 86 | + .build(); |
| 87 | + |
| 88 | + Document response = docsService.documents().get(DOCUMENT_ID).execute(); |
| 89 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 90 | + System.out.println(gson.toJson(response)); |
| 91 | + } |
| 92 | +} |
| 93 | +// [END docs_output_json] |
0 commit comments