Timeline: update

需要授权

就地更新时间轴项。 查看示例

此方法支持 /upload URI,并接受具备以下特征的已上传媒体:

  • 文件大小上限:10MB
  • 接受的媒体 MIME 类型audio/* , image/* , video/*

请求

HTTP 请求

此方法通过两个单独的 URI 提供媒体上传功能。如需了解详情,请参阅有关媒体上传的文档。

  • 上传 URI,用于媒体上传请求:
    PUT https://www.googleapis.com/upload/mirror/v1/timeline/id
  • 元数据 URI,适用于仅包含元数据的请求:
    PUT https://www.googleapis.com/mirror/v1/timeline/id

参数

参数名称 说明
路径参数
id string 时间轴项的 ID。
必需的查询参数
uploadType string 针对 /upload URI 的上传请求的上传类型。 可接受的值包括: <ph type="x-smartling-placeholder">
    </ph>
  • media - 简单上传。仅上传媒体内容,而不上传任何元数据。
  • multipart - 多部分上传。在单个请求中同时上传媒体内容及其元数据。
  • resumable - 可续传上传。以可续传方式上传文件,使用至少包含两个请求的一系列请求,其中第一个请求包含元数据。

授权

此请求需要获得以下至少一个范围的授权(详细了解身份验证和授权)。

范围
https://www.googleapis.com/auth/glass.timeline
https://www.googleapis.com/auth/glass.location

请求正文

在请求正文中,提供具有以下属性的 Timeline 资源作为元数据。如需了解详情,请参阅有关媒体上传的文档。

属性名称 说明 备注
可选属性
bundleId string 此项目的软件包 ID。服务可以指定 bundleId 以将多项进行分组。它们显示在设备的单个顶级项下。 可写入
canonicalUrl string 指向时间轴项所表示数据的规范/高质量版本的规范网址。 可写入
creator nested object 创建此项的用户或群组。 可写入
displayTime datetime 在时间轴中查看此内容时应显示的时间,格式遵循 RFC 3339 标准。该用户的时间轴是按显示时间按时间顺序排序的,因此这也将确定相应项在时间轴中的显示位置。如果该服务未设置,则显示时间默认为 updated 时间。 可写入
html string 此项的 HTML 内容。如果为某个项同时提供了 texthtml,则会在时间轴中呈现 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 的商品
在套装子时间轴中,会显示的商品:
  • 将相关“bundleIdisBundleCover”设置为 false 的商品
可写入
location nested object 与此内容相关联的地理位置。 可写入
notification nested object 控制此项的通知在设备上的显示方式。如果缺少这些信息,则不会生成任何通知。 可写入
notification.deliveryTime datetime 应发送通知的时间。 可写入
notification.level string 说明通知的重要性。允许的值包括:
  • DEFAULT - 具有默认重要性的通知。系统会播放提示音以提醒用户。
可写入
recipients[] list 此内容的共享对象或群组列表。 可写入
sourceItemId string 不透明字符串,可用于将时间轴项映射到您自己的服务中的数据。 可写入
speakableText string 此项内容的可朗读版本。与 READ_ALOUD 菜单项搭配使用时,可以使用此字段提供文本,让大声朗读出来更加清晰,或者为 Glass 上直观显示的内容提供更详细的信息。

Glassware 还应指定 speechableType 字段,如果其他上下文有用,例如用户要求系统朗读通知后,系统会在此文本之前读出该字段。
可写入
speakableType string 此项目的类型的可朗读说明。如果其他上下文有用,例如,当用户请求在收到通知后大声朗读项目时,系统会在用户读取项目内容之前通知用户。

这应该是个简短的名词短语,例如“电子邮件”“短信”或“每日星球新闻动态”。

建议通过 Glassware 为每个时间轴项填充此字段(即使相应项不包含 SpeakableText 或文本),以便用户无需查看屏幕即可了解相应项的类型。
可写入
text string 此项的文字内容。 可写入
title string 此项的标题。 可写入

响应

如果成功,此方法将在响应正文中返回时间轴资源

示例

注意:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。

Java

使用 Java 客户端库

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;

public class MyClass {
  // ...

  /**
   * Update a timeline item in the user's Glass with an optional notification and attachment.
   * 
   * @param service Authorized Mirror service.
   * @param itemId ID of the timeline item to update.
   * @param newText timeline item's text.
   * @param newNotificationLevel Optional notification level, supported values
   *        are {@code null} and "AUDIO_ONLY".
   * @return Updated timeline item on success, {@code null} otherwise.
   */
  public static TimelineItem updateTimelineItem(Mirror service, String itemId, String newText,
      String newNotificationLevel) {
    try {
      // First retrieve the timeline item from the API.
      TimelineItem timelineItem = service.timeline().get(itemId).execute();
      // Update the timeline item's metadata.
      timelineItem.setText(newText);
      if (newNotificationLevel != null && newNotificationLevel.length() > 0) {
        timelineItem.setNotification(new NotificationConfig().setLevel(newNotificationLevel));
      } else {
        timelineItem.setNotification(null);
      }
      return service.timeline().update(itemId, 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>
  /// Update a timeline item in the user's Glass with an optional
  /// notification and attachment.
  /// </summary>
  /// <param name='service'>Authorized Mirror service.</param>
  /// <param name='itemId'>ID of the timeline item to update.</param>
  /// <param name='newText'>Timeline Item's text content.</param>
  /// <param name='newNotificationLevel'>
  /// Optional notification level, supported values are null and
  /// "AUDIO_ONLY".
  /// </param>
  /// <returns>
  /// Updated timeline item on success, null otherwise.
  /// </returns>
  public static TimelineItem UpdateTimelineItem(MirrorService service,
      String itemId, String newText, String newNotificationLevel) {
    try {
      // First retrieve the timeline item from the API.
      TimelineItem timelineItem = service.Timeline.Get(itemId).Fetch();
      // Update the timeline item's metadata.
      timelineItem.Text = newText;
      if (!String.IsNullOrEmpty(newNotificationLevel)) {
        timelineItem.Notification = new NotificationConfig() {
          Level = newNotificationLevel
        };
      } else {
        timelineItem.Notification = null;
      }
      return service.Timeline.Update(timelineItem, itemId).Fetch();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }

  // ...
}

PHP

使用 PHP 客户端库

/**
 * Update a timeline item in the user's Glass with an optional
 * notification and attachment.
 *
 * @param Google_MirrorSservice $service Authorized Mirror service.
 * @param string $itemId ID of the timeline item to update.
 * @param string $newText timeline item's text (can be HTML content).
 * @param string $newNotificationLevel Optional notification level,
 *               supported values are {@code null} and "AUDIO_ONLY".
 * @return Google_TimelineItem Updated timeline item on success,
 *         null otherwise.
 */
function updateTimelineItem($service, $itemId, $newText, $newNotificationLevel) {
  try {
    $timelineItem = $service->timeline->get($itemId);
    $timelineItem->setText($newText);
    if ($notificationlevel != null) {
      $notification = new Google_NotificationConfig();
      $notification->setLevel($newNotificationLevel);
      $timelineItem->setNotification($notification);
    } else {
      $timelineItem->setNotification(null);
    }
    return $service->timeline->update($itemId, $timelineItem);
  } 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 update_timeline_item(service, item_id, new_text, 
                         new_notification_level=None):
  """Update a timeline item in the user's Glass.

  Args:
    service: Authorized Mirror service.
    item_id: ID of the timeline item to update.
    new_text: Timeline item's text (can be HTML content).
    new_content_type: Optional attachment's content type (supported content
                      types are 'image/*', 'video/*' and 'audio/*').
    new_attachment: Optional attachment as data string.
    new_notification_level: Optional notification level, supported values are
                            None and 'AUDIO_ONLY'.
  Returns:
    Updated timeline item on success, None otherwise.
  """
  try:
    # First retrieve the timeline item from the API.
    timeline_item = service.timeline().get(id=item_id).execute()
    # Update the timeline item's metadata.
    timeline_item['text'] = new_text
    if new_notification_level:
      timeline_item['notification'] = {'level': new_notification_level}
    elif 'notification' in timeline_item:
      timeline_item.pop('notification')
    return service.timeline().update(id=item_id, body=timeline_item).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
    return None

Ruby

使用 Ruby 客户端库

##
# Update a timeline item in the user's glass.
#
# @param [Google::APIClient] client
#   Authorized client instance.
# @param [String] item_id
#   ID of the timeline item to update.
# @param [String] new_text
#   Timeline item's new text.
# @param [String] new_notification_level
#   Optional new notification level, supported values are nil and 'AUDIO_ONLY'.
# @return [Google::APIClient::Schema::Mirror::V1::TimelineItem]
#   Updated timeline item instance if successful, nil otherwise.
def update_timeline_item(client, item_id, new_text, new_notification_level)
  mirror = client.discovered_api('mirror', 'v1')
  result = client.execute(
    :api_method => mirror.timeline.get,
    :parameters => { 'id' => item_id })
  if result.success?
    timeline_item = result.data
    timeline_item.text = new_text
    if new_notification_level
      timeline_item.notification = { 'level' => new_notification_level }
    else
      timeline_item.notification = nil
    end
    result = client.execute(
      :api_method => mirror.timeline.update,
      :body_object => timeline_item,
      :parameters => { 'id' => item_id })
    if result.success?
      return result.data
    end
  end
  puts "An error occurred: #{result.data['error']['message']}"
end

Go

使用 Go 客户端库

import (
	"code.google.com/p/google-api-go-client/mirror/v1"
	"fmt"
)

// UpdateTimelineItem updates a timeline item in the user's timeline with an
// optional notification and attachment.
func UpdateTimelineItem(g *mirror.Service, itemId string, newNotificationLevel string) (
	*mirror.TimelineItem, error) {
	t, err := g.Timeline.Get(itemId).Do()
	if err != nil {
		fmt.Printf("An error occurred: %v\n", err)
		return nil, err
	}
	t.Text = newText
	if newNotificationLevel != "" {
		t.Notification = &mirror.NotificationConfig{Level: newNotificationLevel}
	} else {
		t.Notification = nil
	}
	r, err := g.Timeline.Update(itemId, t).Do()
	if err != nil {
		fmt.Printf("An error occurred: %v\n", err)
		return nil, err
	}
	return r, nil
}

原始 HTTP

不使用客户端库。

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

{ "text": "Hello world" }