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 類型清單。如果聯絡人的 acceptTypes 與項目中任何附件類型相符,系統就會向使用者顯示該聯絡人。如未提供接受類型,則所有項目都會顯示聯絡人。 可寫入
phoneNumber string 聯絡人的主要電話號碼。這可以是完整的電話號碼,包含國碼和區碼,或是當地電話號碼。 可寫入
priority unsigned integer 聯絡人的優先順序,用於決定在聯絡人清單中的排序。優先順序較高的聯絡人會顯示在優先順序較低的聯絡人之前。 可寫入
speakableName string 此聯絡人的姓名 (應要唸出)。如果這個聯絡人的名稱必須在語音辨識選單中朗讀,系統會使用這個名稱做為預期的發音。如果聯絡人名稱的字元無法識別,或是顯示拼字錯誤時,此功能就能派上用場。 可寫入
type string 這位聯絡人的類型。用於在 UI 中排序。允許的值包括:
  • INDIVIDUAL - 代表單人。此為預設值。
  • GROUP - 代表多名使用者,
可寫入

回應

如果成功,這個方法會在回應主體中傳回 Contacts 資源

範例

注意:這個方法適用的程式語言眾多,我們只在此提供部分程式碼範例,完整的支援語言清單請參閱用戶端程式庫頁面

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 用戶端程式庫

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