Contacts: insert

需要授權

插入新聯絡人。 參閲範例

要求

HTTP 要求

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

授權

此要求需要以下範圍的授權 (進一步瞭解驗證和授權)。

範圍
https://www.googleapis.com/auth/glass.timeline

要求主體

在要求內容中,請提供聯絡資源並提供下列屬性:

資源名稱 說明 Notes
必要屬性
acceptCommands[].type string 這個指令所對應的作業類型。允許的值包括:
  • TAKE_A_NOTE - 透過「新增記事」語音選單指令,共用使用者語音轉錄的時間軸項目。
  • POST_AN_UPDATE:透過「張貼更新」語音選單指令,分享使用者語音轉錄的時間軸項目。
可寫入
displayName string 此聯絡人的顯示名稱。 可寫入
id string 此聯絡人的 ID。應用程式是由應用程式產生,而且會視為不透明的符記。 可寫入
imageUrls[] list 聯絡人的聯絡圖片網址組合。大多數的聯絡人都有一張圖片,一個「群組」聯絡人最多可包含 8 個圖片網址,而會經過調整並裁剪成用戶端的馬賽克。 可寫入
選填屬性
acceptCommands[] list 聯絡人可處理的語音選單指令清單。Glass 針對每個語音選單指令最多可顯示三個聯絡人。如果數量超過這個上限,系統會顯示該指令 priority 中三個最高的聯絡人。 可寫入
acceptTypes[] list 聯絡人支援的 MIME 類型清單。只要聯絡人的 TypeType 與項目中的任何附件類型相符,系統就會向使用者顯示該聯絡人。如果您未提供接受類型,系統就會顯示所有聯絡人的聯絡人。 可寫入
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;
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"]
}