Timeline: insert

نیاز به مجوز دارد

یک مورد جدید را در جدول زمانی درج می کند. یک نمونه ببینید .

این روش از یک URI /upload پشتیبانی می کند و رسانه های آپلود شده با ویژگی های زیر را می پذیرد:

  • حداکثر حجم فایل: 10 مگابایت
  • انواع MIME رسانه قابل قبول: image/* ، audio/* ، video/*

در صورت ارائه، رسانه آپلود شده به عنوان پیوست به آیتم جدول زمانی درج می شود.

درخواست

درخواست HTTP

این روش قابلیت آپلود رسانه را از طریق دو URI مجزا فراهم می کند. برای جزئیات بیشتر، به سند مربوط به بارگذاری رسانه مراجعه کنید.

  • URI آپلود، برای درخواست های آپلود رسانه:
    POST https://www.googleapis.com/upload/mirror/v1/timeline
  • URI فراداده، برای درخواست‌های فقط فراداده:
    POST https://www.googleapis.com/mirror/v1/timeline

مولفه های

نام پارامتر ارزش شرح
پارامترهای پرس و جو مورد نیاز
uploadType string نوع درخواست آپلود به /upload URI. مقادیر قابل قبول عبارتند از:
  • media - آپلود ساده فقط رسانه را بارگذاری کنید، بدون هیچ گونه ابرداده.
  • multipart - آپلود چند قسمتی . هم رسانه و هم فراداده آن را در یک درخواست آپلود کنید.
  • resumable - Resumable upload . با استفاده از یک سری حداقل دو درخواست که در آن درخواست اول شامل ابرداده است، فایل را به روشی قابل ازسرگیری آپلود کنید.

مجوز

این درخواست به مجوز حداقل با یکی از حوزه های زیر نیاز دارد ( در مورد احراز هویت و مجوز بیشتر بخوانید ).

محدوده
https://www.googleapis.com/auth/glass.timeline
https://www.googleapis.com/auth/glass.location

درخواست بدن

در بدنه درخواست، یک منبع Timeline با ویژگی های زیر به عنوان ابرداده ارائه کنید. برای اطلاعات بیشتر، به سند مربوط به بارگذاری رسانه مراجعه کنید.

نام ملک ارزش شرح یادداشت
ویژگی های اختیاری
bundleId string شناسه بسته برای این مورد. سرویس‌ها می‌توانند یک bundleId را برای گروه‌بندی بسیاری از موارد با هم مشخص کنند. آنها در زیر یک آیتم سطح بالای دستگاه ظاهر می شوند. قابل نوشتن
canonicalUrl string یک URL متعارف که به نسخه متعارف/کیفیت بالا داده‌های نشان‌داده‌شده توسط آیتم جدول زمانی اشاره می‌کند. قابل نوشتن
creator nested object کاربر یا گروهی که این مورد را ایجاد کرده است. قابل نوشتن
displayTime datetime زمانی که باید هنگام مشاهده این مورد در جدول زمانی نمایش داده شود، که مطابق با RFC 3339 قالب‌بندی شده است. جدول زمانی این کاربر بر اساس زمان نمایش مرتب‌سازی می‌شود، بنابراین این نیز تعیین می‌کند که مورد در کجای جدول زمانی نمایش داده شود. اگر توسط سرویس تنظیم نشده باشد، زمان نمایش به طور پیش فرض روی زمان updated تنظیم می شود. قابل نوشتن
html string محتوای HTML برای این مورد. اگر هم text و هم html برای یک آیتم ارائه شده باشد، html در جدول زمانی نمایش داده می شود.

عناصر مجاز HTML - می توانید از این عناصر در کارت های جدول زمانی خود استفاده کنید.

  • سرصفحه ها: h1, h2, h3, h4, h5, h6
  • تصاویر: img
  • لیست ها: li, ol, ul
  • معناشناسی HTML5: article, aside, details, figure, figcaption, footer, header, nav, section, summary, time
  • ساختاری: blockquote, br, div, hr, p, span
  • سبک: b, big, center, em, i, u, s, small, strike, strong, style, sub, sup
  • جداول: table, tbody, td, tfoot, th, thead, tr

عناصر HTML مسدود شده : این عناصر و محتویات آنها از بارهای HTML حذف می شوند.

  • سرصفحه سند: head, title
  • جاسازی ها: audio, embed, object, source, video
  • قاب ها: frame, frameset
  • اسکریپت: applet, script

عناصر دیگر : هر عنصری که در لیست نیست حذف می شود، اما محتوای آنها حفظ می شود.

قابل نوشتن
isBundleCover boolean آیا این مورد یک جلد بسته است.

اگر یک آیتم به عنوان جلد بسته علامت گذاری شده باشد، نقطه ورود به بسته اقلامی است که همان bundleId آن آیتم را دارند. فقط در جدول زمانی اصلی نشان داده می شود - نه در بسته باز شده .

در جدول زمانی اصلی، مواردی که نشان داده می شوند عبارتند از:
  • مواردی که isBundleCover روی true تنظیم شده است
  • مواردی که bundleId ندارند
در یک جدول زمانی فرعی بسته، مواردی که نشان داده می شوند عبارتند از:
  • مواردی که bundleId مورد نظر و isBundleCover روی false تنظیم شده است
قابل نوشتن
location nested object موقعیت جغرافیایی مرتبط با این مورد. قابل نوشتن
notification nested object نحوه نمایش اعلان‌های این مورد را در دستگاه کنترل می‌کند. اگر این مورد وجود نداشته باشد، هیچ اعلانی ایجاد نخواهد شد. قابل نوشتن
notification. deliveryTime datetime زمانی که اعلان باید تحویل شود. قابل نوشتن
notification. level string توضیح می دهد که اعلان چقدر مهم است. مقادیر مجاز عبارتند از:
  • DEFAULT - اعلان های اهمیت پیش فرض. صدای زنگ برای هشدار به کاربران پخش خواهد شد.
قابل نوشتن
recipients[] list لیستی از کاربران یا گروه هایی که این مورد با آنها به اشتراک گذاشته شده است. قابل نوشتن
sourceItemId string رشته مات می‌توانید برای نگاشت یک آیتم جدول زمانی به داده‌های سرویس خودتان استفاده کنید. قابل نوشتن
speakableText string نسخه قابل گفتن محتوای این مورد. همراه با آیتم منو READ_ALOUD ، از این فیلد برای ارائه متنی که هنگام خواندن با صدای بلند واضح‌تر باشد، یا برای ارائه اطلاعات گسترده‌تر به آنچه به صورت بصری روی شیشه نمایش داده می‌شود، استفاده کنید.

Glassware همچنین باید فیلد speakableType را مشخص کند، که در مواردی که زمینه اضافی مفید است، قبل از این متن گفته می‌شود، برای مثال زمانی که کاربر درخواست می‌کند که مورد پس از یک اعلان با صدای بلند خوانده شود.
قابل نوشتن
speakableType string توضیحات قابل گفتن از نوع این مورد. در مواردی که زمینه اضافی مفید باشد، برای مثال زمانی که کاربر درخواست می کند که پس از یک اعلان، مطلب را با صدای بلند خوانده شود، این موضوع قبل از خواندن محتوای مورد به کاربر اعلام می شود.

این باید یک عبارت اسمی کوتاه و ساده مانند «ایمیل»، «پیام متنی» یا «به‌روزرسانی روزانه سیاره نیوز» باشد.

از ظروف شیشه‌ای تشویق می‌شود که این فیلد را برای هر آیتم جدول زمانی پر کنند، حتی اگر مورد حاوی speakableText یا متن نباشد تا کاربر بتواند نوع مورد را بدون نگاه کردن به صفحه یاد بگیرد.
قابل نوشتن
text string محتوای متنی این آیتم قابل نوشتن
title string عنوان این مورد. قابل نوشتن

واکنش

در صورت موفقیت آمیز بودن، این روش یک منبع Timeline را در بدنه پاسخ برمی گرداند.

مثال ها

توجه: نمونه‌های کد موجود برای این روش همه زبان‌های برنامه‌نویسی پشتیبانی‌شده را نشان نمی‌دهند (برای فهرست زبان‌های پشتیبانی‌شده به صفحه کتابخانه‌های سرویس گیرنده مراجعه کنید).

جاوا

از کتابخانه سرویس گیرنده جاوا استفاده می کند.

import com.google.api.client.http.InputStreamContent;
import com.google.api.services.mirror.Mirror;
import com.google.api.services.mirror.model.NotificationConfig;
import com.google.api.services.mirror.model.TimelineItem;

import java.io.IOException;
import java.io.InputStream;

public class MyClass {
  // ...

  /**
   * Insert a new timeline item in the user's glass with an optional
   * notification and attachment.
   * 
   * @param service Authorized Mirror service.
   * @param text timeline item's text.
   * @param contentType Optional attachment's content type (supported content
   *        types are "image/*", "video/*" and "audio/*").
   * @param attachment Optional attachment stream.
   * @param notificationLevel Optional notification level, supported values are
   *        {@code null} and "AUDIO_ONLY".
   * @return Inserted timeline item on success, {@code null} otherwise.
   */
  public static TimelineItem insertTimelineItem(Mirror service, String text, String contentType,
      InputStream attachment, String notificationLevel) {
    TimelineItem timelineItem = new TimelineItem();
    timelineItem.setText(text);
    if (notificationLevel != null && notificationLevel.length() > 0) {
      timelineItem.setNotification(new NotificationConfig().setLevel(notificationLevel));
    }
    try {
      if (contentType != null && contentType.length() > 0 && attachment != null) {
        // Insert both metadata and attachment.
        InputStreamContent mediaContent = new InputStreamContent(contentType, attachment);
        return service.timeline().insert(timelineItem, mediaContent).execute();
      } else {
        // Insert metadata only.
        return service.timeline().insert(timelineItem).execute();
      }
    } catch (IOException e) {
      System.err.println("An error occurred: " + e);
      return null;
    }
  }

  // ...
}

