|
| 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