Contacts: insert

يتطلّب تفويضًا

إدراج جهة اتصال جديدة اطّلِع على مثال.

الطلب

طلب HTTP

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

التفويض

يتطلب هذا الطلب تفويضًا بالنطاق التالي (مزيد من المعلومات عن المصادقة والترخيص).

النطاق
https://www.googleapis.com/auth/glass.timeline

نص الطلب

في نص الطلب، قدِّم مورد "جهات اتصال Google" يتضمّن السمات التالية:

اسم الموقع القيمة الوصف ملاحظات
الخصائص المطلوبة
acceptCommands[].type string نوع العملية التي يتجاوب معها هذا الأمر. القيم المسموح بها هي:
  • TAKE_A_NOTE - يشارك عنصر مخطط زمني مع تحويل صوت الكلام إلى نص من قسم "تدوين ملاحظة". طلب القائمة الصوتية.
  • POST_AN_UPDATE - يشارك عنصر مخطط زمني مع تحويل الصوت إلى نص لخطاب المستخدم من "نشر تحديث" طلب القائمة الصوتية.
قابل للكتابة
displayName string الاسم الذي سيتم عرضه لجهة الاتصال هذه. قابل للكتابة
id string رقم تعريف لجهة الاتصال هذه. يتم إنشاء هذا بواسطة التطبيق ويتم التعامل معه كرمز مميز مبهم. قابل للكتابة
imageUrls[] list مجموعة عناوين URL للصور التي سيتم عرضها لجهة اتصال. سيكون لمعظم جهات الاتصال صورة واحدة، ولكن "مجموعة" على الأكثر 8 عناوين URL للصور، وسيتم تغيير حجمها واقتصاصها ودمجها في صورة فسيفساء على العميل. قابل للكتابة
الخصائص الاختيارية
acceptCommands[] list قائمة بأوامر القائمة الصوتية التي يمكن لجهة اتصال التعامل معها. يعرض تطبيق Glass ما يصل إلى ثلاث جهات اتصال لكل أمر من طلبات القائمة الصوتية. إذا كان هناك أكثر من ذلك، يتم عرض جهات الاتصال الثلاث ذات أعلى priority لهذا الأمر تحديدًا. قابل للكتابة
acceptTypes[] list تمثّل هذه السمة قائمة بأنواع MIME التي تتوافق مع جهة الاتصال. سيتم عرض جهة الاتصال للمستخدم إذا كان أي من علامات AcceptTypes يتطابق مع أي من أنواع المرفقات على العنصر. إذا لم يتم تقديم AcceptTypes، سيتم عرض جهة الاتصال لجميع العناصر. قابل للكتابة
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.

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