Cần phải uỷ quyền
Chèn một địa chỉ liên hệ mới. Xem ví dụ.
Yêu cầu
Yêu cầu HTTP
POST https://www.googleapis.com/mirror/v1/contacts
Ủy quyền
Yêu cầu này cần có sự cho phép có phạm vi sau (đọc thêm về quy trình xác thực và uỷ quyền).
Phạm vi |
---|
https://www.googleapis.com/auth/glass.timeline |
Nội dung yêu cầu
Trong nội dung yêu cầu, hãy cung cấp Tài nguyên danh bạ với các thuộc tính sau:
Tên tài sản | Giá trị | Mô tả | Ghi chú |
---|---|---|---|
Thuộc tính bắt buộc | |||
acceptCommands[].type |
string |
Loại thao tác tương ứng với lệnh này. Các giá trị được phép là:
|
có thể ghi |
displayName |
string |
Tên hiển thị cho người liên hệ này. | có thể ghi |
id |
string |
Mã nhận dạng cho người liên hệ này. Mã này do ứng dụng tạo ra và được coi là mã thông báo mờ. | có thể ghi |
imageUrls[] |
list |
Tập hợp URL hình ảnh hiển thị cho một người liên hệ. Hầu hết địa chỉ liên hệ sẽ có một hình ảnh duy nhất, nhưng có một "nhóm" người liên hệ có thể bao gồm tối đa 8 URL hình ảnh. Các URL này sẽ được đổi kích thước và cắt thành một tranh mosaic trên máy khách. | có thể ghi |
Thuộc tính không bắt buộc | |||
acceptCommands[] |
list |
Danh sách các lệnh trong trình đơn thoại mà một người liên hệ có thể xử lý. Ứng dụng Glass hiển thị tối đa 3 địa chỉ liên hệ cho mỗi lệnh trên trình đơn bằng giọng nói. Nếu nhiều hơn giá trị đó, 3 địa chỉ liên hệ có priority cao nhất sẽ hiển thị cho lệnh cụ thể đó. |
có thể ghi |
acceptTypes[] |
list |
Danh sách các loại MIME mà một người liên hệ hỗ trợ. Thông tin liên hệ này sẽ được hiển thị với người dùng nếu có bất kỳ giá chấp nhận nào khớp với bất kỳ loại tệp đính kèm nào trên mục. Nếu không có mạng AcceptTypes nào, địa chỉ liên hệ này sẽ hiển thị cho tất cả các mục. | có thể ghi |
phoneNumber |
string |
Số điện thoại chính của người liên hệ. Đây có thể là số điện thoại đủ điều kiện, có mã gọi quốc gia và mã vùng hoặc một số điện thoại tại địa phương. | có thể ghi |
priority |
unsigned integer |
Mức độ ưu tiên cho người liên hệ để xác định thứ tự trong danh sách người liên hệ. Người liên hệ có mức độ ưu tiên cao hơn sẽ hiển thị trước người liên hệ có mức độ ưu tiên thấp hơn. | có thể ghi |
speakableName |
string |
Tên của người liên hệ này (phải được phát âm). Nếu phải đọc tên của người liên hệ này trong trình đơn phân định giọng nói, thì tên này sẽ được dùng làm cách phát âm dự kiến. Điều này rất hữu ích cho những tên người liên hệ có các ký tự không phát âm được hoặc những tên có cách viết hiển thị không đúng phiên âm. | có thể ghi |
type |
string |
Loại cho người liên hệ này. Quy tắc này được dùng để sắp xếp trong giao diện người dùng. Các giá trị được phép là:
|
có thể ghi |
Phản hồi
Nếu thành công, phương thức này sẽ trả về một Tài nguyên danh bạ trong nội dung phản hồi.
Ví dụ
Lưu ý: Các đoạn mã mẫu của phương thức này không phải là ví dụ cho mọi ngôn ngữ lập trình được hỗ trợ (xem trang thông tin về các thư viện dùng cho ứng dụng để biết danh sách các ngôn ngữ được hỗ trợ).
Java
Dùng thư viện ứng dụng 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
Dùng thư viện ứng dụng.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
Dùng thư viện ứng dụng 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
Dùng thư viện ứng dụng 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
Dùng thư viện ứng dụng 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
Dùng Thư viện ứng dụng 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 thô
Không sử dụng thư viện ứng dụng.
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"] }