原文地址:http://bbs.seacat.cn/thread-883-1-2.html
订阅
Mirrror API允许当用户需要具体的Timeline Item和当用户的位置发送改变时发送订阅通知(subscribe to notifications),当你订阅通知,你需要提供一个回调URL来处理通知。
接收通知
来自MirrorAPI的通知会发送一个POST请求给订阅的段节点,这里面包含一段JSON数据。
Raw HTTP
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "UPDATE",
"userToken": "harold_penguin",
"verifyToken": "random_hash_to_verify_referer",
"userActions": [
{
"type": "<TYPE>",
"payload": "<PAYLOAD>"
}
]
}
Java代码
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.mirror.model.Notification;
import java.io.IOException;
import java.io.InputStream;
// ...
publicclass MyClass {
// ...
/**
* Parse a request body into a Notification object.
*
* @param requestBody The notification payload sent by the Mirror API.
* @return Parsed notification payload if successful, {@code null} otherwise.
*/
static Notification parseNotification(InputStream requestBody) {
try {
JsonFactory jsonFactory = new JacksonFactory();
return jsonFactory.fromInputStream(requetBody, Notification.class);
} catch (IOException e) {
System.out.println("An error occurred: " + e);
returnnull;
}
}
// ...
}
Python
import json
def parse_notification(request_body):
"""Parse a request body into a notification dict.
Params:
request_body: The notification payload sent by the Mirror API as a string.
Returns:
Dict representing the notification payload.
"""
return json.load(request_body)
通知类型
Mirror API为不同事件会发送不同的通知载体。
共享timeline item
用户通过你的Glassware来共享Timeline item
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "INSERT",
"userToken": "harold_penguin",
"verifyToken": "random_hash_to_verify_referer",
"userActions": [
{
"type": "SHARE"
}
]
}
temId属性共享Timelineitem的id,你可以通过使用 Timeline.get来获得timeline item。下面一个典型的示例展示了timelineitem带照片做为附件。
{
"id": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"attachments": [
{
"contentType": "image/jpeg",
"id": "<ATTACHMENT_ID>"
}
],
"recipients": [
{
"kind": "glass#contact",
"source": "api:<SERVICE_ID>",
"id": "<CONTACT_ID>",
"displayName": "<CONTACT_DISPLAY_NAME>",
"imageUrls": [
"<CONTACT_ICON_URL>"
]
}
]
}
注意:有关更多信息,请参见 联系人(Contacts)与联系人分享内容。
回复
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "INSERT",
"userToken": "harold_penguin",
"verifyToken": "random_hash_to_verify_referer",
"userActions": [
{
"type": "REPLY"
}
]
}
inReplyTo属性设置为timelineitem的ID项回复
text属性设置为文本转录
{
"kind": "glass#timelineItem",
"id": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"inReplyTo": "3236e5b0-b282-4e00-9d7b-6b80e2f47f3d",
"text": "This is a text reply",
"recipients": [
{
"id": "CREATOR_ID",
"displayName": "CREATOR_DISPLAY_NAME",
"imageUrls": [
"CREATOR_IMAGE_URL"
]
}
]
}
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "DELETE",
"userToken": "harold_penguin",
"verifyToken": "random_hash_to_verify_referer",
"userActions": [
{
"type": "DELETE"
}
]
}
用户选择了您服务设置的一个自定义菜单项(custom menu item)
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "UPDATE",
"userToken": "harold_penguin",
"userActions": [
{
"type": "CUSTOM",
"payload": "PING"
}
]
}
位置更新
{
"collection": "locations",
"itemId": "latest",
"operation": "UPDATE",
"userToken": "harold_penguin",
"verifyToken": "random_hash_to_verify_referer"
}
当你的Glassware接收位置更新、发送一个请求给glass.locations.get让端点知道最新的位置。你的Glassware每十分钟接收一次位置更新
注意:获取位置信息需要在https://www.googleapis.com/auth/glass.location范围之内
语音命令
你的用户激活语音命令,例如:“Ok Glass, take anote, Cat Stream, 明天是Chipotle是的生日”。以下通知发送到你的Glassware
{
"collection": "timeline",
"operation": "INSERT",
"userToken": "chipotle's_owner",
"verifyToken": "mew mew mew",
"itemId": "<ITEM_ID>",
"userActions": [
{“type”: "LAUNCH"}
]
}
{
"id": "<ITEM_ID>",
"text": "Chipotle's birthday is tomorrow",
"recipients": [
{"id": "CAT_STREAM"}
]
}
收件人属性包含联系人id所使用的语音命令。