从Google联系人迁移到People API的小贴士:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.31.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.31.0</version>
</dependency>
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.people.v1.PeopleService;
import com.google.api.services.people.v1.PeopleServiceScopes;
import com.google.api.services.people.v1.model.ListConnectionsResponse;
import com.google.api.services.people.v1.model.Person;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class PeopleApiExample {
private static final String APPLICATION_NAME = "Your Application Name";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List<String> SCOPES = Arrays.asList(PeopleServiceScopes.CONTACTS_READONLY);
public static void main(String[] args) throws Exception {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize(httpTransport);
PeopleService peopleService = new PeopleService.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
ListConnectionsResponse connectionsResponse = peopleService.people().connections().list("people/me")
.setPersonFields("names,emailAddresses")
.execute();
List<Person> connections = connectionsResponse.getConnections();
for (Person person : connections) {
List<Person.Name> names = person.getNames();
List<Person.EmailAddress> emailAddresses = person.getEmailAddresses();
if (names != null && emailAddresses != null) {
for (Person.Name name : names) {
System.out.println("Name: " + name.getDisplayName());
}
for (Person.EmailAddress emailAddress : emailAddresses) {
System.out.println("Email: " + emailAddress.getValue());
}
}
}
}
private static Credential authorize(HttpTransport httpTransport) throws Exception {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(PeopleApiExample.class.getResourceAsStream("/client_secrets.json")));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.build();
return new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setClientSecrets(clientSecrets)
.build()
.setFromTokenResponse(flow.newTokenRequest("").setScopes(SCOPES).execute());
}
}
领取专属 10元无门槛券
手把手带您无忧上云