Skip to content

Commit c388bb6

Browse files
committed
Add Docs quickstart
1 parent 77bf8e3 commit c388bb6

6 files changed

Lines changed: 128 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ hs_err_pid*
4141
.classpath
4242
.project
4343
properties
44+
45+
# IDE
46+
.vscode/

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ A collection of samples that demonstrate how to call G Suite APIs in Java.
2424

2525
- [Quickstart](https://developers.google.com/classroom/quickstart/java)
2626

27+
### Docs
28+
29+
- [Quickstart](https://developers.google.com/docs/api/quickstart/java)
30+
2731
### Drive V3
2832

2933
- [Quickstart](https://developers.google.com/drive/v3/web/quickstart/java)

docs/quickstart/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: 'java'
2+
apply plugin: 'application'
3+
4+
mainClassName = 'DocsQuickstart'
5+
sourceCompatibility = 1.7
6+
targetCompatibility = 1.7
7+
version = '1.0'
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
compile 'com.google.api-client:google-api-client:1.23.0'
15+
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
16+
compile 'com.google.apis:google-api-services-docs:v1-rev20190128-1.28.0'
17+
}

docs/quickstart/settings.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This settings file was generated by the Gradle 'init' task.
3+
*
4+
* The settings file is used to specify which projects to include in your build.
5+
* In a single project build this file can be empty or even removed.
6+
*
7+
* Detailed information about configuring a multi-project build in Gradle can be found
8+
* in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html
9+
*/
10+
11+
/*
12+
// To declare projects as part of a multi-project build use the 'include' method
13+
include 'shared'
14+
include 'api'
15+
include 'services:webservice'
16+
*/
17+
18+
rootProject.name = 'quickstart'
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
// [START docs_quickstart]
16+
import com.google.api.client.auth.oauth2.Credential;
17+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
18+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
19+
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
20+
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
21+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
22+
import com.google.api.client.http.javanet.NetHttpTransport;
23+
import com.google.api.client.json.JsonFactory;
24+
import com.google.api.client.json.jackson2.JacksonFactory;
25+
import com.google.api.client.util.store.FileDataStoreFactory;
26+
import com.google.api.services.docs.v1.Docs;
27+
import com.google.api.services.docs.v1.DocsScopes;
28+
import com.google.api.services.docs.v1.model.Document;
29+
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+
public class DocsQuickstart {
38+
private static final String APPLICATION_NAME = "Google Docs API Java Quickstart";
39+
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
40+
private static final String TOKENS_DIRECTORY_PATH = "tokens";
41+
private static final String DOCUMENT_ID = "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE";
42+
43+
/**
44+
* Global instance of the scopes required by this quickstart.
45+
* If modifying these scopes, delete your previously saved tokens/ folder.
46+
*/
47+
private static final List<String> SCOPES = Collections.singletonList(DocsScopes.DOCUMENTS_READONLY);
48+
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
49+
50+
/**
51+
* Creates an authorized Credential object.
52+
* @param HTTP_TRANSPORT The network HTTP Transport.
53+
* @return An authorized Credential object.
54+
* @throws IOException If the credentials.json file cannot be found.
55+
*/
56+
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
57+
// Load client secrets.
58+
InputStream in = DocsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
59+
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
60+
61+
// Build flow and trigger user authorization request.
62+
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
63+
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
64+
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
65+
.setAccessType("offline")
66+
.build();
67+
LocalServerReceiver receier = new LocalServerReceiver.Builder().setPort(8888).build();
68+
return new AuthorizationCodeInstalledApp(flow, receier).authorize("user");
69+
}
70+
71+
public static void main(String... args) throws IOException, GeneralSecurityException {
72+
// Build a new authorized API client service.
73+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
74+
Docs service = new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
75+
.setApplicationName(APPLICATION_NAME)
76+
.build();
77+
78+
// Prints the title of the requested doc:
79+
// https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
80+
Document response = service.documents().get(DOCUMENT_ID).execute();
81+
String title = response.getTitle();
82+
83+
System.out.printf("The title of the doc is: %s\n", title);
84+
}
85+
}
86+
// [END docs_quickstart]

vault/quickstart/README

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)