How to pull attachments from Google Glass item?

帅比萌擦擦* 提交于 2019-12-11 22:16:48

问题


I created NodeJS server that communicate with Google Glass, I want to know how to pull attachment from item, below you can see the item with attachments:

Note: in my project I already have:
*Send item item to glass(contact, card, location, etc..)
*Subscription to timeline collection
*Contact with callback to let Glass user share content - for more info Visit How to add another option to the share functionality of Google Glass?

Do I need to use the selfLink to pull the attachment? if yes then how I can execute HTTP request for the selfLink while including the token?


回答1:


The selfLink refers to the URL of the timelineItem itself. You want to look at the attachments attribute of the object. It might look something like this:


{ "kind": "mirror#timelineItem",
  "id": "da61598c-2890-4852-2123-031011dfa004",
  ...
  "attachments": [
    "id": ...
    "contentType": "image/jpeg".
    "contentUrl": "https://www.googleapis.com/mirror/v1/timeline/da61598c-2890-4852-2123-031011dfa004/attachments/ps:605507433604363824",
    "isProcessingContent": false
  ]
}

You should check to make sure isProcessingContent is false before you try to fetch it, otherwise the fetch will fail. This is usually pretty quick for images, but can take longer for video.

See more at https://developers.google.com/glass/v1/reference/timeline/attachments

To fetch it, you can issue an HTTPS request to that URL with an Authorization header with a value of Bearer auth_token (replacing auth_token with the actual value of the auth token).

To make the request itself, you'll probably want to use the http.request() method. So something like this (untested) might work:


var item = {the item you got sent above};
var attachment = item.attachments[0];
if( !attachment.isProcessingContent ){
  var contentUrl = url.parse( attachment.contentUrl );
  var options = {
    "hostname": contentUrl.hostname,
    "path": contentUrl.path,
    "headers": {
      "Authorization": 'Bearer '+authToken;
    }
  }
  https.request( options, function(res){
    // Get the image from the res object
  });
}

See the documentation for URL.parse and HTTPS.request for details.



来源:https://stackoverflow.com/questions/25685220/how-to-pull-attachments-from-google-glass-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!