Skip to content

Commit f724c5d

Browse files
committed
Add people quickstart
1 parent 62477ed commit f724c5d

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

README.md

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

3434
- [Quickstart](https://developers.google.com/gmail/api/quickstart/java)
3535

36+
### People
37+
38+
- [Quickstart](https://developers.google.com/people/quickstart/java)
39+
3640
### Sheets
3741

3842
- [Quickstart](https://developers.google.com/sheets/api/quickstart/java)

people/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 = 'PeopleQuickstart'
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-people:v1-rev277-1.23.0'
17+
}

people/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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 people_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.people.v1.PeopleService;
27+
import com.google.api.services.people.v1.PeopleScopes;
28+
import com.google.api.services.people.v1.model.ListConnectionsResponse;
29+
import com.google.api.services.people.v1.model.Person;
30+
31+
import java.io.IOException;
32+
import java.io.InputStream;
33+
import java.io.InputStreamReader;
34+
import java.security.GeneralSecurityException;
35+
import java.util.Collections;
36+
import java.util.Arrays;
37+
38+
public class PeopleQuickstart {
39+
private static final String APPLICATION_NAME = "Google People API Java Quickstart";
40+
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
41+
private static final String CREDENTIALS_FOLDER = "credentials"; // Directory to store user credentials.
42+
43+
/**
44+
* Global instance of the scopes required by this quickstart.
45+
* If modifying these scopes, delete your previously saved credentials/ folder.
46+
*/
47+
private static final List<String> SCOPES = Arrays.asList(PeopleScopes.CONTACTS_READONLY);
48+
private static final String CLIENT_SECRET_DIR = "client_secret.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 there is no client_secret.
55+
*/
56+
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
57+
// Load client secrets.
58+
InputStream in = PeopleQuickstart.class.getResourceAsStream(CLIENT_SECRET_DIR);
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(CREDENTIALS_FOLDER)))
65+
.setAccessType("offline")
66+
.build();
67+
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
68+
}
69+
70+
public static void main(String... args) throws IOException, GeneralSecurityException {
71+
// Build a new authorized API client service.
72+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
73+
PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
74+
.setApplicationName(APPLICATION_NAME)
75+
.build();
76+
77+
// Request 10 connections.
78+
ListConnectionsResponse response = service.people().connections()
79+
.list("people/me")
80+
.setPageSize(10)
81+
.setPersonFields("names,emailAddresses")
82+
.execute();
83+
84+
// Print display name of connections if available.
85+
List<Person> connections = response.getConnections();
86+
if (connections != null && connections.size() > 0) {
87+
for (Person person : connections) {
88+
List<Name> names = person.getNames();
89+
if (names != null && names.size() > 0) {
90+
System.out.println("Name: " + person.getNames().get(0)
91+
.getDisplayName());
92+
} else {
93+
System.out.println("No names available for connection.");
94+
}
95+
}
96+
} else {
97+
System.out.println("No connections found.");
98+
}
99+
}
100+
}
101+
// [END people_quickstart]

0 commit comments

Comments
 (0)