การทำงานกับรายชื่อโดยใช้ Google People Service Client ใน Java

PeopleClient จาก com.aspose.email.services.google.people.client แพ็กเกจทำงานกับรายชื่อและกลุ่มรายชื่อผ่าน Google People API โดยเชื่อมต่อผ่าน HTTP เพื่อเรียนรู้วิธีเลือกไลบรารี HTTP ดู HTTP Clients สำหรับบริการอีเมล.

เชื่อมต่อกับ Google People

คลิเอนต์ทำการตรวจสอบสิทธิ์ด้วย OAuth 2.0 เช่นเดียวกับ GmailClient. รายละเอียดเพิ่มเติมดู เชื่อมต่อกับ Gmail.

โค้ดตัวอย่างต่อไปนี้สร้าง client และพิมพ์โปรไฟล์ของผู้ใช้ที่ตรวจสอบแล้ว ตัวอย่างโค้ดอื่นในหน้านี้ใช้ people ตัวแปรที่สร้างที่นี่

import com.aspose.email.services.google.base.auth.GoogleOAuthTokenProvider;
import com.aspose.email.services.google.people.client.PeopleClient;
import com.aspose.email.services.google.people.model.GooglePerson;

GoogleOAuthTokenProvider tokens = new GoogleOAuthTokenProvider("client-id", "client-secret", "refresh-token");

try (PeopleClient people = PeopleClient.builder().tokenProvider(tokens).build()) {
    GooglePerson me = people.me().get();
    System.out.println(me.getDisplayName() + ": " + me.getPrimaryEmail());
}

การแสดงรายการรายชื่อ

นี้ listAll เมธอดนี้อ่านทุกหน้าของรายชื่อและคืนค่าเป็นรายการเดียว:

import com.aspose.email.services.google.people.model.GooglePerson;

for (GooglePerson person : people.contacts().listAll()) {
    System.out.println(person.getResourceName() + ": " + person.getDisplayName() + ", " + person.getPrimaryEmail());
}

โดยค่าเริ่มต้น client จะร้องขอชื่อ ที่อยู่อีเมล เบอร์โทร ที่อยู่และสมาชิกของกลุ่ม เพื่อขอฟิลด์อื่นหรืออ่านหน้าละหนึ่งหน้าให้ใช้ list ด้วยชื่อฟิลด์ จำนวนหน้าและ token ของหน้า:

import com.aspose.email.services.google.base.api.GooglePagedResult;
import com.aspose.email.services.google.people.model.GooglePerson;

String pageToken = null;
do {
    GooglePagedResult<GooglePerson> page = people.contacts().list("names,emailAddresses", 100, pageToken);
    for (GooglePerson person : page.getItems()) {
        System.out.println(person.getDisplayName());
    }
    pageToken = page.getNextPageToken();
} while (pageToken != null);

การสร้างรายชื่อ

สร้าง GooglePerson อ็อบเจ็กต์ ตั้งค่าคุณสมบัติ แล้วส่งให้ create. ตัวสร้างของ GoogleEmailAddress และ GooglePhoneNumber ยอมรับค่าและประเภทของมัน วัตถุที่คืนค่ามีชื่อทรัพยากรของรายชื่อใหม่

import com.aspose.email.services.google.people.model.GoogleEmailAddress;
import com.aspose.email.services.google.people.model.GoogleName;
import com.aspose.email.services.google.people.model.GooglePerson;
import com.aspose.email.services.google.people.model.GooglePhoneNumber;
import java.util.Arrays;

GooglePerson person = new GooglePerson();
person.setNames(Arrays.asList(new GoogleName("Olena", "Shevchenko")));
person.setEmailAddresses(Arrays.asList(new GoogleEmailAddress("olena@example.com", GoogleEmailAddress.TYPE_WORK)));
person.setPhoneNumbers(Arrays.asList(new GooglePhoneNumber("+380441234567", GooglePhoneNumber.TYPE_MOBILE)));

GooglePerson created = people.contacts().create(person);
System.out.println(created.getResourceName());

การอ่านและการลบรายชื่อ

เพื่อทำงานกับรายชื่อหนึ่งรายการ ให้เรียก people พร้อมชื่อทรัพยากรของมัน:

import com.aspose.email.services.google.people.model.GooglePerson;

GooglePerson person = people.people("people/c1234567890").get();
System.out.println(person.getDisplayName());

people.people("people/c1234567890").delete();

การทำงานกับกลุ่มรายชื่อ

นี้ contactGroups เมธอดนี้คืนค่า client สำหรับแสดงรายการและสร้างกลุ่ม เพื่อทำงานกับกลุ่มหนึ่ง ให้ส่งชื่อทรัพยากรของมัน delete เมธอดนี้รับพารามิเตอร์ที่ระบุว่าจะลบรายชื่อของกลุ่มด้วยหรือไม่

import com.aspose.email.services.google.people.model.GoogleContactGroup;

for (GoogleContactGroup group : people.contactGroups().listUserGroups()) {
    System.out.println(group.getResourceName() + ": " + group.getName() + ", " + group.getMemberCount());
}

GoogleContactGroup group = new GoogleContactGroup();
group.setName("Partners");
GoogleContactGroup created = people.contactGroups().create(group);

people.contactGroups(created.getResourceName()).delete(false);

การจัดการข้อผิดพลาด

เมื่อ Google People API ส่งสถานะข้อผิดพลาด ลูกค้าจะโยน HttpClientException, ซึ่งประกอบด้วยสถานะและ response body หาก token provider ไม่สามารถรับ access token ลูกค้าจะโยน java.io.IOException, ซึ่งเป็นคลาสฐานของ HttpClientException.

import com.aspose.ext.http.HttpClientException;

try {
    people.people("people/unknown-id").get();
} catch (HttpClientException e) {
    System.out.println(e.getStatusCode() + ": " + e.getResponseBody());
}