需要授權
在時間軸中插入新項目。 請點選此處查看範例。
此方法支援 /upload URI,並接受具有下列特性的上傳媒體:
- 檔案大小上限:10 MB
- 接受的媒體 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 的上傳要求類型。 可接受的值為: |
授權
這項要求需要至少擁有下列其中一個範圍的授權 (進一步瞭解驗證和授權)。
| 範圍 |
|---|
https://www.googleapis.com/auth/glass.timeline |
https://www.googleapis.com/auth/glass.location |
要求主體
在要求主體中,提供時間軸資源並附上下列屬性,做為中繼資料。詳情請參閱媒體上傳相關文件。
| 屬性名稱 | 值 | 說明 | 附註 |
|---|---|---|---|
| 選用屬性 | |||
bundleId |
string |
這個項目的軟體包 ID。服務可以指定一個 bundleId,將多個商品歸為一組。這類素材資源會顯示在裝置上的單一頂層項目底下。 | 可寫入 |
canonicalUrl |
string |
指向時間軸項目所代表資料的標準/高品質版本標準網址。 | 可寫入 |
creator |
nested object |
建立這個項目的使用者或群組。 | 可寫入 |
displayTime |
datetime |
透過時間軸查看這個項目時應顯示的時間,採用 RFC 3339 格式。這位使用者的時間軸是依顯示時間先後排序,因此也會決定該項目在時間軸中的顯示位置。如果服務未設定,顯示時間會預設為 updated 時間。 |
可寫入 |
html |
string |
這個項目的 HTML 內容。如果同時為某個項目提供 text 和 html,系統會在時間軸中顯示 html。允許的 HTML 元素 - 您可以在時間軸資訊卡中使用這些元素。
封鎖的 HTML 元素:這些元素及其內容會從 HTML 酬載中移除。
其他元素:系統會移除未列出的元素,但保留其內容。 |
可寫入 |
isBundleCover |
boolean |
這項商品是否為套裝組合封面。 如果商品已標示為套裝組合封面,則系統會將該商品設為與該項目具有相同 bundleId 的項目組合的進入點。此圖表只會顯示在主要時間軸上,而「不會顯示在已開啟的套件中」。主要時間軸會顯示的項目如下:
|
可寫入 |
location |
nested object |
與這個項目相關聯的地理位置。 | 可寫入 |
menuItems[] |
list |
使用者在時間軸中選取這個項目時,會看到選單項目清單。 | 可寫入 |
menuItems[].action |
string |
控管使用者選取選單選項時的行為。允許的值包括:
|
可寫入 |
menuItems[].id |
string |
這個選單項目的 ID。這項資訊是由應用程式產生,並視為不透明權杖。 | 可寫入 |
menuItems[].payload |
string |
一般酬載,其意義會因此 MenuItem 的動作而異。
|
可寫入 |
menuItems[].removeWhenSelected |
boolean |
如果針對 CUSTOM 選單項目設為 true,選取後就會從選單移除。 |
可寫入 |
menuItems[].values[] |
list |
針對 CUSTOM 項目,這裡列出的值清單,用於控制選單項目在各個狀態下的外觀。必須提供 DEFAULT 狀態的值。如果缺少 PENDING 或 CONFIRMED 狀態,就不會顯示。 |
可寫入 |
menuItems[].values[].displayName |
string |
選單項目的顯示名稱。如果您為內建選單項目指定這個屬性,系統就不會顯示該選單項目的預設內容相關語音指令。 | 可寫入 |
menuItems[].values[].iconUrl |
string |
與選單項目一同顯示的圖示網址。 | 可寫入 |
menuItems[].values[].state |
string |
此值適用的狀態。允許的值包括:
|
可寫入 |
notification |
nested object |
控管這個項目的通知在裝置上的顯示方式。如果缺少這項資訊,系統就不會產生任何通知。 | 可寫入 |
notification.deliveryTime |
datetime |
傳送通知的時間。 | 可寫入 |
notification.level |
string |
說明通知的重要性。允許的值包括:
|
可寫入 |
recipients[] |
list |
可與這個項目共用的使用者或群組清單。 | 可寫入 |
sourceItemId |
string |
不透明字串,可用來將時間軸項目對應至自家服務中的資料。 | 可寫入 |
speakableText |
string |
這個項目的內容可朗讀版本。除了 READ_ALOUD 選單項目外,您也可以使用這個欄位提供清楚的文字,在大聲朗讀時要清楚辨識,或是針對 Glass 顯示的視覺內容提供延伸資訊。Glassware 也應指定 readableType 欄位,如果能提供其他背景資訊相當實用,例如在通知後朗讀項目,例如使用者要求系統朗讀該項目時,系統就會直接讀出該欄位。 |
可寫入 |
speakableType |
string |
這個項目的類型說明。若使用者要求系統朗讀某項商品時,在通知後建議系統朗讀該項目,系統會在使用者閱讀該項目內容前向他們宣布。 請使用簡短且簡單的名詞片語,例如「電子郵件」、「簡訊」或「每日星球新聞最新消息」。 我們鼓勵在每個時間軸項目填入這個欄位,即使該項目未包含 readableText 或文字,也能讓使用者不必查看螢幕就知道項目的類型。 |
可寫入 |
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 用戶端程式庫。
##
# 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--