ต้องมีการให้สิทธิ์
แทรกรายชื่อติดต่อใหม่ ดูตัวอย่าง
ส่งคำขอ
คำขอ HTTP
POST https://www.googleapis.com/mirror/v1/contacts
การให้สิทธิ์
คำขอนี้ต้องได้รับการให้สิทธิ์ตามขอบเขตต่อไปนี้ (อ่านเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์และการให้สิทธิ์)
ขอบเขต |
---|
https://www.googleapis.com/auth/glass.timeline |
เนื้อหาของคำขอ
ในเนื้อหาคำขอ ให้ระบุทรัพยากรรายชื่อติดต่อพร้อมด้วยพร็อพเพอร์ตี้ต่อไปนี้
ชื่อพร็อพเพอร์ตี้ | ค่า | คำอธิบาย | หมายเหตุ |
---|---|---|---|
พร็อพเพอร์ตี้ที่จำเป็น | |||
acceptCommands[].type |
string |
ประเภทของการดำเนินการตามคำสั่งนี้ ค่าที่ใช้ได้คือ
|
เขียนได้ |
displayName |
string |
ชื่อที่จะแสดงสำหรับรายชื่อติดต่อนี้ | เขียนได้ |
id |
string |
รหัสของรายชื่อติดต่อนี้ โดยแอปพลิเคชันจะสร้างขึ้นและถือเป็นโทเค็นที่คลุมเครือ | เขียนได้ |
imageUrls[] |
list |
ชุด URL รูปภาพที่จะแสดงสำหรับรายชื่อติดต่อ รายชื่อติดต่อส่วนใหญ่จะมีรูปภาพเดียว แต่มี "กลุ่ม" รายชื่อติดต่ออาจรวม URL รูปภาพได้สูงสุด 8 รายการ และข้อมูลดังกล่าวจะถูกปรับขนาดและครอบตัดเป็นภาพโมเสคบนไคลเอ็นต์ | เขียนได้ |
พร็อพเพอร์ตี้ที่ไม่บังคับ | |||
acceptCommands[] |
list |
รายการคําสั่งเมนูเสียงที่ผู้ติดต่อจัดการได้ Glass แสดงรายชื่อติดต่อได้สูงสุด 3 รายการสำหรับคำสั่งเสียงแต่ละคำสั่ง หากมีมากกว่านั้น รายชื่อติดต่อ 3 รายการที่มี priority สูงสุดจะปรากฏสำหรับคำสั่งนั้นโดยเฉพาะ |
เขียนได้ |
acceptTypes[] |
list |
รายการประเภท MIME ที่รายชื่อติดต่อรองรับ รายชื่อติดต่อจะแสดงต่อผู้ใช้ หากacceptTypes ใดก็ตามตรงกับไฟล์แนบประเภทใดก็ตามในรายการ หากไม่ได้ระบุacceptTypes รายชื่อติดต่อไว้จะแสดงขึ้นสำหรับรายการทั้งหมด | เขียนได้ |
phoneNumber |
string |
หมายเลขโทรศัพท์หลักสำหรับรายชื่อติดต่อ ซึ่งอาจเป็นหมายเลขที่สมบูรณ์ในตัวเอง พร้อมรหัสประเทศและรหัสพื้นที่ หรือหมายเลขท้องถิ่น | เขียนได้ |
priority |
unsigned integer |
ลำดับความสำคัญที่ผู้ติดต่อจะพิจารณาถึงการจัดลำดับในรายชื่อผู้ติดต่อ รายชื่อติดต่อที่มีลำดับความสำคัญสูงกว่าจะแสดงก่อนรายชื่อติดต่อที่มีลำดับความสำคัญต่ำกว่า | เขียนได้ |
speakableName |
string |
ชื่อของรายชื่อติดต่อนี้ตามที่ควรออกเสียง หากต้องพูดชื่อของผู้ติดต่อรายนี้เป็นส่วนหนึ่งของเมนูการชี้แจงด้วยเสียง ชื่อนี้จะใช้เป็นการออกเสียงที่คาดไว้ ซึ่งมีประโยชน์สำหรับชื่อผู้ติดต่อที่มีอักขระที่ไม่สามารถออกเสียงได้ หรือที่ตัวสะกดที่แสดงได้ไม่ใช่การออกเสียง | เขียนได้ |
type |
string |
ประเภทของรายชื่อติดต่อนี้ ซึ่งใช้สำหรับจัดเรียงใน UI ค่าที่ใช้ได้คือ
|
เขียนได้ |
คำตอบ
หากทำสำเร็จ เมธอดนี้จะแสดงทรัพยากร Contacts ในเนื้อหาการตอบกลับ
ตัวอย่าง
หมายเหตุ: ตัวอย่างโค้ดที่มีสำหรับวิธีการนี้ไม่ได้แสดงถึงภาษาโปรแกรมที่รองรับทั้งหมด (ดูรายการภาษาที่รองรับได้ในหน้าไลบรารีของไคลเอ็นต์)
Java
import com.google.api.services.mirror.Mirror; import com.google.api.services.mirror.model.Contact; import java.io.IOException; import java.util.Arrays; public class MyClass { // ... /** * Insert a new contact for the current user. * * @param service Authorized Mirror service. * @param contactId ID of the contact to insert. * @param displayName Display name for the contact to insert. * @param iconUrl URL of the contact's icon. * @return The inserted contact on success, {@code null} otherwise. */ public static Contact insertContact(Mirror service, String contactId, String displayName, String iconUrl) { Contact contact = new Contact(); contact.setId(contactId); contact.setDisplayName(displayName); contact.setImageUrls(Arrays.asList(iconUrl)); try { return service.contacts().insert(contact).execute(); } catch (IOException e) { System.err.println("An error occurred: " + e); return null; } } // ... }
.NET
using System; using System.Collections.Generic; using Google.Apis.Mirror.v1; using Google.Apis.Mirror.v1.Data; public class MyClass { // ... /// <summary> /// Insert a new contact for the current user. /// </summary> /// <param name='service'>Authorized Mirror service.</param> /// <param name='contactId'>ID of the contact to insert.</param> /// <param name='displayName'> /// Display name for the contact to insert. /// </param> /// <param name='iconUrl'>URL of the contact's icon.</param> /// <returns> /// The inserted contact on success, null otherwise. /// </returns> public static Contact InsertContact(MirrorService service, String contactId, String displayName, String iconUrl) { Contact contact = new Contact() { Id = contactId, DisplayName = displayName, ImageUrls = new List<String>() {iconUrl} }; try { return service.Contacts.Insert(contact).Fetch(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); return null; } } // ... }
PHP
/** * Insert a new contact for the current user. * * @param Google_MirrorService $service Authorized Mirror service. * @param string $contactId ID of the contact to insert. * @param string $displayName Display name for the contact to insert. * @param string $iconUrl URL of the contact's icon. * @return Google_Contact The inserted contact on success, null otherwise. */ function insertContact($service, $contactId, $displayName, $iconUrl) { try { $contact = new Google_Contact(); $contact->setId($contactId); $contact->setDisplayName($displayName); $contact->setImageUrls(array($iconUrl)); return $service->contacts->insert($contact); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); return null; } }
Python
from apiclient import errors
# ...
def insert_contact(service, contact_id, display_name, icon_url):
"""Insert a new contact for the current user.
Args:
service: Authorized Mirror service.
contact_id: ID of the contact to insert.
display_name: Display name for the contact to insert.
icon_url: URL of the contact's icon.
Returns:
The inserted contact on success, None otherwise.
"""
contact = {
'id': contact_id,
'displayName': display_name,
'imageUrls': [icon_url]
}
try:
service.contacts().insert(body=contact).execute()
except errors.HttpError, error:
print 'An error occurred: %s' % error
return None
Ruby
## # Insert a new contact for the current user. # # @param [Google::APIClient] client # Authorized client instance. # @param [String] contact_id # ID of the contact to insert. # @param [String] display_name # Display name for the contact to insert. # @param [String] image_url # URL of the contact's icon. # @return [Google::APIClient::Schema::Mirror::V1::Contact] # The inserted contact on success, nil otherwise. def insert_contact(client, contact_id, display_name, image_url) mirror = client.discovered_api('mirror', 'v1') contact = mirror.contacts.insert.request_schema.new({ 'id' => contact_id, 'displayName' => display_name, 'imageUrls' => [image_url] }) result = client.execute( :api_method => mirror.contacts.insert, :body_object => contact) if result.success? return result.data else puts "An error occurred: #{result.data['error']['message']}" end end
Go
import ( "code.google.com/p/google-api-go-client/mirror/v1" "fmt" ) // InsertContact inserts a new contact for the current user. func InsertContact(g *mirror.Service, contactId string, displayName string, iconUrl string) (*mirror.Contact, error) { s := &mirror.Contact{ Id: contactId, DisplayName: displayName, ImageUrls: []string{iconUrl}, } r, err := g.Contacts.Insert(s).Do() if err != nil { fmt.Printf("An error occurred: %v\n", err) return nil, err } return r, nil }
HTTP ดิบ
ไม่ได้ใช้ไลบรารีของไคลเอ็นต์
POST /mirror/v1/contacts HTTP/1.1 Authorization: Bearer auth token Content-Type: application/json Content-Length: length { "id": "harold" "displayName": "Harold Penguin", "imageUrls": ["https://developers.google.com/glass/images/harold.jpg"] }