Contacts: insert

需要授权

插入新的联系人。查看示例

请求

HTTP 请求

POST https://www.googleapis.com/mirror/v1/contacts

授权

此请求需要获得下列范围的授权(详细了解身份验证和授权)。

范围
https://www.googleapis.com/auth/glass.timeline

请求正文

在请求正文中,提供具有以下属性的通讯录资源

属性名称 说明 备注
必需属性
acceptCommands[].type string 此命令对应的操作类型。允许的值为:
  • TAKE_A_NOTE - 共享包含“添加记事”语音菜单命令的用户语音转录的时间轴项。
  • POST_AN_UPDATE - 共享时间轴内容,其中包含来自“发布更新”语音菜单命令的用户语音转录内容。
可写入
displayName string 要为此联系人显示的姓名。 可写入
id string 此联系人的 ID。此令牌由应用生成,被视为不透明令牌。 可写入
imageUrls[] list 为联系人显示的一组图片网址。大多数联系人都有一张图片,但“小组”联系人最多可以包含 8 个图片网址,系统会在客户端上重新调整其大小并裁剪为拼接图。 可写入
可选属性
acceptCommands[] list 联系人可处理的语音菜单命令列表。对于每个语音指令菜单,Glass 最多会显示三个联系人。如果超过该数量,系统将显示该特定命令的 priority 最高的三个联系人。 可写入
acceptTypes[] list 联系人支持的 MIME 类型列表。如果该联系人的任一 allowTypes 与该项中的任一附件类型匹配,则系统会向用户显示该联系人。如果未提供 allowTypes,将显示所有商品的联系人信息。 可写入
phoneNumber string 联系人的主要电话号码。这可以是包含国家/地区呼叫代码和区号的完全限定号码,也可以是本地号码。 可写入
priority unsigned integer 联系人确定联系人列表中顺序的优先顺序。优先级较高的联系人会排在优先级较低的联系人前面。 可写入
speakableName string 此联系人的姓名(应该如此发音)。如果必须将该联系人的姓名包含在语音消除歧义菜单中,则该名称用作预期的发音。如果联系人姓名包含发音有误或显示的拼写不是拼音,这项功能会非常有用。 可写入
type string 此联系人的类型。这用于在界面中进行排序。允许的值为:
  • INDIVIDUAL - 代表一个人。这是默认值。
  • GROUP - 代表多人。
可写入

响应

如果成功,此方法将在响应正文中返回联系人资源

示例

注意:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。

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"]
}