谷歌眼镜Mirror API开发指南之Subscriptions

原文地址:http://bbs.seacat.cn/thread-883-1-2.html




订阅

Mirrror API允许当用户需要具体的Timeline Item和当用户的位置发送改变时发送订阅通知(subscribe to notifications),当你订阅通知,你需要提供一个回调URL来处理通知。

接收通知


来自MirrorAPI的通知会发送一个POST请求给订阅的段节点,这里面包含一段JSON数据。


Raw HTTP



[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "timeline",  

  3.  "itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  4.  "operation": "UPDATE",  

  5.  "userToken": "harold_penguin",  

  6.  "verifyToken": "random_hash_to_verify_referer",  

  7.  "userActions": [  

  8.    {  

  9.      "type": "<TYPE>",  

  10.      "payload": "<PAYLOAD>"  

  11.    }  

  12.  ]  

  13. }  





Java代码


[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import com.google.api.client.json.JsonFactory;  

  2. import com.google.api.client.json.jackson.JacksonFactory;  

  3. import com.google.api.services.mirror.model.Notification;  

  4. import java.io.IOException;  

  5. import java.io.InputStream;  

  6. // ...

  7. publicclass MyClass {  

  8. // ...

  9. /**

  10.    * Parse a request body into a Notification object.

  11.    *

  12.    * @param requestBody The notification payload sent by the Mirror API.

  13.    * @return Parsed notification payload if successful, {@code null} otherwise.

  14.    */

  15. static Notification parseNotification(InputStream requestBody) {  

  16. try {  

  17.      JsonFactory jsonFactory = new JacksonFactory();  

  18. return jsonFactory.fromInputStream(requetBody, Notification.class);  

  19.    } catch (IOException e) {  

  20.      System.out.println("An error occurred: " + e);  

  21. returnnull;  

  22.    }  

  23.  }  

  24. // ...

  25. }  





Python


[python] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import json  

  2. def parse_notification(request_body):  

  3. """Parse a request body into a notification dict.

  4.  Params:

  5.    request_body: The notification payload sent by the Mirror API as a string.

  6.  Returns:

  7.    Dict representing the notification payload.

  8.  """

  9. return json.load(request_body)  






你的服务如果没有错误发生必须对HTTP状态码等于200时做出响应。如果你的服务响应一个错误代码,Mirror API可能会重发通知给你的服务。
注意: 10秒无响应连接将超时。如果你的请求时间过长,你需要新开一个线程。

通知类型


Mirror API为不同事件会发送不同的通知载体。


共享timeline item


用户通过你的Glassware来共享Timeline item


[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "timeline",  

  3.  "itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  4.  "operation": "INSERT",  

  5.  "userToken": "harold_penguin",  

  6.  "verifyToken": "random_hash_to_verify_referer",  

  7.  "userActions": [  

  8.    {  

  9.      "type": "SHARE"  

  10.    }  

  11.  ]  

  12. }  





temId属性共享Timelineitem的id,你可以通过使用 Timeline.get来获得timeline item。下面一个典型的示例展示了timelineitem带照片做为附件。


[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "id": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  3.  "attachments": [  

  4.      {  

  5.          "contentType": "image/jpeg",  

  6.          "id": "<ATTACHMENT_ID>"  

  7.      }  

  8.  ],  

  9.  "recipients": [  

  10.      {  

  11.          "kind": "glass#contact",  

  12.          "source": "api:<SERVICE_ID>",  

  13.          "id": "<CONTACT_ID>",  

  14.          "displayName": "<CONTACT_DISPLAY_NAME>",  

  15.          "imageUrls": [  

  16.              "<CONTACT_ICON_URL>"  

  17.          ]  

  18.      }  

  19.  ]  

  20. }  





注意:有关更多信息,请参见 联系人(Contacts)与联系人分享内容。


回复


用户使用内置的答复回复你timeline item


[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "timeline",  

  3.  "itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  4.  "operation": "INSERT",  

  5.  "userToken": "harold_penguin",  

  6.  "verifyToken": "random_hash_to_verify_referer",  

  7.  "userActions": [  

  8.    {  

  9.      "type": "REPLY"  

  10.    }  

  11.  ]  

  12. }  





itemId属性设置为项目包含的ID

inReplyTo属性设置为timelineitemID项回复
text属性设置为文本转录

收件人属性设置为timeline item的创造者的回复,如果它存在的话

例子:



[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "kind": "glass#timelineItem",  

  3.  "id": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  4.  "inReplyTo": "3236e5b0-b282-4e00-9d7b-6b80e2f47f3d",  

  5.  "text": "This is a text reply",  

  6.  "recipients": [  

  7.    {  

  8.      "id": "CREATOR_ID",  

  9.      "displayName": "CREATOR_DISPLAY_NAME",  

  10.      "imageUrls": [  

  11.        "CREATOR_IMAGE_URL"  

  12.      ]  

  13.    }  

  14.  ]  

  15. }  





删除

用户删除一个timeline item




[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "timeline",  

  3.  "itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  4.  "operation": "DELETE",  

  5.  "userToken": "harold_penguin",  

  6.  "verifyToken": "random_hash_to_verify_referer",  

  7.  "userActions": [  

  8.    {  

  9.      "type": "DELETE"  

  10.    }  

  11.  ]  

  12. }  





temId属性设置为删除项的ID。项目不再包含元数据除了它的IDisDeleted属性
注意:如果用户删除一个timeline item,建议您从您的系统删除这些内容

自定义菜单项选择



用户选择了您服务设置的一个自定义菜单项(custom menu item)


[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "timeline",  

  3.  "itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",  

  4.  "operation": "UPDATE",  

  5.  "userToken": "harold_penguin",  

  6.  "userActions": [  

  7.    {  

  8.      "type": "CUSTOM",  

  9.      "payload": "PING"  

  10.    }  

  11.  ]  

  12. }  





temId属性设置为用户选择菜单项的ID
userActions数组包含用户点击这个项目自定义操作的列表,。你的服务应该相应地处理这些行为。

位置更新

一个新的位置对当前用户可用



[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "locations",  

  3.  "itemId": "latest",  

  4.  "operation": "UPDATE",  

  5.  "userToken": "harold_penguin",  

  6.  "verifyToken": "random_hash_to_verify_referer"  

  7. }  





当你的Glassware接收位置更新、发送一个请求给glass.locations.get让端点知道最新的位置。你的Glassware每十分钟接收一次位置更新

注意:获取位置信息需要在https://www.googleapis.com/auth/glass.location范围之内

语音命令

你的用户激活语音命令,例如:“Ok Glass, take anote, Cat Stream, 明天是Chipotle是的生日。以下通知发送到你的Glassware


[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "collection": "timeline",  

  3.  "operation": "INSERT",  

  4.  "userToken": "chipotle's_owner",  

  5.  "verifyToken": "mew mew mew",  

  6.  "itemId": "<ITEM_ID>",  

  7.  "userActions": [  

  8.    {“type”: "LAUNCH"}  

  9.  ]  

  10. }  





这个通知是有别于其他启动userActions的通知。
您可以使用itemId获取timeline item的值



[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  

  2.  "id": "<ITEM_ID>",  

  3.  "text": "Chipotle's birthday is tomorrow",  

  4.  "recipients": [  

  5.    {"id": "CAT_STREAM"}  

  6.  ]  

  7. }  





收件人属性包含联系人id所使用的语音命令。



你可能感兴趣的:(开发,Collection,operation,谷歌,眼镜)