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 รหัสสําหรับรายชื่อติดต่อนี้ ซึ่งแอปพลิเคชันสร้างขึ้นและถือว่าเป็นโทเค็นที่คลุมเครือ เขียนได้
imageUrls[] list ชุดของ URL รูปภาพที่จะแสดงสําหรับรายชื่อติดต่อ รายชื่อติดต่อส่วนใหญ่จะมีรูปภาพเดียว แต่รายชื่อติดต่อ "กลุ่ม" อาจมี URL รูปภาพได้สูงสุด 8 รายการ และจะมีการปรับขนาดและครอบตัดเป็นโมเสคบนไคลเอ็นต์ เขียนได้
พร็อพเพอร์ตี้ที่ไม่บังคับ
acceptCommands[] list รายการคําสั่งเมนูเสียงที่ผู้ติดต่อสามารถจัดการได้ Glass จะแสดงรายชื่อติดต่อได้สูงสุด 3 รายการสําหรับคําสั่งเมนูเสียงแต่ละรายการ ถ้ามีมากกว่านั้น รายชื่อติดต่อ 3 รายการที่มี priority สูงสุดจะแสดงคําสั่งนั้น เขียนได้
acceptTypes[] list รายการประเภท MIME ที่รายชื่อติดต่อรองรับ รายชื่อติดต่อจะแสดงให้ผู้ใช้เห็นหากประเภทการยอมรับตรงกับไฟล์แนบประเภทใดก็ได้ในรายการ หากไม่ได้ยอมรับ TypeType รายชื่อติดต่อจะแสดงขึ้นสําหรับรายการทั้งหมด เขียนได้
phoneNumber string หมายเลขโทรศัพท์หลักของรายชื่อติดต่อ ซึ่งอาจเป็นหมายเลขที่มีคุณสมบัติครบถ้วน โดยใช้รหัสประเทศของการโทรและรหัสพื้นที่ หรือหมายเลขในพื้นที่ เขียนได้
priority unsigned integer ลําดับความสําคัญของรายชื่อติดต่อเพื่อกําหนดลําดับรายชื่อติดต่อ รายชื่อติดต่อที่มีลําดับความสําคัญสูงกว่าจะแสดงก่อนรายชื่อติดต่อที่มีลําดับความสําคัญต่ํากว่า เขียนได้
speakableName string ชื่อของรายชื่อติดต่อนี้เนื่องจากควรออกเสียง หากจําเป็นต้องพูดชื่อของรายชื่อติดต่อนี้โดยเป็นส่วนหนึ่งของเมนูที่มีการออกเสียงด้วยเสียง ระบบจะใช้ชื่อนี้เป็นการออกเสียงที่คาดไว้ ซึ่งเป็นประโยชน์สําหรับชื่อผู้ติดต่อที่มีอักขระที่ออกเสียงไม่ได้ หรือตัวสะกดที่แสดงไม่มีการออกเสียง เขียนได้
type string ประเภทของรายชื่อติดต่อนี้ ใช้สําหรับจัดเรียงใน UI ค่าที่อนุญาตคือ
  • INDIVIDUAL - แสดงถึงบุคคลธรรมดา นี่คือค่าเริ่มต้น
  • GROUP - แสดงถึงคนมากกว่า 1 คน
เขียนได้

คำตอบ

หากใช้วิธีนี้ วิธีนี้จะแสดงทรัพยากรรายชื่อติดต่อในส่วนเนื้อหาของการตอบกลับ

ตัวอย่าง

หมายเหตุ: ตัวอย่างโค้ดที่มีสำหรับวิธีการนี้ไม่ได้แสดงถึงภาษาโปรแกรมที่รองรับทั้งหมด (ดูรายการภาษาที่รองรับได้ในหน้าไลบรารีของไคลเอ็นต์)

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