Contacts: update

需要授权

更新就地联系人。查看示例

请求

HTTP 请求

PUT https://www.googleapis.com/mirror/v1/contacts/id

参数

参数名称 说明
路径参数
id string 联系人的 ID。

授权

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

范围
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;

public class MyClass {
  // ...

  /**
   * Rename an existing contact for the current user.
   * 
   * @param service Authorized Mirror service.
   * @param contactId ID of the contact to rename.
   * @param newDisplayName New displayName for the contact.
   * @return Patched contact on success, {@code null} otherwise.
   */
  public static Contact renameContact(Mirror service, String contactId, String newDisplayName) {
    try {
      // Get the latest version of the contact from the API.
      Contact contact = service.contacts().get(contactId).execute();

      contact.setDisplayName(newDisplayName);
      // Send an update request to the API.
      return service. contacts().update(contactId, contact).execute();
    } catch (IOException e) {
      System.err.println("An error occurred: " + e);
      return null;
    }
  }

  // ...
}

.NET

使用 .NET 客户端库

using System;

using Google.Apis.Mirror.v1;
using Google.Apis.Mirror.v1.Data;

public class MyClass {
  // ...

  /// <summary>
  /// Rename an existing contact for the current user.
  /// </summary>
  /// <param name='service'>Authorized Mirror service.</param>
  /// <param name='contactId'>ID of the contact to rename.</param>
  /// <param name='newDisplayName'>
  /// New displayName for the contact.
  /// </param>
  /// <returns>
  /// Updated contact on success, null otherwise.
  /// </returns>
  public static Contact RRenameContact(MirrorService service,
      String contactId, String newDisplayName) {
    try {
      Contact contact = service.Contacts.Get(contactId).Fetch();
      contact.DisplayName = newDisplayName;
      return service.Contacts.Update(contact, contactId).Fetch();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }

  // ...
}

PHP

使用 PHP 客户端库

/**
 * Rename an existing contact for the current user.
 *
 * @param Google_MirrorService $service Authorized Mirror service.
 * @param string $contactId ID of the contact to rename.
 * @param string $newDisplayName New displayName for the contact.
 * @return Google_Contact Updated contact on success, null otherwise.
 */
function renameContact($service, $contactId, $newDisplayName) {
  try {
    $updatedContact = $service->contacts->get($contactId);
    $updatedContact->setDisplayName($newDisplayName);
    return $service->contacts->update($contactId, $updatedContact);
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
    return null;
  }
}

Python

使用 Python 客户端库

from apiclient import errors
# ...

def rename_contact(service, contact_id, new_display_name):
  """Rename an existing contact for the current user.

  Args:
    service: Authorized Mirror service.
    contact_id: ID of the contact to rename.
    new_display_name: New displayName for the contact.

  Returns:
    return Patched contact on success, None otherwise.
  """
  try:
    # Get the latest version of the contact from the API.
    contact = service.contacts().get(contact_id).execute()

    contact['displayName'] = new_display_name
    return service. contacts().update(
        id=contact_id, body=contact).execute()
  except errors.HttpError, e:
    print 'An error occurred: %s' % error
    return None

Ruby

使用 Ruby 客户端库

##
# Rename an existing contact for the current user.
#
# @param [Google::APIClient] client
#   Authorized client instance.
# @param [String] contact_id
#   ID of the contact to rename.
# @param [String] new_display_name
#   New displayName for the contact.
# @return [Google::APIClient::Schema::Mirror::V1::Contact]
#   Updated contact on success, nil otherwise.
def rename_contact(client, contact_id, new_display_name)
  mirror = client.discovered_api('mirror', 'v1')
  # Get the latest version of the contact from the API.
  result = client.execute(
    :api_method => mirror.contacts.get,
    :parameters => { 'id' => contact_id })
  if result.success?
    contact = result.data
    contact.display_name = new_display_name
    result = client.execute(
      :api_method => mirror.contacts.update,
      :parameters => { 'id' => contact_id },
      :body_object => contact)
    if result.success?
      return result.data
    end
  end
  puts "An error occurred: #{result.data['error']['message']}"
end

Go

使用 Go 客户端库

import (
        "code.google.com/p/google-api-go-client/mirror/v1"
        "fmt"
)

// RenameContact renames an existing contact for the current user.
func RenameContact(g *mirror.Service, contactId string,
        newDisplayName string) (*mirror.Contact, error) {
        s, err := g. Contacts.Get(contactId).Do()
        if err != nil {
                fmt.Printf("An error occurred: %v\n", err)
                return nil, err
        }
        s.DisplayName = newDisplayName
        r, err := g.Contacts.Patch(contactId, s).Do()
        if err != nil {
                fmt.Printf("An error occurred: %v\n", err)
                return nil, err
        }
        return r, nil
}

原始 HTTP

不使用客户端库。

PUT /mirror/v1/contacts/harold HTTP/1.1
Authorization: Bearer auth token
Content-Type: application/json
Content-Length: length

{
  "displayName": "Harold Penguin",
  "imageUrls": ["https://developers.google.com/glass/images/harold.jpg"]
}