需要授权
插入新联系人。 查看示例。
请求
HTTP 请求
POST https://www.googleapis.com/mirror/v1/contacts
授权
此请求需要获得下列范围的授权(详细了解身份验证和授权)。
| 范围 |
|---|
https://www.googleapis.com/auth/glass.timeline |
请求正文
在请求正文中,提供具有以下属性的 Contacts 资源:
| 属性名称 | 值 | 说明 | 备注 |
|---|---|---|---|
| 必需属性 | |||
acceptCommands[].type |
string |
与此命令对应的操作类型。允许的值包括:
|
可写入 |
displayName |
string |
要显示此联系人的姓名。 | 可写入 |
id |
string |
此联系人的 ID。它由应用生成,被视为不透明令牌。 | 可写入 |
imageUrls[] |
list |
要为联系人显示的一组图片网址。大多数联系人只有一张图片,但是“群组”联系人最多可包含 8 个图片网址,它们会在客户端上调整大小并剪裁成拼接图。 | 可写入 |
| 可选属性 | |||
acceptCommands[] |
list |
联系人可处理的语音菜单命令的列表。Glass 会针对每条语音菜单命令最多显示 3 位联系人。如果超过 1 个,针对该特定命令将显示 priority 最高的三个联系人。 |
可写入 |
acceptTypes[] |
list |
联系人支持的 MIME 类型的列表。如果联系人的任何一种 acceptTypes 与该项的任何附件类型匹配,该联系人便会向用户显示。如果未提供 acceptType,则将针对所有项显示联系人。 | 可写入 |
phoneNumber |
string |
联系人的主要电话号码。此号码可以是包含国家/地区呼叫代码和区号的完全限定号码,也可以是本地号码。 | 可写入 |
priority |
unsigned integer |
用于确定联系人列表中顺序的联系人优先级。优先级较高的联系人会先于优先级较低的联系人显示。 | 可写入 |
speakableName |
string |
此联系人的姓名(应发音)。如果此联系人姓名在语音消除歧义菜单中必须说出,则会按照预期发音使用此姓名。这对联系人姓名包含不发音的字符或者显示拼写不同于发音的联系人姓名非常有用。 | 可写入 |
type |
string |
此联系人的类型。用于在界面中排序。允许的值包括:
|
可写入 |
响应
如果成功,此方法将在响应正文中返回一项 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
使用 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"] }