.خالص

از کتابخانه کلاینت دات نت استفاده می کند.

using System;
using System.IO;

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

public class MyClass {
  // ...

  /// <summary>
  /// Insert a new timeline item in the user's glass with an optional
  /// notification and attachment.
  /// </summary>
  /// <param name='service'>Authorized Mirror service.</param>
  /// <param name='text'>Timeline Item's text.</param>
  /// <param name='contentType'>
  /// Optional attachment's content type (supported content types are
  /// "image/*", "video/*" and "audio/*").
  /// </param>
  /// <param name='attachment'>Optional attachment stream</param>
  /// <param name='notificationLevel'>
  /// Optional notification level, supported values are null and
  /// "AUDIO_ONLY".
  /// </param>
  /// <returns>
  /// Inserted timeline item on success, null otherwise.
  /// </returns>
  public static TimelineItem InsertTimelineItem(MirrorService service,
      String text, String contentType, Stream attachment,
      String notificationLevel) {
    TimelineItem timelineItem = new TimelineItem();
    timelineItem.Text = text;
    if (!String.IsNullOrEmpty(notificationLevel)) {
      timelineItem.Notification = new NotificationConfig() {
        Level = notificationLevel
      };
    }
    try {
      if (!String.IsNullOrEmpty(contentType) && attachment != null) {
        // Insert both metadata and media.
        TimelineResource.InsertMediaUpload request = service.Timeline.Insert(
            timelineItem, attachment, contentType);
        request.Upload();
        return request.ResponseBody;
      } else {
        // Insert metadata only.
        return service.Timeline.Insert(timelineItem).Fetch();
      }
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }

  // ...
}

PHP

از کتابخانه مشتری PHP استفاده می کند.

/**
 * Insert a new timeline item in the user's glass with an optional
 * notification and attachment.
 *
 * @param Google_MirrorService $service Authorized Mirror service.
 * @param string $text timeline item's text.
 * @param string $contentType Optional attachment's content type (supported
 *                            content types are "image/*", "video/*"
 *                            and "audio/*").
 * @param string $attachment Optional attachment content.
 * @param string $notificationLevel Optional notification level,
 *                                  supported values are {@code null}
 *                                  and "AUDIO_ONLY".
 * @return Google_TimelineItem Inserted timeline item on success, otherwise.
 */
function insertTimelineItem($service, $text, $contentType, $attachment,
                            $notificationLevel) {
  try {
    $timelineItem = new Google_TimelineItem();
    $timelineItem->setText($text);
    if ($notificationlevel != null) {
      $notification = new Google_NotificationConfig();
      $notification->setLevel($notificationLevel);
      $timelineItem->setNotification($notification);
    }
    $optParams = array();
    if ($contentType != null && $attachment != null) {
      $optParams['data'] = $attachment;
      $optParams['mimeType'] = $contentType;
    }
    return $service->timeline->insert($timelineItem, $optParams);
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
    return null;
  }
}

پایتون

از کتابخانه کلاینت پایتون استفاده می کند.

import io

from apiclient import errors
from apiclient.http import MediaIoBaseUpload
# ...

def insert_timeline_item(service, text, content_type=None, attachment=None,
                         notification_level=None):
  """Insert a new timeline item in the user's glass.

  Args:
    service: Authorized Mirror service.
    text: timeline item's text.
    content_type: Optional attachment's content type (supported content types
                  are 'image/*', 'video/*' and 'audio/*').
    attachment: Optional attachment as data string.
    notification_level: Optional notification level, supported values are None
                        and 'AUDIO_ONLY'.

  Returns:
    Inserted timeline item on success, None otherwise.
  """
  timeline_item = {'text': text}
  media_body = None
  if notification_level:
    timeline_item['notification'] = {'level': notification_level}
  if content_type and attachment:
    media_body = MediaIoBaseUpload(
        io.BytesIO(attachment), mimetype=content_type, resumable=True)
  try:
    return service.timeline().insert(
        body=timeline_item, media_body=media_body).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

روبی

از کتابخانه کلاینت Ruby استفاده می کند.

##
# Insert a new Timeline Item in the user's glass.
#
# @param [Google::APIClient] client
#   Authorized client instance.
# @param [String] text
#   Timeline item's text.
# @param [String] content_type
#   Optional attachment's content type (supported content types are 'image/*',
#   'video/*' and 'audio/*').
# @param [String] filename
#   Optional attachment's filename.
# @param [String] notification_level
#   Optional notification level, supported values are nil and 'AUDIO_ONLY'.
# @return [Google::APIClient::Schema::Mirror::V1::TimelineItem]
#   Timeline item instance if successful, nil otherwise.
def insert_timeline_item(client, text, content_type, filename,
                         notification_level)
  mirror = client.discovered_api('mirror', 'v1')
  timeline_item = mirror.timeline.insert.request_schema.new({ 'text' => text })
  if notification_level
    timeline_item.notification = { 'level' => notification_level }
  end
  result = nil
  if filename
    media = Google::APIClient::UploadIO.new(filename, content_type)
    result = client.execute(
      :api_method => mirror.timeline.insert,
      :body_object => timeline_item,
      :media => media,
      :parameters => {
        'uploadType' => 'multipart',
        'alt' => 'json'})
  else
    result = client.execute(
      :api_method => mirror.timeline.insert,
      :body_object => timeline_item)
  end
  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"
	"io"
)

