Search code examples
node.jsgoogle-glassgoogle-mirror-api

How to pull attachments from Google Glass item?


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: enter image description here

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?


Solution

  • 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.