Placeholder Image

字幕列表 影片播放

  • JENNY MURPHY: So welcome to Building Glassware With the

  • Google Mirror API.

  • Today, we're going to show you some code on

  • building some Glassware.

  • Before we get into that, let's tell you a

  • little bit about ourselves.

  • ALAIN VONGSOUVANH: Hi, everyone.

  • My name's Alain Vongsouvanh.

  • I'm a Developer Programs Engineer on

  • Glass, and this is--

  • JENNY MURPHY: Hi.

  • I'm Jenny Murphy.

  • I'm also a Developer Programs Engineer on Glass.

  • So here's our menu for today.

  • We're going to briefly touch on the guidelines so you know

  • how to build some awesome Glassware.

  • We're going to cover this really briefly because it's a

  • lot of the same information that Timothy

  • covered early in a session.

  • In fact, we assume a little bit of foreknowledge here.

  • We assume you're familiar with Glass, either you attended

  • Timothy's session, or you have Glass on right now, or maybe

  • you visited some of our partners in the

  • Glass Sandbox earlier.

  • But even if you haven't, just hang tight, take some notes,

  • and make sure to follow up with that information like

  • seeing Timothy's video.

  • Once we're done with that, we're going to go through the

  • components of the Mirror API.

  • These are the building blocks that you

  • use to build Glassware.

  • We're going to go through each one and show you some code of

  • using them in isolation.

  • Once we're done with that, we're going to show you a

  • couple of examples of how you can put them together to build

  • some interesting Glassware.

  • We hope that this will give you the context that you can

  • use to build your own Glassware.

  • ALAIN VONGSOUVANH: All right.

  • Thank you, Jenny.

  • Sounds really interesting.

  • But before we dive into the code and learn how we can use

  • the Mirror API to build Glass services, or what we call

  • Glassware, we'd like to take a few moments to talk about

  • guidelines.

  • So keep in mind that the Glassware you write

  • [INAUDIBLE] user, and with the technology being so close to

  • the user, it's really important that the experience

  • we provide is the best possible.

  • That is why we designed guidelines

  • around those four points.

  • I won't go into details into those because Timothy really

  • did a great job explaining them.

  • If you managed to attend his sessions--

  • I bet many of you have--

  • you should be good to go.

  • If not, make sure to check his session when it's back online.

  • And always keep those guidelines in mind when you

  • are designing in Glassware, [INAUDIBLE] in Glassware, and

  • testing in Glassware.

  • Sounds good?

  • All right.

  • JENNY MURPHY: Hi.

  • Let's move on to the Mirror API, those building blocks I

  • mentioned before.

  • Before I get into that, how many of you here have built

  • software using RESTful web services before?

  • Wow.

  • Almost everyone.

  • How about mobile applications like Android apps?

  • A good number, and quite a bit of overlap.

  • You're going to find that you can leverage a lot of the

  • knowledge you already have, a lot of the experience you have

  • from those two.

  • But our perspective is a little bit different so please

  • try to keep an open mind.

  • And as many technical presentations start, we have

  • our architecture diagram, and this one is no different.

  • We have the Mirror API, the 10,000 foot view.

  • Up at the top, we have a green circle representing the Mirror

  • API itself, and this is a collection of RESTful web

  • services, which is really just a bunch of directories of JSON

  • that you work with over HTTPS.

  • Over on the right side, we have a box representing Glass

  • and its connection to the Mirror API with Glass Sync.

  • This is something you actually don't need to

  • worry too much about.

  • This is one of the cool parts of the Mirror API.

  • We take care of the synchronization for you.

  • We handle connectivity, we handle power, all

  • that kind of stuff.

  • You get to focus on the stuff to the other side, those JSON

  • messages you send back over HTTPS and your Glassware, the

  • code that interacts with the Mirror API.

  • And if you're observant, you may have noticed

  • I missed one thing.

  • There were directories of JSON up there, but we currently

  • have no way to tell the Mirror API what user you're

  • interacting with and no way to verify that they've given you

  • access to their data.

  • This is where OAuth comes in.

  • We use OAuth to install Glassware and to give the user

  • the opportunity to provide you access to their data.

  • OAuth is kind of like the valet keys for the internet.

  • It gives users the ability to give you access to just some

  • of their data out there.

  • Valet keys that kind of dance, and I'm going to walk you

  • through the dance that we use to do this.

  • So a new user arrives at your Glassware.

  • You don't know who they are.

  • Maybe it's time to install, maybe it's time just to hit a

  • Settings page.

  • In either case, what you're going to do is you're going to

  • redirect then to Google's OS servers, or

  • maybe open a pop-up.

  • You're going to send over all the permissions you want.

  • Typically, this is going to be glass.timeline, but we have

  • some other permissions like glass.location which I'll be

  • going into a little bit later.

  • The user logs into their Google account if they're not

  • already logged in, and then they see a page where they see

  • a list of permissions that you've asked for.

  • Since your Glassware has been prudent on the permissions

  • you're asking for and you have some really compelling

  • functionality, of course they're going say yes and

  • grant you those permissions.

  • They get redirected back to your service, and along with

  • that redirection comes a code.

  • Now it's your turn to complete that three way handshake.

  • You take that code and you make a back end request, along

  • with some details of your service, and we send you an

  • access token.

  • You use this access token to both identify the user when

  • you make those requests and to verify that you have those

  • permissions.

  • You include it in a header on all those requests.

  • There are a bunch of steps here.

  • One cool thing is we have client libraries in our

  • Resources that care of almost all of this heavy lifting for

  • you, so you don't need to worry about these details.

  • But if you want to, you can code at this level, too.

  • So now we have access tokens and we can start using the

  • Mirror API.

  • ALAIN VONGSOUVANH: Thank you, Jenny.

  • So now that the user has installed your Glassware, it's

  • time to do stuff with it.

  • Before we dive into how to write Glassware, let's talk

  • about the Mirror API and what it's made of.

  • I'm pretty sure many of you already know, but it's always

  • good to get a reminder.

  • So Jenny mentioned that the Mirror API is a RESTful API,

  • so it means that it's an API composed of connections that

  • you manipulate through HTTP requests.

  • So you can update, retrieve, delete, insert new items to

  • your collections.

  • The main collection you're going to use when you write

  • your Glassware is the Timeline collection.

  • This is how you insert content into the user's Glass, what we

  • call Timeline Card, and this is how you retrieve Timeline

  • Card from the user's Glass as well.

  • Each Glassware has their own Timeline, so you don't get

  • access to another Glassware Timeline.

  • For example, the Google+ Glassware doesn't have access

  • to the Gmail Glassware, and so forth.

  • Very few exceptions, but that needs user input and we'll get

  • into that later.

  • Now, the example you see on screen is the most simple

  • Timeline Card you can insert.

  • It's text only.

  • So this is a JSON representation of a Timeline

  • item, set the text property to Hello World, and then send an

  • authorized post request to the Timeline collection, and boom,

  • it's going to be in the user's Glass.

  • MALE SPEAKER: Hi everyone.

  • ALAIN VONGSOUVANH: It's as simple as that.

  • Thanks for the noise.

  • All right.

  • Let's jump to the other example.

  • So we inserted text, but you can also insert rich media to

  • the user's Glass.

  • The Glass client supports images and videos, and you

  • insert those using HTTP requests as well.

  • So you can insert media only, or you can also provide

  • metadata along with the media.

  • This is an example of a multipart HTTP request, but as

  • Jenny mentioned, we have client libraries that leverage

  • all of that for you so you don't have to do

  • all the heavy lifting.

  • And we have extensive documentation on how to do

  • that in our reference documentation.

  • So now, we have text, we have media, but sometimes, you want

  • to add your own branding as well.

  • You want to customize your Timeline Card.

  • You want to do more stuff, provide a better

  • experience to the user.

  • For that, we have HTML support.

  • So you can set HTML payload into the

  • Timeline item metadata.

  • It's as simple as just setting the HTML into the HTML

  • Property, and we have a list of tags that we support and

  • tags that we don't, like Script and Link.

  • All of that is documented in our Reference Guide as well.

  • And we also have a built-in CSS that we encourage you to

  • use to provide a consistent experience

  • across all the Glassware.

  • Now, let's look at how it looks like to the code.

  • So in our slide, we decided to use Java, but again, the

  • Mirror API is a RESTful API, so as long as you have a

  • language that can communicate over HTTP, you're good to go.

  • And Google does provide many client libraries in different

  • languages like Java, Python, PHP, .NET, and so forth.

  • We have documentation again, and then you can also check

  • the Client Libraries Project page to

  • learn more about those.

  • So for Java, how to insert a Timeline Item, you just create

  • a new Timeline Item Resource and then set the text or the

  • metadata properties you want for the Timeline Item.

  • In this example, we're only saying hello to our Explorers.

  • And then once you're good with your Timeline Item, use an

  • authorized Mirror service and send an authorized HTTP

  • request to the user's Glass.

  • And that's it.

  • You're done.

  • You just sent a Timeline Item to your user's Glass.

  • It's so simple.

  • Now, I could talk about this for 20 more minutes, or I can

  • jump to a demo.

  • All right.

  • So we have a tool we built for you called the Playground

  • where you can experiment with the API.

  • So the Playground is actually using the Mirror API to send

  • data to your user's Glass.

  • So we have a bunch of templates.

  • Let's start with a much simpler one, text only.

  • So you can simply edit the text.

  • Glass Explorers are awesome!

  • You can see on the right side of the screen, the JSON

  • representation.

  • And then when you're good with the Timeline Item, just hit

  • Insert Item.

  • It's going to use the API and then send it

  • to the user's Glass.

  • Now, let me go to my demo device with a cable.

  • Hang into it so I can show you what the screen

  • looks like for me.

  • There we go.

  • Just now, I just received the item I just inserted.

  • It's as simple as that.

  • Cool.

  • Now, let's jump back to the Playground.

  • We also have template support, HTML support, and we have

  • built some templates for you so you can get started

  • building your own.

  • So use the ones you like, do some modifications

  • like JFK, et cetera.

  • Modify it.

  • You have the JSON on the right side.

  • It's kind of complicated to modify via HTML from the JSON

  • payload, so you can also just edit the HTML by

  • clicking on the tab.

  • And then when you're happy with your template, just push

  • it into your Glass, make sure it looks the same, copy the

  • template over into your code, and you're good to go.

  • Sounds good?

  • Cool.

  • Let's jump back to the slide.

  • All right.

  • So right now, we've been doing one way interaction.

  • We've been pushing content to a user of Glass.

  • But the Glass also lets users take action on the card you

  • insert to the users.

  • You do that by adding more information into a Timeline

  • Item Resource that we call Menu Items.

  • There are two main categories of Menu Items, the built-in

  • ones that usually leverage the Glass client, like sharing,

  • which would create a new item-- more on that later--

  • Get Directions, that uses the Glass GPS and internal

  • navigation mode to navigate to the location provided in the

  • Timeline Item, and Reply, that will use a microphone from

  • Glass to get text input from the user.

  • There are many others built in menu items that we have in our

  • documentation.

  • And then we also have custom menu items.

  • This is what you use when you want to provide custom

  • features to the user.

  • For example, if you're a news agency and you're pushing news

  • content to a user of Glass, you might want to provide a

  • way for the user to save an article or

  • to vote on an article.

  • Glass doesn't know what to do with that, so you simply

  • provide more information to us.

  • You provide an ID, so you get to know when the user took an

  • action, you get to know which action the user took.

  • And then when we push that to you, it's up to you to process

  • the notification.

  • And then you also need to provide more information to

  • us, like the display name, because we don't know how to

  • display your custom menu item in an icon URL that will be

  • displayed alongside the display name.

  • All right.

  • Now, let's take a quick look at the code.

  • So as I mentioned, adding a menu item is simply adding

  • more property to the Timeline Resource.

  • So again, we create a new Timeline Item Resource, set

  • the text, and then set a built-in menu item.

  • In our case, we want to let the user be able to share this

  • card with other Glassware.

  • So you simply set the action to menu item to share, and

  • then add it to the list of menu items to

  • the Timeline Resource.

  • Add other properties, use the service object to push it to

  • Glass, and you're good to go.

  • Now let's take a quick look at custom menu items.

  • So it's almost the same thing.

  • Create a new Timeline Item, create a new Menu Item, set

  • the action to Custom, but now we need more information from

  • you so we know what to do.

  • So you need to set the ID so when the user takes the

  • action, the API will push a notification back to you and

  • let you know which action was taken by the user.

  • And also, we need to know how to display the menu item.

  • So simply provide a list of values with a display name

  • that will be displayed when the user selects the list of

  • menu items and an icon that will be displayed alongside

  • the display name.

  • Add other properties and then add the Menu Items to the

  • Timeline Resource and push it to Glass, and

  • you're good to go.

  • That's pretty simple, right?

  • Cool.

  • JENNY MURPHY: Next collection.

  • Next collection.

  • Contacts.

  • Here's a flow you're probably familiar with on Glass.

  • Pick a photo, in this case, a wonderful photo of

  • Chipotle the cat.

  • Of course you want to share that with all your friends.

  • So you tap, you share, and you share it with

  • your friends on Google+.

  • This is a flow of sharing that shares to contacts.

  • In this case, the contact represents

  • that Circle on Google+.

  • But contacts can represent other things, too, anything

  • you might want to share to.

  • They might represent an individual person, they might

  • represent a group of people like that circle, or they can

  • represent things that are a little bit more abstract, like

  • a whole Glassware or a feature within a Glassware like Path.

  • So let's look at the code that makes this possible.

  • So we're going to create a Contact Resource, we're going

  • to create an object.

  • We're going to set an ID.

  • This ID is passed back in the notification so that you can

  • figure out which contact was tripped

  • when an item was shared.

  • It's how you do the correlation of the round trip.

  • We're going to set some display properties.

  • We're going to set a name and an icon.

  • This is what shows up on your user's Glass.

  • And one property that's a little more sophisticated,

  • we're going to set Accept Type.

  • By default, contacts accept any kind of content on Glass.

  • Any Timeline Item that has been marked sharable could be

  • copied and shared.

  • So this includes text Timeline Items, HTML, images, video.

  • But sometimes, the Glassware may only want to process one

  • kind of content.

  • In this case, this example, we only want to process images,

  • so we're only going to accept cards.

  • And we specify that using a Mime Content Type, in this

  • case, image/*.

  • Accept any type of image.

  • Then we insert it, and that's it.

  • The contact is made available, and users can turn it on in My

  • Glass and start sharing content with it.

  • And one more collection, location.

  • So say we want to render something cool, like

  • this map, in a card.

  • We want to get the user's location.

  • There's a special collection, which has its own scope that

  • you can request, and the user can opt to share their

  • location with your Glassware.

  • If they've done that, you can check for

  • their latest location.

  • It's updated about every 10 minutes.

  • This one is super easy to use because we're just pulling

  • from a collection.

  • So the code is very simple.

  • We just do a Get request for the latest location, which

  • will give us their location within

  • about the past 10 minutes.

  • Then we do cool stuff with it.

  • In this case, we're doing something pretty simple.

  • We're just printing out the latitude,

  • longitude, and accuracy.

  • That's all there really is to location.

  • We read it, we get the latest location.

  • So up until now, we've been talking about pushing content

  • to Glass and having users interact with the content

  • you've pushed to their Glass device.

  • But there's really no mechanism yet for that

  • information to get from their Glass back to your Glassware.

  • What you could actually do is you could poll all these

  • collections every couple minutes and look for changes.

  • You could look for new cards that have

  • been shared with you.

  • But that's not very efficient.

  • So we have a better way with subscriptions.

  • Subscriptions allow you to subscribe to notifications

  • about changes in collections on Glass.

  • Kind of flips the API on its head.

  • Instead of you posting JSON to the Mirror API, the Mirror API

  • will post JSON back to you.

  • Here's some code that allows you to do it.

  • This is code that captures kind of the first half of

  • this, subscribing to notifications.

  • To subscribe to notifications, you insert an object into a

  • collection called subscriptions, you specify the

  • collection you want to monitor.

  • There are a couple of options.

  • In this case, you're subscribing to changes in the

  • Timeline Collection.

  • You specify a couple of identifiers.

  • These are user-specific identifiers that are passed

  • back in all of the notifications so that you can

  • verify that this is a bona fide notification, a true

  • notification, and you can identify the user that

  • triggered that change.

  • And of course, we need somewhere to send those

  • notifications.

  • So as part of this description, you specify a

  • URL, and you'll want to use an HTTPS URL

  • to keep things secure.

  • Once you have all that together, you insert it, and

  • after this point, any changes to the Timeline Collection

  • will be sent back to your service.

  • When the user takes action, or over time, for the case of

  • locations, you're going to be receiving these

  • pingbacks to that URL.

  • The structure looks a lot like the

  • subscription we just inserted.

  • Up at the top, we have the collection.

  • On one side, we have the Timeline.

  • On another case, we have the Locations.

  • We have an identifier of the object that caused the change

  • in notifications.

  • If there's a shared item, this is going to be the ID of that

  • card in the timeline, same for a menu trigger.

  • Another case for Locations on the other side, that's always

  • going to be the same ID because Locations always

  • surfaces the latest location.

  • In both cases, in all notifications that come back,

  • we're going to echo back to you the User Token and the

  • Verify Token so that you can verify the authenticity and

  • the user who made that request.

  • And in the case of Timeline, we're also going

  • to share some metadata.

  • In this case, we're telling you upfront from the

  • notification that this is a share that triggered this so

  • you can write cleaner code that takes advantage of that.

  • So let's take a look at the code for processing that

  • notification.

  • This code looks a little different than the code we've

  • had up here before.

  • Instead of doing an Insert, we're responding to a request.

  • Since we're talking Java today, we're going to use a

  • servelet method.

  • In this case, we're going to handle a post.

  • So a post comes on in.

  • The notification is given to us in the request body.

  • So the first thing we're going to do is pull that request

  • body and parse the JSON.

  • We're going to build an object.

  • We're going to push it into a notification using Jackson, so

  • then we can take action.

  • First thing we're going to do is we're going

  • to verify that data.

  • We're going to find the user, we're going to verify the

  • Verify Token to make sure everything's on the up and up,

  • and then we're going to create a Client Object that allows us

  • to make calls on their request using their credentials that

  • we got from that OAuth flow.

  • And then the fun stuff starts.

  • In this case, what we're doing is if it was a Timeline

  • notification, we're going to go fetch that Timeline Item,

  • we can inspect it, and then we can take action based on it.

  • That's the flow you're going to use for most of your

  • notifications coming in.

  • That's all the pieces of the Mirror API that you can use to

  • build stuff.

  • Here they are, Timeline, Menu Items, Contacts,

  • Subscriptions, and Locations.

  • And now let's see how we can put them together to build

  • some cool stuff.

  • ALAIN VONGSOUVANH: All right.

  • Thank you, Jenny.

  • So now we know what the Mirror API is made of.

  • It's quite simple but still pretty powerful.

  • We'd like now to take a moment to actually dive into some

  • demos of Glassware we brought for you, and we actually have

  • two of them.

  • First one, it's probably the most simple Glassware you

  • might start with, and we encourage you to start with

  • such a Glassware.

  • Cat Facts.

  • So everybody loves Cat Facts, right?

  • Just simple Glassware.

  • Just receive new Cat Facts every few minutes, every few

  • hours, or when new Cat Facts are available.

  • But on a more serious note, it's actually how the "New

  • York Times" Glassware or the CNN Glassware is working.

  • Every time they get a news update, they go through a user

  • database and then send push notifications to all users who

  • have subscribed.

  • Cool.

  • Now, let's take a look at the demo.

  • So again, this is a live demo, and we've been using random

  • Cat Facts, so we might have funny things showing up.

  • Let's go back to my Gmail Glass.

  • Let's see what I got while talking.

  • Cats are very lazy and yet very warm creatures.

  • I didn't know that.

  • Did you?

  • JENNY MURPHY: No, I didn't.

  • ALAIN VONGSOUVANH: Wow.

  • Cool.

  • Now I know.

  • It's awesome.

  • It's really simple.

  • Let's try another one.

  • We've seen this one, it's the item I inserted.

  • Let's see if I have one.

  • Pure black cats are part of an ancient cat voodoo cult.

  • Might be why some people are scared of black cats.

  • Cool.

  • All right.

  • So this is Cat Facts.

  • It's actually quite simple.

  • And we also had a placeholder for you.

  • Did you know that cats can see in the dark?

  • Again, I didn't know.

  • That's why I got so spooked many times by my cat.

  • Let's see what Cat Facts used from the Mirror API.

  • Well, this is a pretty simple Glassware.

  • This is the most simple Glassware you can write.

  • It's only using the Timeline collection.

  • We don't need any other features from the Mirror API

  • because we're only pushing contents to the Glass.

  • But for instance, if you wanted to provide a way for a

  • user to save Cat Facts or to share Cat Facts with their

  • friends, you might want to add menu items, et cetera.

  • Let's see what the user flow looks like.

  • Again, it's pretty simple.

  • So the user starts on your website,

  • authenticate using OAuth 2.0.

  • Then once you get the user's token, store the

  • token in your database.

  • And when you get a new Cat Fact or when your Chron Job

  • runs, just go through your list of users, get the

  • credentials, insert a new service object, insert the

  • item into the Mirror API, and we'll take care of

  • the rest from here.

  • Google's server will sync with the user's Glass and

  • eventually, Cat Facts will be pushed to the user's Glass and

  • make the user happy.

  • Let's take a look at the code.

  • Again very simple.

  • We start by getting a random cat fact.

  • So this is case, we're only setting the text, but in your

  • Glassware, you might want to use HTML to be able to add

  • your own branding.

  • Then we are getting a random cat fact, but you should get

  • push notifications from the news provider you're getting

  • your information from.

  • And then you go through all of your users in your database,

  • get their credentials, build a Mirror service.

  • This is Java.

  • It might change according to the language you're using.

  • And then push the item to the user's Glass,

  • to the user's Timeline.

  • Of course, always check for exceptions.

  • Something bad might happen and you want to catch that so you

  • don't break the process for all the rest of the users.

  • And then that's it.

  • You just send all the cat facts to all of the users.

  • But what we did here was sending a request

  • for all of our users.

  • There might be a better way to do that.

  • Instead of sending tons of HTTP requests every hour when

  • you get a new update, you might be able to

  • have a better process.

  • And actually, API Stacks, on which the Mirror API is built

  • on, supports batching requests.

  • The way it works is that you simply build a multipart HTTP

  • request composed of sub-HTTP requests, and you send a

  • single HTTP request for all the users.

  • So all the current libraries might not

  • support batch requests.

  • You should check on the Project page.

  • The Java that we're using here does

  • support batching requests.

  • And the way it works is that you first need to create a

  • batch callback class that extends the JSON batch

  • callback interface.

  • You need to implement two methods, on

  • success and on failure.

  • This is to keep track of which requests have failed or which

  • requests have succeeded.

  • In our case, we're only keeping track of

  • the number of requests.

  • But in your case, you might want to keep the request and

  • retry again, or if the user had revoked your token, just

  • remove the user from the database.

  • Once you're done implementing your class, simply create a

  • Batch Request from the Mirror service.

  • So this is unauthorized.

  • You don't need to authorize a Batch Request.

  • Create the new callback object and then go through all your

  • users again, get a credentials from the

  • database, same as before.

  • Create a new Insert Request, but don't execute the

  • request right now.

  • Simply add them to the queue of a Batch Request.

  • Once you've added all the sub-requests to the batch,

  • execute the Batch Request and check out the results.

  • That's it.

  • You just saved bandwidth.

  • You sent multiple requests into one SQL request, and with

  • gzip encoding you might actually save a lot of

  • bandwidth and data.

  • But batching requests doesn't save you quota, though.

  • All right, this was really simple.

  • Let's jump into another demo that leverages the API more.

  • JENNY MURPHY: Now we're going to show you another Glassware.

  • This one's a little bit more sophisticated.

  • It makes photos better by adding cats to them.

  • Let's take a look at it.

  • So switch into the demo Glass device.

  • Does anyone mind if I take a photograph?

  • Here we go.

  • So here's a photo of all of you.

  • What I can do now is I can share it with not a circle,

  • but with Add a Cat to That.

  • And we'll loop back in a moment

  • just to see what happens.

  • So right now, that's being sent off to the server to get

  • processed, have a cat added to it.

  • So let's take a look at how that works.

  • This uses a few more features of the Mirror API.

  • In addition to the Timeline, which we used to work with the

  • card up there and also to change the card once the proof

  • photo comes back, we also used the Contacts Collection to add

  • a contact for adding a cat.

  • And we subscribed to changes in the Timeline Collection so

  • we know when it's time to add a cat to a photo.

  • The flow of this Glassware has a couple different chunks.

  • First chunk is the Bootstrap phase.

  • This is a design pattern we see in a lot of Glassware,

  • especially the more sophisticated ones.

  • This gets executed whenever a user installs this Glassware.

  • We do a few different things to set them up.

  • So it starts with the user authenticating

  • for the first time.

  • We store their authentication credentials so that we can

  • make requests on their behalf later, and we immediately do a

  • couple things.

  • We're going to insert that contact so they can share

  • photos to a Glassware, and we're also going to insert the

  • subscription to their changes in the Timeline so we know

  • when they share a photo with us.

  • Later, when our user is out about running around, taking

  • pictures that could be a little bit better with the

  • addition of a cat, we do our interesting stuff.

  • They take the photo, they share it with us.

  • That causes a notification to be sent to our Glassware.

  • We fetch the bytes of the photo, take those bytes and

  • composite them with the pictures of the

  • cat, making it better.

  • Then we go back to their Timeline and find the card

  • that was created when they shared that, and replace the

  • image with our improved image.

  • Let's take a look.

  • It's already back.

  • And here is that photo improved.

  • [APPLAUSE]

  • JENNY MURPHY: So let's take a look at the code that made

  • that possible.

  • First, we're going to go through that

  • Bootstrap phase again.

  • This code will look really familiar because we're just

  • using the basic features of those two different

  • collections.

  • First, we're adding that contact.

  • We're giving it a name, we're setting display properties and

  • then ID so we can correlate it later, and we're setting an

  • accept type.

  • Our software can make any image better.

  • But we could have made it more narrow.

  • For example, if we only wanted to process JPEGs, we could

  • have said image/JPEG here.

  • And then we insert it.

  • We're also subscribing to notifications on the Timeline

  • Collection.

  • This is pretty much the same code you saw before.

  • We're setting an endpoint, HTTPS, setting tokens, and

  • subscribing.

  • Once the Bootstrap phase is complete, then we are ready to

  • start handling those notifications as they come in

  • from photos being shared.

  • Pretty similar to last time, we're using Jackson to parse

  • those notifications as they come in.

  • We're verifying the notification, we're inspecting

  • that token and making sure everything looks clean.

  • Fetching the user, creating a client so that we can make

  • requests and manipulate their Timeline Item.

  • And then we call the Add a Cat to that function to add a cat

  • to that Timeline Item.

  • Let's dig into there.

  • First thing we do up here is we get the

  • Timeline Item that impacted.

  • This is a copy of the original photo that we took that is

  • accessible to our Glassware so that we can

  • make changes in place.

  • We're going to go through all of the attachments that are on

  • this Timeline Item.

  • Typically, a card that's taken from the built-in camera will

  • only have one attachment, but another Glassware could have

  • created and made sharable a Timeline Item with many

  • attachments.

  • We're going to iterate through all of them and make them all

  • better, because of course, they want cats in all of them.

  • We're going to get the bytes and process the photo.

  • We're going to do the composition in that function.

  • Then what we're going to do is we're going to go through each

  • one of those attachments and we're going to remove the one

  • that came to us and replace it with the improved one so that

  • the card is replaced in place.

  • And then finally, we're going to update the card itself and

  • make it sharable.

  • Because of course, you're going to want to share these

  • cat improved photos with social

  • networks and other Glassware.

  • Then we insert it.

  • And that was Add a Cat to That.

  • Slightly more sophisticated Glassware.

  • And yeah, it's silly, but it shows you how some of those

  • features in the API can come together to maybe produce

  • something you didn't think was quite obvious.

  • So let's talk about next steps.

  • So we're now two sessions into the track.

  • I hope you've enjoyed them so far.

  • There are a couple more left and we'd like to encourage you

  • to go check them out.

  • We have Hack Your Glass coming up in a little while where you

  • can dive deeper in beyond the Mirror API on the bleeding

  • edge in maybe void your warranty in the process.

  • And immediately after, we have a fireside chat with a lot of

  • the leaders of our team.

  • Alain and myself and a whole bunch of developers from the

  • Glass team are hanging out in the Sandbox so please come by

  • later today, maybe tomorrow, and ask us questions, share

  • your ideas, and just chat with us about Glass.

  • We love hearing from you.

  • And now and beyond I/O, we have community

  • on the web, of course.

  • You can come and find us on StackOverflow to ask your

  • questions about the Mirror API in Glass.

  • You can find our sample code on GitHub.

  • And if you found an issue or have a feature request, you

  • can go to our Issue Track and report it.

  • You can find links to all of these up on developers.googl

  • e.com/glass/community.

  • And just for you Explorers in the audience--

  • I see a bunch of you--

  • we have a special community for you to discuss Glass and

  • the Mirror API.

  • So if you have Glass already, you can go there right now and

  • discuss with your co-explorers, and if you

  • haven't picked up Glass yet, you'll gain access once you

  • pick it up.

  • And that's it.

  • We have a few minutes for questions now.

  • Thank you for listening to us.

  • So if you'd like to come up to the microphone to ask

  • questions, please do.

  • And after that, we're going to be in the Sandbox.

  • So if you have questions we don't have time to answer,

  • come ask me there.

  • [APPLAUSE]

  • ALAIN VONGSOUVANH: Thank you.

  • Question?

  • AUDIENCE: Yeah.

  • So in the Add a Cat example, you describe removing the

  • attachments, modifying them, and then doing an update.

  • Why is that the pattern?

  • Why isn't it creating a new update and pushing that?

  • Could you describe the difference

  • between both of those?

  • ALAIN VONGSOUVANH: You mean the client, creating a copy.

  • So it's that because the Glassware, so each Glassware

  • has access to their own timeline only, so you don't

  • get to share a common timeline.

  • And so that the user still has access to the original photo

  • if they want to share with other Glasswares.

  • So we create a copy for you that only your Glassware has

  • access to, and you have ownership over this item.

  • AUDIENCE: Got it.

  • ALAIN VONGSOUVANH: So you can do updates on it, and if

  • you've been using the Path on Google+ Glassware, this is

  • actually what we're doing.

  • So when you share a photo with Google+, Google+ reports that

  • photo and then updates the card to let the user know that

  • it has been processed.

  • And then when they get Plus One or comments, they'll be

  • pushed into the bundle.

  • AUDIENCE: So you don't have to create a new one and push it?

  • ALAIN VONGSOUVANH: Yes.

  • You update the existing one.

  • AUDIENCE: And would there be any time that you would

  • actually create a new one?

  • ALAIN VONGSOUVANH: It really depends.

  • JENNY MURPHY: For bundles.

  • ALAIN VONGSOUVANH: Yeah, it really depends.

  • If you have an Instagram Glassware, for instance, you

  • might want to create multiple attachments and add them to

  • the item so the user can choose which one they prefer.

  • AUDIENCE: OK, got it.

  • Thank you.

  • ALAIN VONGSOUVANH: You're welcome.

  • JENNY MURPHY: How about from this side?

  • Question?

  • AUDIENCE: Hello.

  • I'm [INAUDIBLE] from Thailand.

  • I'm wondering if it supports international language, for

  • example, Thai language or any Asian language for displaying

  • the timeline.

  • JENNY MURPHY: So the Timeline Items themselves are UTFA, and

  • you can actually see from some of our previous demos, we do

  • translation.

  • We're actually putting character sets from other

  • languages in there.

  • So they should work, and if you encounter a situation

  • where they don't, please let us know.

  • AUDIENCE: How can I know that it doesn't work?

  • JENNY MURPHY: Sorry?

  • AUDIENCE: I'm wondering if this works.

  • I saw in the last session there is a "late-aral"?

  • menu?

  • JENNY MURPHY: Literal?

  • AUDIENCE: Literal, yeah.

  • And this works for non-English languages, too?

  • JENNY MURPHY: Yeah, it's actually one of the really

  • cool features of Glass is you can Google for a translation.

  • So I can ask it how to say something in another language,

  • and it reads to me in that language, too.

  • AUDIENCE: OK, cool.

  • Thank you.

  • JENNY MURPHY: It's pretty neat.

  • AUDIENCE: Hi.

  • Billy Lo from Toronto.

  • Specifically around the OAuth integration, it's not quite

  • clear how a Glass Mirror API handles the OAuth Refresh of

  • the token because generally, there's an expiration of the

  • OAuth token, right?

  • ALAIN VONGSOUVANH: This is actually part of the protocol

  • and our API Stack.

  • So usually, what you can do is either keep the expiry date

  • you get when you retrieve an access token, or the other way

  • you can do that is try to send a request and look for an

  • authorized request back from the API.

  • So if you get a 401 or 403 back, try to refresh the

  • token, and if this is successful,

  • just use the new token.

  • AUDIENCE: I see.

  • Thank you.

  • ALAIN VONGSOUVANH: And our client libraries handle that

  • for you as well.

  • AUDIENCE: Excellent.

  • Thank you.

  • JENNY MURPHY: The slide we showed was a slight

  • oversimplification to try and do OAuth in one slide, so

  • there's a little more detail to it.

  • This side?

  • AUDIENCE: Hi there.

  • JENNY MURPHY: Hi.

  • AUDIENCE: How soon can we send audio attachments to the

  • Mirror API?

  • JENNY MURPHY: Audio attachments.

  • So right now, you can attach photographs and video, and you

  • can take the photos and embed them into the card.

  • Audio attachments is something we want to support sometime.

  • I don't have any specific timeline on it.

  • You can actually attach them, they just

  • don't play right now.

  • But as we've mentioned before, it's in developer preview, so

  • that'll be coming at some point.

  • AUDIENCE: Thank you.

  • JENNY MURPHY: You're welcome.

  • AUDIENCE: Hey, this is Andreas from Minnesota.

  • I'm interested in lower latency scenarios and wonder

  • what round trip times could be in [INAUDIBLE]

  • scenarios.

  • And beyond Mirror, if you're looking at other APIs which

  • enable like more real time things?

  • ALAIN VONGSOUVANH: So Timothy this morning during his

  • session announced that we're working on GDK, so Glass

  • Development Kit.

  • It's not ready yet.

  • We're still working on it.

  • Make sure to file feature requests if you have any, and

  • we'll keep you updated when we have more information.

  • JENNY MURPHY: And in the meantime, I recommend you go

  • to the Hack Your Glass session.

  • I think you'll like the content there.

  • Switching sides again.

  • Hi.

  • AUDIENCE: Is there a character limit for the bodies of the

  • messages, because it looks like in the Gmail app, emails

  • are truncated?

  • JENNY MURPHY: So for content and length.

  • So you can fit about seven lines comfortably, but they're

  • variable with fonts.

  • AUDIENCE: So when you have multiple cards, though, the

  • message will keep going?

  • JENNY MURPHY: Yes, you can have multiple cards.

  • AUDIENCE: Is there a limit on how many

  • characters you can put?

  • JENNY MURPHY: There are a couple ways to do this.

  • If you're handcrafting HTML, you kind of frame it so it

  • fits in there well.

  • If you have plain text content, we'll actually page

  • it for you automatically.

  • That's how Gmail supports it.

  • AUDIENCE: Is that a bug in the Gmail app then?

  • If you have a long email, it truncates the message and you

  • can't read the full email on Glass.

  • JENNY MURPHY: You can't read the full email?

  • It should be showing you the full email.

  • So if you can show that to me, swing back by the Sandbox, and

  • we'll take a peek.

  • AUDIENCE: Thanks.

  • JENNY MURPHY: I think we have time for one more quick one.

  • ALAIN VONGSOUVANH: Seven seconds.

  • JENNY MURPHY: Six seconds left.

  • AUDIENCE: So I've been wondering--

  • Timothy showed this morning the Facebook application which

  • allows you to add a caption to a photo after sharing it.

  • I've been wondering what kind of menu item this is.

  • ALAIN VONGSOUVANH: It's a reply menu item.

  • AUDIENCE: So you can change the way the standard menu

  • items are displayed?

  • ALAIN VONGSOUVANH: Yes.

  • So you can use built-in menu items and override the display

  • name and the icon.

  • AUDIENCE: OK, thanks.

  • ALAIN VONGSOUVANH: So for other questions, we'll be at

  • the Sandbox.

  • Feel free to swing by and ask us any questions you have.

  • JENNY MURPHY: Yes.

  • Come talk to us.

  • We love talking about Glass.

  • ALAIN VONGSOUVANH: Thank you for coming.

  • JENNY MURPHY: Thank you, everyone.

JENNY MURPHY: So welcome to Building Glassware With the

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

Google I/O 2013 - 利用Google Mirror API構建眼鏡服務 (Google I/O 2013 - Building Glass Services with the Google Mirror API)

  • 70 7
    田立瑋 發佈於 2021 年 01 月 14 日
影片單字