// InsertTimelineItem inserts a new timeline item in the user's glass with an
// optional notification and attachment.
func InsertTimelineItem(g *mirror.Service, text string, attachment io.Reader,
	notificationLevel string) (*mirror.TimelineItem, error) {
	t := &mirror.TimelineItem{Text: text}
	if notificationLevel != "" {
		t.Notification = &mirror.NotificationConfig{Level: notificationLevel}
	}
	req := g.Timeline.Insert(t)
	if attachment != nil {
		// Add attachment to the timeline item.
		req.Media(attachment)
	}
	r, err := req.Do()
	if err != nil {
		fmt.Printf("An error occurred: %v\n", err)
		return nil, err
	}
	return r, nil
}

HTTP خام

از کتابخانه مشتری استفاده نمی کند.

## A very simple timeline item

POST /mirror/v1/timeline HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer auth token
Content-Type: application/json
Content-Length: 26

{ "text": "Hello world" }

## A more complete timeline item with an attachment

POST /mirror/v1/timeline HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer auth token
Content-Type: multipart/related; boundary="mymultipartboundary"
Content-Length: length

--mymultipartboundary
Content-Type: application/json; charset=UTF-8

{
 "text": "Hello world",
 "menuItems": [
   { "action": "REPLY" },
   {
     "action": "CUSTOM",
     "id": "complete"
     "values": [{
       "displayName": "Complete",
       "iconUrl": "http://example.com/icons/complete.png"
     }]
   }
}
--mymultipartboundary
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

binary image data
--mymultipartboundary--