需要授權
插入新聯絡人。 請點選此處查看範例。
要求
HTTP 要求
POST https://www.googleapis.com/mirror/v1/contacts
授權
此要求需要獲得下列範圍的授權 (進一步瞭解驗證和授權)。
| 範圍 |
|---|
https://www.googleapis.com/auth/glass.timeline |
要求主體
在要求主體中,提供聯絡人資源並附上以下屬性:
| 屬性名稱 | 值 | 說明 | 附註 |
|---|---|---|---|
| 必要屬性 | |||
acceptCommands[].type |
string |
這個指令對應的作業類型。允許的值包括:
|
可寫入 |
displayName |
string |
這位聯絡人的顯示名稱。 | 可寫入 |
id |
string |
這位聯絡人的 ID。這項資訊是由應用程式產生,並視為不透明權杖。 | 可寫入 |
imageUrls[] |
list |
一組聯絡人要顯示的圖片網址。大部分的聯絡人只會有一張圖片,而「群組」則只有一張圖片。聯絡人最多可包含 8 個圖片網址,且網址會經過調整,並裁剪成用戶端上的馬賽克圖片。 | 可寫入 |
| 選用屬性 | |||
acceptCommands[] |
list |
聯絡人可處理的語音選單指令清單。Glass 的每個語音選單指令最多可顯示三位聯絡人。如果超過此數量,特定指令將顯示 priority 最高的三個聯絡人。 |
可寫入 |
acceptTypes[] |
list |
聯絡人支援的 MIME 類型清單。如果聯絡人的接受 Types 符合項目上任何類型的附件,系統就會向使用者顯示聯絡人。如未提供接受類型,則所有項目都會顯示聯絡人。 | 可寫入 |
phoneNumber |
string |
聯絡人的主要電話號碼。這可以是完整的電話號碼,包含國碼和區碼,或是當地電話號碼。 | 可寫入 |
priority |
unsigned integer |
聯絡人在聯絡人清單中排序的優先順序。優先順序較高的聯絡人會顯示在優先順序較低的聯絡人之前。 | 可寫入 |
speakableName |
string |
此聯絡人的姓名 (應要唸出)。如果系統必須將這位聯絡人的姓名用於消歧選單,就會使用這個名稱成為預期的發音。如果聯絡人名稱的字元無法識別,或是顯示拼寫方式與意思不同,這項功能就非常實用。 | 可寫入 |
type |
string |
這位聯絡人的類型。用於在 UI 中排序。允許的值包括:
|
可寫入 |
回應
如果成功,這個方法會在回應主體中傳回 Contacts 資源。
範例
注意:這個方法適用的程式語言眾多,我們只在此提供部分程式碼範例,完整的支援語言清單請參閱用戶端程式庫頁面。
Java
使用 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
使用 .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
使用 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
使用 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
使用 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"] }