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 เหมือนกับสินค้าดังกล่าว ข้อมูลจะแสดงในไทม์ไลน์หลักเท่านั้น โดยไม่แสดงใน Bundle ที่เปิดไว้

ในไทม์ไลน์หลัก รายการที่แสดงคือ
  • รายการที่ตั้งค่า 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

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

ข้อมูลนี้ควรเป็นวลีคำนามสั้นๆ ง่ายๆ เช่น "อีเมล" "ข้อความ" หรือ "อัปเดตข่าวดาวเคราะห์รายวัน"

เราขอแนะนำให้กลาสแวร์กรอกข้อมูลในช่องนี้สำหรับทุกรายการในไทม์ไลน์ แม้ว่ารายการนั้นจะไม่มีข้อความหรือข้อความที่พูดได้ เพื่อให้ผู้ใช้เรียนรู้ประเภทของรายการได้โดยไม่ต้องมองหน้าจอ
เขียนได้
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--