Timeline: insert

ต้องมีการให้สิทธิ์

แทรกรายการใหม่ลงในไทม์ไลน์ ดูตัวอย่าง

วิธีการนี้รองรับ URI /upload และยอมรับสื่อที่อัปโหลดซึ่งมีลักษณะต่อไปนี้

  • ขนาดไฟล์สูงสุด: 10 MB
  • ประเภท MIME ของสื่อที่ยอมรับ: image/* , audio/* , video/*

หากระบุไว้ ระบบจะแทรกสื่อที่อัปโหลดเป็นไฟล์แนบของรายการในไทม์ไลน์

ส่งคำขอ

คำขอ HTTP

วิธีนี้จะมีฟังก์ชันการอัปโหลดสื่อผ่าน URI แยกกัน 2 รายการ โปรดดูรายละเอียดเพิ่มเติมในเอกสารเกี่ยวกับการอัปโหลดสื่อ

  • URI การอัปโหลดสําหรับคําขออัปโหลดสื่อ:
    POST https://www.googleapis.com/upload/mirror/v1/timeline
  • URI ของข้อมูลเมตาสําหรับคําขอเฉพาะข้อมูลเมตาเท่านั้น:
    POST https://www.googleapis.com/mirror/v1/timeline

พารามิเตอร์

ชื่อพารามิเตอร์ ค่า คำอธิบาย
พารามิเตอร์การค้นหาที่จําเป็น
uploadType string ประเภทคําขออัปโหลดไปยัง URI /upload โดยค่าที่ยอมรับมีดังต่อไปนี้

การให้สิทธิ์

คําขอนี้ต้องมีการให้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 ขอบเขต (อ่านเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์และการให้สิทธิ์)

ขอบเขต
https://www.googleapis.com/auth/glass.timeline
https://www.googleapis.com/auth/glass.location

เนื้อหาของคำขอ

ในเนื้อหาคําขอ ให้ระบุทรัพยากรไทม์ไลน์ด้วยพร็อพเพอร์ตี้ต่อไปนี้เป็นข้อมูลเมตา สําหรับข้อมูลเพิ่มเติม โปรดดูเอกสารเกี่ยวกับการอัปโหลดสื่อ

ชื่อพร็อพเพอร์ตี้ ค่า คำอธิบาย หมายเหตุ
พร็อพเพอร์ตี้ที่ไม่บังคับ
bundleId string รหัสชุดสําหรับสินค้านี้ บริการจะระบุ BundleId เพื่อจัดกลุ่มรายการเข้าด้วยกันได้ ซึ่งจะปรากฏใต้รายการระดับบนสุดรายการเดียวในอุปกรณ์ เขียนได้
canonicalUrl string Canonical URL ที่ชี้ไปยังข้อมูล Canonical/เวอร์ชันคุณภาพสูงของข้อมูลที่แสดงโดยรายการไทม์ไลน์ เขียนได้
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 ให้ใช้ช่องนี้เพื่อระบุข้อความที่จะชัดเจนขึ้นเมื่ออ่านออกเสียง หรือเพื่อให้ข้อมูลเพิ่มเติมเกี่ยวกับสิ่งที่แสดงใน Glass

Glassware ควรระบุช่อง SpeakableType ซึ่งจะพูดก่อนข้อความนี้ในกรณีที่บริบทเพิ่มเติมมีประโยชน์ เช่น เมื่อผู้ใช้ขอให้รายการนี้อ่านออกเสียงตามการแจ้งเตือน
เขียนได้
speakableType string คําอธิบายที่พูดได้ของประเภทรายการนี้ ระบบจะแจ้งให้ผู้ใช้ทราบก่อนที่จะอ่านเนื้อหาของรายการในกรณีที่บริบทเพิ่มเติมมีประโยชน์ เช่น เมื่อผู้ใช้ขอให้รายการดังกล่าวอ่านออกเสียงหลังจากการแจ้งเตือน

ควรเป็นวลีสั้นๆ ที่เรียบง่ายและเป็นคํานาม เช่น "อีเมล" "SMS" หรือ "การอัปเดตข่าวประจําวันของดาวเคราะห์"

เราขอแนะนําให้ใช้ Glassware เพื่อเติมข้อมูลในช่องนี้สําหรับทุกรายการในไทม์ไลน์ แม้ว่ารายการดังกล่าวจะไม่มี SpeakableText หรือ SMS เพื่อให้ผู้ใช้ได้เรียนรู้ประเภทของรายการโดยไม่ต้องดูหน้าจอ
เขียนได้
text string เนื้อหาข้อความของรายการนี้ เขียนได้
title string ชื่อของรายการนี้ เขียนได้

คำตอบ

หากสําเร็จวิธีนี้จะแสดงทรัพยากรไทม์ไลน์ในส่วนการตอบกลับ

ตัวอย่าง

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

Java

ใช้ไลบรารีของไคลเอ็นต์ Java

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;
    }
  }

  // ...
}

.NET

ใช้ไลบรารีของไคลเอ็นต์ .NET

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;
  }
}

Python

ใช้ไลบรารีของไคลเอ็นต์ Python

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

ใช้ไลบรารีของไคลเอ็นต์ 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

ใช้ไลบรารีของไคลเอ็นต์ 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--