Power Your Apps with Google APIs with Wesley Chun (GDD India '17)

Power Your Apps with Google APIs with Wesley Chun (GDD India '17)

Show Video

This. Session is an. Introduction. To. Generally. Using our api's, because. I'm on the G Sui team most, of the api's we're gonna look talked about today our G, suite relator or Google Apps alright, so Gmail, Drive, app. Script calendar, sheets slides and all those things so you'll be, able to learn those things. But. You're not restricted, to just those things once. You learn how to use Google API, in general you can also use the cloud platform stuff, and. Other Google services, that connect to Apps Script ok, so, today we're gonna show you two different ways, to, programmatically. Access G, suite technologies, and build apps that use these tools that you already know how to use right. Everybody, knows how to use Gmail I think everybody knows how to use sheets and drive but, now we're gonna start coding, those things and by. The time you're done with, these two code labs and you have enough you will have enough information to get, started on a real projects, so, it's really up to your imagination as. To what you can do with it afterwards ok. Ok. So, each. Each, code, lab is about 40, maybe 30 to 40 minutes long so we're gonna be together for the next 90 minutes, and. So, like I said we're gonna explore two different ways two very different ways of using Google, developer, tools and api's and you kind of decide what you, are comfortable with okay. Because my job is to, make sure that you are successful using, our developer tools and if. Certain things like slow, you down make, it hard for you to get started I'm here, to break, down those walls so. That you don't have any problems, ok, so that's really my job some, of you might know that I host the G suite dev show on, YouTube so, every. Month, or so will come up with a different video to show you a different way of using a different G suite API and, also, am a contributor, to the blog as well as our Twitter my. Background, is I've been a software engineer for more than 20 years probably. The most well known thing I've ever I've done is I was one of the original ten engineers that built yahoo mail 20 years ago if, anybody has used yahoo mail before, long. Time ago in fact it was it was called rocket, mail that, was the very first version it was came out about eight months after hotmail, so, we were the very second, web-based email system, would come out so there were 10 of us that did it using Python and no one ever heard of Python before in 1997. I. Also. Wrote some books. Some Python books so if you know, those that's great but I've been a teacher since before many of you were born so. That's not a typo it is 1983, so I know a lot of you guys are much younger than that so I'm very old okay. And. I'm also of, a. Python. Community member, okay. So we have some helpers helping us today so I'm just going to ask them to raise their hands as I introduce them so we, have Patrick back, there he, he. Lives, in Chennai, so. He's pretty close right. He's. A local so he can go find him later, okay. But he is a Google developer expert, which, means that he is not a Googler, but he's as close to a real Googler, as possible, and in, fact he knows more than some of us because. He, makes his living working. With G sweet api's as well as GCP api's okay, and then, we also have alex alex, on the training team he's back there he'll also help you out with he's so. He works on like I said the Google developer training team so he has helped, put together multiple, training. Courses in the past and then, we have Roman Romans. Right there yep, so he's also a GDE specifically. For GCP and so he's helping as well - all right, I will tell you this though the code samples, we will do our very very, short, there, are only four lines each so.

Four Lines for each code lab but, the most important, thing is the barriers, that we break down not, the number of lines of code once. You get these four lines of code working then, the, sky is the limit you can do much. More with it and. It's only what you can do in Europe once you can think of in your imagination, okay. All. Right so, let's talk. About the training a little bit. And let's. Get started, okay. So first of all I'm very happy to be at GDD India, I love, coming to developer, events when, I'm not making videos and. Writing. Blog posts I like, to go out and talk to developers because, I like to see developers, build exciting, apps I like to see developers, become successful, okay. So that's really my main job so I'm very happy to come to events like this even if it's halfway around the world for me you, know I haven't been in India in 16, years so, it's been a long time since I've been here and so I'm very happy to be back so, namaskara, all. Right. Yeah. If you want to know where I've been in India I've been to more places and you guys have, okay. Mumbai. Arang APOD Jaipur, Delhi, Agra, carajo, Varanasi. And now Bangalore, or, Bengaluru. Okay. So it's true you're nodding your head because you know I've been to more parts of your country than you have, okay. Why because you don't feel like a tourist and if you go somewhere far away they don't speak my language and things like that okay, I don't let that bother me alright, wherever I go I try to learn one, or two words in a language just because I like, culture and I like meeting people alright, but. I'm here today for you and our. Job is to make you guys successful, okay, so what. Are some of these things well guess what these are tools that you guys have seen many times before, all. Right these, are our consumer, tools you have you search you have use YouTube you, have an Android device you've, used Chrome and you probably have used Google Apps alright. However. That's. Only, for, people who are users, you. Guys are more than users, that's why you're here at GDD you are a developer, okay. So, while, it's true that. My. Co-workers are happy to produce tools like this for you we can't write everything, so, we rely on you guys to, actually build, things on top of our stuff because we can't do it we don't have time or the effort to go and write an HR tool a payroll tool a benefits, tool, travel. App you, know Metro, app bus, and taxi app we don't have time to write all those things but you can leverage our technologies. To help you do that. Okay that's, what our developer tools are for so. Instead of using waiting, for Google to write a tool you can use our technologies, and write your own tool whether, it be for mobile or web, or most. Likely both, right, because in order to succeed you really have to do both these days okay you can't just do, only one okay. However, one. Problem people, tell us is oh you're a PRS are too hard to use, you. Know every time I try it like I can't get past the OAuth it's, really difficult and, I'm, just you, know I'm just gonna do something else right, so, I don't want that to happen so that's why my job here is to fix it so that this is not not, not a problem for you as much okay. So, we're gonna focus of course on G's week because that's you know that's the the the, team that I work on but. You, can leverage other Google, developer. Tools to not just not, just these okay, and by the way so yes we used to be called Google Apps but we changed the name of Jesus we because. A lot of times people ask me so. What do you work on at Google I say oh I work on Google Apps and then people go which one. Okay. So it's kind of confusing so we just want to make it you know like it's. Called G suite because it is a suite of tools to, help people become more productive all right so that's the main reason why we. Changed the name okay, and there are many companies that use it to become productive okay. And it's also used at a lot of schools too so it's. Not just companies so it's used at companies that use at schools and we, don't Google. Does not charge force, to schools. To use it in the classroom so that's why you. Know we have a lot of students, that that know how to use they. Know how to use G suite okay. Okay. So. Bring. Up your laptops and go. To the first code lab okay. So we are going to do Google Apps Script now so this will introduce you to this this. Technology, so. It's, at the link that you see on the screen there so we have this really cool JavaScript. Environment and, we, have an IDE that's, built into the browser to. Let you quickly hack things together okay. So we're going to create something really quick and we're, gonna do a little bit of Apps Script code use, some Google technologies.

Then We're gonna run it and then, we're going to be successful and then we can move on to something. That's a little bit more difficult, which is using our REST API s okay. Okay. You should see something like this. Okay. And. So. In this code lab we're. Going to learn how to use Google Apps Script with various Google services. Okay. We're going to develop code, from within the. Development. Environment. Okay. So I'm. Going to do it with you. Okay. So what you need is you need internet, access okay, you need a web browser I think you guys all have a web browser, okay. A Google, account I think if you guys are at this conference you probably have a Google account okay. You need some basic JavaScript. Skills, you know nothing hardcore just using. JavaScript as a programming, language not. Like oh I don't know how to access, the Dom or whatever it's nothing like that, okay and then, just basic. Spreadsheet like you know how to open a spreadsheet you know how to type something into a spreadsheet so I'm pretty sure you guys can do that. Okay. So what are we going to do exactly so we're going to learn something about Apps Script enough for you to get going we're, gonna create a new spreadsheet, okay. We're gonna put a street address in the spreadsheet and then, we're gonna learn how to use a script editor too for, any document, and then we're gonna type in Apps Script code we're gonna save it we're gonna run it and then you're gonna use Gmail to, show that everything's working all, right so you're gonna use all those things once you get it once you get online, okay. So you can read all this stuff I'm not gonna like line, by line talk, about the code lab because it's accessible to you guys after this class is over all. Right so I'm. Gonna. Let's. See I'm just gonna show. You what you're going to do once you get once you get working okay, so down here you don't have to read any of this stuff and. You can read it later you, should read it just, don't do it now okay. So. The first thing we're going to do is I'm gonna ask you, to go and create a spreadsheet all. Right do, we have a shortcut, if you need to create a brand new spreadsheet from scratch you can go to that link sheets that google.com slash create, and it just makes a spreadsheet for you okay, so that's the first thing we're gonna do. Otherwise. You can go to your Google Drive and, then pull down the new click, new and then Google, sheets and then, create a blank spreadsheet, okay. So. That's what we're gonna do and then. Once. You have the spreadsheet, the first thing you're gonna do is type an address, it doesn't have to be in the US it could be in your home area. All right anywhere you want just any kind of address, that Google, Maps should be able to find. So. I'll show you the four lines you're gonna you're gonna do. Okay. It's, very very basic, this is one, JavaScript. Function it's got four lines of code and I'm. Just going to describe every. Single, piece of every, single line so you understand, everything now in, general the.

JavaScript. Code that we do for Apps Script should be fairly easy to understand, if you know any kind of JavaScript, you should you should be able to figure this out without, me telling you but, if you cannot that's fine too because I'm going to explain everything, alright, so this. Is one single function this is the only function we're gonna write in this code lab okay. In the first code lab all right these four lines the, first line spreadsheet. App that. Means that whatever spreadsheet, we created, this. Will give you access to, that spreadsheet just. By doing spreadsheet, out when, you're in the editor as soon as you hit the dot the period, after a PP it, will show you the names of all the commands that you can do one. Of them is called get active sheet. Okay. What, does that mean, that, means whatever, spreadsheet. That I'm using right now it's, going to get that object. Okay. Easy, everybody, understand the first line get, the current spreadsheet, the, second one is now that we have the, spreadsheet. Assigned, to the object, in the variable, sheet what, can we do with it I could, say give me the, cell, range. A1, now, I can do more than a1 I can do a 1 :, d5, to. Get a box, of multiple, cells but I just want one so, I just leave it as a1 so whatever is the upper left corner dot. Get, value, which means pull, that out right so you remember you typed in the address or you're going to type in an address once, you have Wi-Fi access, okay. So you will get the address and now it's gonna sign it to the address variable. Alright, and then. On the next one I'm gonna say hey Google Maps dot. Okay. They should have probably called it Maps app but, maybe map app is it sounds too funny, so, it's just Maps dot, create. A new static, map, dot. Put. A pin with the add marker, fit on that, address that I pulled out so. Now Google, Maps is going to make a map and I, assign it to that variable and in the very last line of code. Mail. App, okay. I'm gonna ask Gmail. To. Send, an email to my friend change. The email address by the way don't use that because you're not gonna get any thing. Change it to yours or your friends or something, subject. Line is map and then. For the map object I'm going to get the URL for that map so. What. Does this app do, right. It. Takes the address that you, typed in from your spreadsheet it. Creates a map it, puts a pin, or a marker, on that, map where that address is and then it's gonna mail the URL, of that map to your friend or to you what, you're going to do okay then, you're gonna go check your Gmail then, you're gonna see, that there's, a new email with the subject line map and then, you're gonna click it and then. You'll see a Google map show up on your screen of the address that you typed in okay. So, the magical. Thing about this is that it is only four lines of code but. It uses three different Google, products, right.

It Uses Google sheets Google Maps, and Gmail, all, in just four lines of code now. This is you know this app is not gonna make you rich. Okay. But. It shows you what you can do right, now you just gotta use your imagination and, figure. Out what else can, I do to. Make it useful and if you guys came to my Apps, Script add-ons talked yesterday I gave more examples, of the things that you can do all, right otherwise, if you weren't able to come then just you know watch the video right so the videos will be up very. Soon if not already and. You can watch that to get more ideas of what you can do with Apps Script but the main idea is to get you started with Apps scripts so you know how to do Apps Script and the next time you want to try this you won't be scared you know how to do it maybe, you don't want to do it with a sheet maybe the next time you're gonna use a Google Doc or a Google Form or something else right, so. All those different things all the editors you can actually do Apps Script apps with them I'm, going to slowly walk through, the rest of the code lab ok so I'm just going to go back a little, so, create, a new spreadsheet so that's the first thing all. Right so. Can. You guys raise your hand if you're able to make a brand new spreadsheet. That's. A lot of you okay, okay good all right so you have a blank spreadsheet that's great so. I'm gonna go slowly. Type. An address into the upper left hand corner. Just. Like this, okay. And. Then. The next step is open. The script editor so. Once. You've typed in the address go. To tools and then, pull. Down the script editor. Okay. And then you should see, your. Code editor underneath. So. This will open a new window. Okay. So, then you have this blank right. Here. Okay. Now. I'm just gonna keep on checking to see how far you guys are at how many people have a code, window right now. Okay. A little bit less all right okay, everybody try and catch up try. And once. You have the spreadsheet some, more of you guys raise your hand so you definitely got the spreadsheet, okay. Type in the address and, then. Go to oops. Go. To tools and then. Script. Editor and then, that should open another window. For, you into. The code editor. Okay. And then. So what you're gonna do is you're gonna actually take, the code that I just showed you on the slide just. Copy and paste the whole thing just grab the whole thing and. Stick it into your spreadsheet so I'm just gonna demo demonstrate, it here. Okay, so my address, is. Okay, so I type in this address which, is Google corporate headquarters in, California and, then. I'm going to do tools script, editor. Okay. And, then I have my code editor now, so all you do is just paste the whole thing over this. Okay. So it's the same thing. Except, you know chain obviously you have to change the email address to your email address. Okay, otherwise everything, else is exactly the, same. Exactly. The same okay. Okay. One, of the other things that we're not going to cover in this code lab is the only current, duct all. Right this is an additional. Annotation. Okay. In order. For you to restrict, this, app to only be able to run with this sheet instead, of everybody sheets. Okay. So it's an extra level of security so, you can read more about it in the documentation, okay, so there's a link there. Okay. And then, you have to replace the fake email address with one of your email addresses. Okay. Once. You pasted. The code in, save. It. And, then we can move to the next page. Okay. And then, you can click on Run. Run. Function, there. Should only be one function. You can run. Okay. All, right how many people are able to cut and paste the code into the code editor and save it oh. That's. Pretty good, okay good we. Will finish this code lab on time. Okay. When you try to run it you will see the authorization. What does this mean. Okay. This, means that. Your. Code is trained, this, code. Other. Users, can use it. But. They. Have to give your. Code. Permission. To. Access their spreadsheet, because it is their personal, data. Do. You guys understand, that you, can't just arbitrarily. Just run this anywhere. Okay. That. Person who, owns the data this spreadsheet has to give your app, permission. To run that's, why a lot of times you know when you install an app or when, you run a web app it will say some you know bring up a body log, box that says this, app needs these permissions, and you need to click allow right so, that's exactly what's, going on here is that.

Because, This, could be foreign code right you don't know what go maybe you're just a user and you see this right, you need to be aware of what you're granting access to, you, just don't grant access to everybody, that wants access to your data right so. That's. What this box is is you need to review the permissions, okay since we're in development mode we're gonna get something like oh this app is not verified it's really scary okay. We're gonna try and make this a little bit more simple but you know because we've had some security, issues lately we wanted to make sure the security is very tight so. What's going to happen is you're gonna see something like this were Oh the app isn't verified, so you have to click on advanced and then go to the. One that on the bottom where it says unsafe, and then you have to agree to it I think it's it's, simpler, than this now but you may still have to go through this whole process. Okay. Has anybody seen the warning yet, have, you guys tried that try to run it okay so, you basically have to you know just go through this whole process we're. Gonna try and make it simpler over time but right now it's still pretty, restrictive. Okay. So don't be scared remember this is you this is your app okay. It's not somebody else's app so you can give permission to yourself. Okay. Okay. So that's the next step. We. Have five more minutes left I think you guys are doing very good, okay. And then you'll actually get the real dialog, box which is the thing that says I want. To I want to allow, this. App to. View and, manage my spreadsheets, because, I need to be able to read the, address that you put into cell a1 so. The code is requesting, for that permission it's also requesting, permission to send email as you because, you're calling, Gmail, are. You calling the mail app the mail service. Okay. So those are important, permissions, and you have to click allow in order, to give permission for this code to, do those two things. Okay. Everybody, got this screen how many people have gotten that screen, okay. Good you guys have made it very far okay good we are gonna finish on time all, right so once you click it it sends. An email how, many people have gone to their Gmail and see the link oh wow. Okay, almost all of you okay great so once you click allow you're, gonna see a little a little, yellow, box on the top it says running, this script or something like that okay. And then once that yellow box disappears. The code is done and then, you can go to your Gmail or whatever. Email service you use and then, you should see a link. To. Whatever. Address, you have and then. When you click on it hopefully. You see a Google. Map of. The. Address that you put in right. How many people have seen the Google map, Wow. Good. You guys are making good progress wasn't that cool. Right. It's exciting, now use, your imagination, what else can I do now so, watch, the talk from yesterday I have a whole bunch of videos that show, what else you can do with Apps Script but, congratulations, you've. Just used some G suite in Google api's. Okay. Yeah. Great. We're. Ahead of time now so let's see what else there is in the code, and. What. Else you can do next, okay. So what can you do next. We. Have a whole bunch of documentation, so the.

The, First link is general documentation, for Apps Script the second link is specifically, if you want to find out more about using, Gmail the, third link is if you want to find out more about doing, spreadsheet, stuff the, fourth link is about more. Stuff with Google Maps. Okay. And then, I created a whole bunch of videos I don't have a private life anymore. Okay. So you can watch any of the videos that will help you so the first one is that quick, introduction, it's kind of like this easier than this one where I talk about what is Apps Script why is it a secret because people haven't heard of it before all. Right and then this example, the. Example we just did in this class has, an a video as well too so that's, the. Second one and then I have I have like three more Apps Script videos that you can watch so if you go to that link there you'll just see all of my Apps Script videos but it's Apps Script only though okay. If you want to see other videos that, I did then, that's these two okay. So I have, this is the current, dev show that I'm doing and then this was the previous show that I worked on which, was more for startups but it's still beginner content, just, like this one you can read our blog you can follow our Twitter and then we also have a monthly newsletter as, well too okay. So lots of different things that you guys can do and. Then. The. Next thing we're going to do in this class is move, on to something a little bit harder okay. And that is the G suite in Google API is code lab okay. Once. You finish that one once, we finish with that one then, you guys will have the ability to do the harder ones and then. Maybe look at the reference app as well too okay, so, these are these are harder, than, the intro the other intro one we're gonna do right now and. Then a reference application. Is what, is sort of a medium-sized, application. That uses this API so in other words this is the the. Example for the slides API where you can actually write an entire slide presentation, in markdown and then, it will automatically, convert that to Google slides for you so you can actually get that medium-sized, application, and use it and modify it and do whatever it's on github okay. So it's all open source but it's written by one of my colleagues. Okay alright. So how many people have everything, working for this code lab. You. Feel sense of accomplishment. Excellent. Okay so what I'm gonna do is I'm gonna give, you guys like maybe two minutes to, ask me some questions about, this code lab or anything. Else related to Apps Script and, then, once we finish with the questions for this part then we move on to the next code lab okay. So, who has anybody has questions. If. You ask questions, you will get something. All. Right so question. The. Question is can I get user information in my app I think there is a service, that allows you to get the users email, and. Maybe part, of their profile information, so. Pretty. Sure that's where I answer okay, come and get a something. For the bag its. User yeah. The, way we did here like. Just. One address with the map yep, okay. I mean let's, say we have hundreds. Of users at risk and we just have the single, map with you. Know where all the located, and, exactly. That was part of my talk yesterday so. The. Main idea behind this. Example. That I showed yesterday in the talk is that maybe you're a salesperson and you're trying to visit multiple customers, so you can have multiple addresses, in your sheet and then you can create one map and put a pin on every single one and then also the other thing you have access to with Google Maps is you can actually ask for like driving, directions walking, directions or whatever so you can actually plot, the, best route, to, visit all of them so it had all the I think, the markers, in the URL itself so is there a limit that you could have this much data or let's say I mean we. Have finder, shops, in Bangalore, for example, the person has to visit yeah, and we have all the addresses we just mark it off and give it to him so he can see where it's located and you can plan his day or something like that yeah, I think the, restriction. Is what I am, yeah. That's a good question I don't know the exact answer but you should be able to you may have to create something that's not a static, map you may have to create a dynamic map okay. So there's, just, check the validation. Exactly, okay. Come and get something to okay. How. About you sir okay, one over there, so. The question is that right, now I had to manually go ahead and run the script right can, it be done by a trigger like you know a new event is coming you know something like that is there a mechanism for those kind of hooks or a trigger so, so so the answer is yes you can make it so that if somebody types something into a cell or if a file is uploaded, to drive that you can run some code so, basically, like I, could have a Google Form where people are inputting the routes and this, could automatically drop me a mail in the customize form and mm-hm yeah yeah, like exactly so I'd like as soon as somebody fills submits, a form and, it gets entered into the spreadsheet you can like send them an email to acknowledge, hey you've, entered, something.

Yeah. Okay, great how, about you sir so, actually I was working on a project where you know how to write. Some data from Android, to Google, spreadsheet. Aha so my, question is like is it possible to create, kind of like a URL where, I can put that spiritual, tidy and if. I'll execute that URL then that all the data and all this thing will be there will be written, on it directly go to that spreadsheet. Yeah. Let's wait till after the second code lab because like we, will learn how to do that you won't be using Apps Script you'll be using an API so, that's the part of the second code lab okay. Thanks ask again afterwards, if that still doesn't answer okay, well do two more and then we'll go generate. Charge, oh. You. Know, what maybe not, you might have to use the sheets advance service right, now the sheets, the. Spreadsheet, app is only tied to the last version of the API v3. Right. Now v3, cannot, do that cannot, create charts but v4, API can so. At, 5:20, today I have another talk on the sheets API if. You do want to use the API from a script you can use what is called the advanced service, and that will let you talk to the v4 API and then you can create charts. Real-sized, is just whatever is the maximum size of the type of document that you have so, like for spreadsheets you, can have up to like what 5 million cells or something like that all. Right 2. Million 2 million cells, but. Otherwise. If it's if you can upload it to a drive folder you can get access to the file ok. That's one last question then we're going to move on to the next code lab and then save your other questions for afterwards yeah so again we connected to the external services. That we write, uh-huh. So watch, the talk from yesterday but. Yes you can use URL, fetch to, post, or get to another web service or if, you want to have an external database you can use a JDBC, service ok, it's, got any limit to that. There's. Always limits. There's always limits. It's. One of the easiest, things to do if you want to learn more about a script so if you go to the. If. You go to the documentation. Which. I will do right now if, my, internet is working, ok. There. Is a, quota. So. Under this quota page you will find out what all your limits are like. You can only create, you know 5,000. Calendar, events every day you'd. Only create 1000, contacts, every day, ok. So there's a whole, bunch. Of limits, depending on how much you, know you paying customer, are, you not a paying customer obviously, get higher you. Know higher limits. So. Anyway all your scripts have to run within 6 minutes otherwise, they get terminated. Ok. Your maximum, of attachment, email body size okay so all this stuff is in, the documentation, so if you want to find out what the limits are ok, great, all right let's move on ok.

One More that's it. Yeah. So the question is can we automate doing, tasks with this and the answer is yes you can have time-based. Triggers, that, will run like every night kind of like a cron job in. Linux so you can run at night every night go and fetch data from other sources, and then put it into spreadsheets or do whatever or you. Cannot, port, VB code into it but if you port, your application. From VB into JavaScript. And you can stick it into Apps Script, okay. But you have to like be B to JavaScript, okay. Okay. Great all right let's move on, so. We. Are now on the second part. Okay. So. Again. Congratulations you. Guys have used some G suite in Google API s but. Apps. Script, is very user friendly there's. Actually, a little bit more going on under the covers, remember. That olá permissions. Okay. Well, you know what that is done for you by Apps Script but. As a developer, you normally have to do it yourself so now we're gonna go a little bit lower level, where we actually have, to work on the OAuth code okay, it's not that bad but I need to explain how it works okay, so in, other words Apps, Script, is made easy because, it's made for people who are not necessarily full-time. Engineers, or people that have computer, science degrees you, could be a business person and you need to write some code right. So, that's why Apps Script handles a lot of things for you so that you don't have to worry about it but if you are a you, know more of a serious developer, or you're a software engineer then, you're more likely to use the API is because it's less restrictive. Okay, so, you notice that you know one of you know the last question we asked that we have is can I actually do this in a different language, and the answer is no because Apps, Script only supports JavaScript, okay. And also, you have to edit your code in that code editor so those things are more restrictive, right what if you want to use your programming, language what if you want to use your development, environment, okay. And so. That's what the second part of this code lab is about is learning how to use the REST API switch, is lower level, but it gives programmers. More flexibility. Okay, and you're not tied to the environment. That Google gives you okay. So. That's really the main idea so that code, there cause our api's under. The covers but now we want, to do it manually so that you know how to do that this harder part okay so, those these are the fundamental, building blocks, that you would use to actually integrate, with Google technologies, but you don't, have to do everything, inside Apps Script okay so that's the main idea all right so everybody go to this code lab. Okay. And so. It, would look something like this one. Okay. And then we can take a look at what we're going to do here all, right we're going to create a project using the Google cloud developers, console, we're going to get our Olaf applications. We're going to learn about using client, libraries, we're going to write applications with Google and G sweet API s and then, the demonstration. App the, four line demonstration, app is it, will read your the, first 100, files in your Google Drive files. Or folders in your Google Drive okay. Again you need to have basic internet, access and a browser a Google account that's easy. I'm. Going to do the demos in Python, because I'm a Python guy and also there's fewer lines of code but, if you, want to use another language you can so just go to the other link and get the client library for your favorite language so, you can do it in Java JavaScript. PHP. Dart. C-sharp. Go. Ruby. Okay. There's lots of options so whatever is your comfortable. Development. Language you can use it you don't have to do it in Python even, though I'll do that as the example. Okay, and then we're going to you. Should have some files and folders in your Google Drive because if it's completely emptied it's not going to be a useful, demonstration. Okay, so, that's what we're going to do. Okay. So in this code lab we're going to download the Google API client library for Python, we're going to create a new project in the developer, console, we're going to get those credentials and then we're going to use those credentials okay a lot of it has to do with security of course and oh up so, that's, the one difference, between using Apps Script and the API is you, have to do it more yourself. Okay. Okay. So if, you have a Linux or a Mac machine. You probably have Python installed already, you, can find out what version, you have by doing that it in. This code lab it doesn't matter what version you use, because. The client library works for Python too and Python 3 so you can actually try to see whether or not you, have Python 2 and your computer or python 3 the, only people that really need to install, Python, our PC, users running Windows.

Okay. Those are really the only users. That have to install it unless your company has already installed it for you okay but basically what you can do is you can type python 2 or 3 to see which version you have and then ctrl D to exit, that, ok. And. This. Code, lab also assumes you have the p IP installation. Tool called pip, ok. If you have pip on your computer, you should be able to run pip install -. Capital, you Google, API, Python client with, hyphens in the middle, okay. So run this command from, your command line ok either, it's pip pip 2 or PIP 3 depending on whether you want to do it in Python 2 or 3 but, try and get this installed, okay, so it's, it's shouldn't. Take very long at all if, your internet is working. Ok. And then. The way you tell if everything, is working is that once the installation is finished and. You. Start, up your Python again if you, can type import, API, client, HTTP, lib to an oauth2 client and, you, don't get any errors, and you get your Python prompt then you know it's working if for, some reason, somehow you get completely stuck then just feel free to kind of like maybe look over your neighbors shoulders or just kind of like start talking to people maybe, they can get you your next job. Ok, this, is a community, event, meet. Other people, from different parts of the country you know see. What language, they know. Right. It's fun ok, it's. Fun, meeting new people. Yeah, again you can use Python two or three it doesn't really matter the client library works for both versions. Okay. You don't have to do Python three if you only have Python 2 that's fine it's, not a problem all, of my code works for Python 2 or 3 ok, the. Next thing you're going to do is go to console. Dot. Developers, that Google well you can't really read that. Let. Me go back to this and move to the next page. Ok. The best thing to do is when you're on this page page number 4 in the code lab click. On this because it's the easiest, way of creating. A new project and turn on the drive API, so. Click on this link that's console, developers.google.com. Slash. Start, slash. API question. Mark ID equal Drive okay, and then login to your Google account if you haven't already otherwise. We have to do it more manually, this link is a faster, way of doing it because what will happen is this command, creates. A brand new project and it automatically, turns on the drive API. Otherwise. You, have to create the project on your own and, then manually, turn on the drive API that's, fine too if you want to do that but this one's just more for convenience, it just does everything in one shot ok. So you'll get this screen that's if you've never done any Google projects, before you're, gonna get a screen, that says, you have no existing, project. And. Then a new project will be created and then you, know you have to click yes or no on the questions, and then you click agree and continue, ok. But, if you've already created a project before then you'll get something like this like do I want to create a new project or do I want, to use an existing project, ok. So. Whether. You have one or you don't have one it doesn't really matter but you're, gonna create a new one ok, and. Then. When you create a new one ok, so you can go down here and when you click on it it'll actually bring up all the existing projects, that you have and then, it, will, ask you to either, manage your existing project, or create a brand new one okay so once you've made your selection you can either choose an existing project if you have one that you started that you want to continue. Or, you. Want to create a brand new one from scratch, okay the drive API will be turned on for you okay. And what happens is you, will know the drive API has been turned on when you see something like this where it says the, Google Drive API has, been enabled.

Okay. So. I'll let you guys kind of like catch up to this point so. If. You, created, a project manually, you didn't use that link. Okay. You, would have to turn on the drive API by yourself, and the way you do that is you click on API, library, and it. Shows you all the different API, is that you can turn on there's so many that. You actually have to search for it so here's Google Drive you can see it here but, if you don't want to scroll down because there's too many you, can just search for it and just type Drive up here. Okay. And then it'll just show you all the Google Drive related. API. S and then when you click on it. Okay. It'll say something like try this API or, it's already been enabled so for you guys if, you haven't enabled it'll say something like click here to enable or something, like that so this is tells you a little bit about about. The API okay. So once you enable Google Drive when, you go back to your. Project. When you go back to your dashboard so you get a dashboard, on the left hand side you can see the. The. Dashboard, link and then the library, link is the one we were just at the API library, okay. And we're gonna go look at credentials, pretty soon but I just wanted to get familiar. With this so, once so this, dashboard gives, you all the information about your app that you're working on okay, and down, at the bottom you can actually see all the different API as you have enabled so. For this particular project, that I worked on I have the drive API enabled, I've got the Sheetz API the slides' API the Google+, API the YouTube, API the calendar API Gmail, bigquery. I turn, on a whole bunch of them and you, can do that. Okay and the, reason why you do this is because Google, is not going to make every API available for you by, default you have to actually tell Google which ApS you want to use so that we can go and allocate, those resources for. You okay. So that's why you have to turn it on some, of sometimes some projects will automatically, turn on some other cloud stuff but that's okay you don't have to use it you can also turn them off if you feel uncomfortable that, they're there okay. So when you look at your dashboard you may have some cloud ApS turned on it's very easy to turn it off okay. Just click disable if you're not going to use those ApS just turn it off because you're afraid that you know it's gonna start charging your credit card or whatever that's fine okay.

Turn Off anything that you're not going to use and just leave drive on the, G sweet api's are free, they, don't cost any money so they don't require you to put in a credit card okay. But of course the cloud ones charge money so you have to be a little careful okay. So, how many people have created a project. Okay. And of the those, of you who raised your hand how many of you have the eight drive API turned on now. Okay. So that means that everybody else. Double. Check to see whether or not your drive API is turned on by going to your dashboard and just looking for one if you only have just to drive API by itself that's fine you don't have to have the list of ten that I have is just that I use this project for a lot of stuff. Okay. Alright, so let's move on see, what the next step is I think the next step is credential, so it's getting. A little harder. Okay. So the click go, to credentials. So, either you click on the button to go to credentials, or in your, project. Let's. See. I'm. Just gonna close the sheets, one yeah so. Over here you can also click on credentials right here, same. Thing okay. So whether you click this button or you click on the credentials on the left hand side you, will arrive at a screen, like this and you're gonna have blank right now I already have some IDs, created, okay, but you won't have anything created, yet so I'm gonna talk you through it. Okay, so what we're gonna do is you're gonna on the credentials page, okay. Ill it's, going to see these questions, all. Right like what credentials do I need what API are using the drive API where, will you be calling the API from, ok so, from this, select. Choose. Ok. So where will you be calling the API from, ok. We're going to use a command line tool because, we're just going to different command line so we're gonna do other UI. Okay. Whether you're on a window system or you just want to write a generic Linux, command line tool just click on this other UI. Ok. But later on if you guys want to build other apps using our API so you can choose any one of these is it a web browser app is it iOS is it Android, Chrome whatever. Okay. So you can choose but for now for, our example we're, gonna do other UI, okay. So, why don't we do that. And. So. Once you select that that's what it's gonna look like on your screen and then. It's, gonna say what. Data. Will you be accessing, so, user data application. Data so we're gonna choose user data. Ok. And then it will tell you you need to create an oauth2 client ID. Ok, so create, an oauth2 client ID, and just choose, whatever.

Name You want. Just. Make it generic, okay. And then create client, ID when you're done. Ok. Once you press create, some, gears are gonna whirl around in Google server somewhere it's. Gonna make your credentials, and then it's gonna say do you want to download it ok so it's gonna show you something. Alright, and you're gonna click on download, or you can do it later that's fine too but we should do it now so download this file, ok. You can call it client, ID or, client. Secret, it, either one of these names is fine what's, gonna happen is when it gives you the default one it's going to be client, secret and some really long name so you can change that to client ID or client secret it doesn't really matter just something that says clients something alright, and you can cut off the extra long part cuz it's a big hash number for your project, ID you don't need to have that. Ok. And. Once you download that download, that to a folder that you're working on this the, codelab. Okay. So I'll let you get that done and then I'll, come back and we'll, continue. I'm. Also going, to show you like if you don't use this wizard, what. It looks like if you're gonna do it manually okay, so if you're on this page like, this I don't have the wizard here to ask Oh what kind of app are you writing what kind of api's, are you using where, where, am I going to access this app okay. If I'm gonna create it manually I click. Over here it says create credentials, and then. I will select Olaf client, ID so let me quickly kind of describe, the different types of credentials and why we're creating an OAuth client ID. So. These, are the three types of credentials. That you can get for your projects, the reason why we're picking the middle one is because you're asking. This app to access your personal data which is your files and drive if you're, not, gonna be asking, for authorized, data like that maybe. You want to you know search for YouTube videos or, you, want to look through public, Google+, posts, or you, want to use the Google Maps API to look up a location that is all public, data and, then. Public data does not require, Olaf because, you're not accessing, user information, in those, cases you can use API, key, so API, key is just for simple, API access where. You're not asking, for private, user data if you're, asking, for private, user data you need raw client, ID that's why we're doing that for this project and then, if you later on you want to write code that runs in the cloud and you need to you know the your, cloud vm. Has. To quote, pretend, to be a user then you would be using a service account because, it's basically a robot that is a user, but it's really your app that's, running as a robot right so these are the three types of credentials you can create and the, reason again the reason why we're doing is, because I'm accessing.

Private. User data, okay. So once you select this if you did it manually, it's, gonna ask you the same information like where's, my app going to be running from well it's going to be a command line app. And then I can create create, and it's gonna make one for me I've already created a whole bunch of other ones down here that I'm not using okay. I. Think, yeah. I think I use I'm using this one that I created like six years ago it still works which is great okay. But you can you don't have to use the wizard to create your ID you can do it manually too so if you kind of mess up and you pop out of the wizard and you can, still do it manually here okay how many people have, created their oath to client, ID and downloaded. The file, okay. Great you guys are doing really good we are gonna finish on time you guys are gonna be really happy about this okay great that's, very good alright, so let's move on to the next step which. Is the actual, code and so. Let me read you I'm gonna walk you through all, of. The lines of this code okay. And it's. Only actually I was wrong I thought this was a four line application, it's only three lines the, real application is just down here if you can believe that well, okay I'll count this as the fourth line okay, so maybe there's for everything. Else above it is just, for OAuth something. We didn't have to worry about with Apps Script right so let me just kind of like talk about all these lines of code so at, the very top we. Have this print, function import what, does that do that makes my code run in Python 2 and python 3 because. What happens is in Python 2 we have a print statement in Python, 3 it changes to a print function and so. What I'm doing is if I'm in Python 2 I am gonna ask Python, hey can we use the Python 3 print function so I only have to have one thing because that's gonna break your code that's, one of the reasons why python 2 & 3 are backwards, incompatible, because. Print changes, from a statement to a function, so I want to make my code run in Python 2 and 3 so I'm gonna tell Python to hey import. Me the Python 3 print function I'm gonna use that instead and then, this line of code is ignored in Python 3 because print is already a function, okay, so that's the top line the next, three imports, are importing, different pieces of the Python client library remember, when we did pip install. Python. Client library for google okay, that's just importing. All the things that we need to be able to make our code work okay. And I'm, gonna talk about those objects, as we go through the source code.

And Then the first so the first real line of code you see is a scope, okay. What are the scopes so you know when you get that dialog box it says. This. Application, wants to view, and manage your spreadsheets, this, application, wants to send email as you okay. Remember that dialog, box that Olaf box well all each, of those, lines is one scope, a scope. Is a permission, a permission. That your app is asking for, so in this case I'm only gonna read the file, names and the file type from your Google Drive I don't need to read the file I don't need to upload files, so. I only, need read-only. Access to. The file metadata, so, that is the scope I'm asking, for if you, want to ask for other things like I want to be able to send email on your behalf I want to be able to view and manage your spreadsheets, just like our Apps Script example you would have more than one scope you would have an array of scopes, that, you. Would be asking for for, your users so when your user runs the code then they're gonna see the dialog box that says I want to act I want to read your Drive. File, metadata. I want to view and manage your spreadsheets, I want to send email on your behalf and things like that right so, this is where we're creating the OAuth ourself. Okay. The. Scopes are necessary, because your, app needs, to tell Google, then, who needs to tell your user what, permissions your app is asking for so, it's either one string, or you can have an array of strings, it doesn't really matter okay in this example we only need one okay. So that's the Scopes line and then. In the next line file that storage that says when I get my credentials. From the Google server where do I save, that I'm, gonna save it in a file called storage that Jason, okay. So you that hasn't you have to have these tokens in order to talk to the Google API to, get access to the user data you, don't have a choice okay so just be aware of that okay. So all that information goes, in storage Jason, okay. Then. What happens on the next line is I'm gonna say go and get those credentials, get. Those tokens from this storage file okay, if I do not have any credentials, or my, credentials, are invalid, like they've expired, or something that I need, to go and run the flow okay. And the flow is that big box it says what permissions I'm asking, for okay.

So What, it's gonna do it's gonna go grab your client secrets or whatever you saved your file name as make, sure that you change this file name if it's client, underscore, ID or client, underscore, secret, make sure that line is the same because it has to go and read your OAuth, credentials. Your OAuth client, ID, and then. It's gonna pair that with permissions, that you're asking, for okay. So that's how it builds that big dialog window that the OAuth flow so. That saves the flown to the flow variable, and then, you the next line is to run that flow which is actually, making that dialog box show up for your user which. Will be you okay. So that that person clicks allow so, this runs that flow the first line creates, the flow the second line runs the flow and if, the, user clicks allow, then. Google, will send back an access token so. That you can talk to the API and get access to the data okay. It tells, it I'm gonna someone so I get that access token I'm gonna stick it into stores Jason. Okay. So that's the stuff and then what happens is because Google we're very paranoid about security so. You're at those access, tokens, expire in, one hour so. It's not very long okay, and so what happens is when. It gets the, access, token is returned to you from the Google server it also returns, to you another. Token called the Refresh token okay. And so what that means is that. When. That access token has expired it. Will, use the Refresh, token, to talk to the Google servers and say oh you. Know what this person got this permission, before, can. I get another access, token, please okay. And if that's the case then, that will get saved in stores out Jason so then once you have a valid access token, then you can go and talk to the, Google API and get access to the data but, you always have to have a valid access token and if, you do not have a valid access token that it, uses the Refresh token, to go and get another one, okay unless. Of course you have no tokens at all in which case that's, when the dialog box shows up so, by the time this line ends, okay. We, need to have valid, credentials so that we can make this call to, discovery, that build and ask for, a service, endpoint, to the drive API I want, to use the Google Drive API version. 3 because, that's the newest one alright, and once, I have valid credentials I'm going to create, an HTTP. Client sign. It with their credential, so everything, is encrypted. Okay. And then. Once that's done I have a handle, to the drive API I can now make calls to the API so. What am I gonna do I'm gonna, say Drive, dot files that list which, means give me a listing of my first 100 files that's, the default you can change it to like 50, or 75. Or 200, files but if you don't say how, many files you want it will do 100 by default, and then, I'm gonna execute the command and then, I'm gonna get back a JSON, payload.

And In the JSON payload I will have a key called files once. I get that then those are actually, the files I got from my Google Drive for. Every file in my files print, the name of the file and the type of the file okay. So that is line, by line how, this application works, and then it's, only these four lines of code everything, else is authorization. And that, code you can cut and paste that and drop that into any app that you use that, to talk to any other API, because once you can talk to drive now you can talk to Gmail API calendar, sheets slides, all of those okay. If that code stays, the same the, code above the OAuth code stays the same when. You look in our documentation. Our off. Code is probably going to be longer than seven lines but, because you know I spent so much time doing Python, I basically, optimize it down to seven but if you look at our documentation just, be aware that it may be like 40, lines of off code okay. I just cut it down to seven because I don't like extra code, okay. All. Right so that's what, it does and then. Once you cut, and paste that into some file call it Drive list, okay. So that's the name so, cut and paste this code save. It into Drive lists, copy and paste this whole thing okay. Save, it as Drive underscore. List in the same directory, that you have your client secret or your client ID JSON, file and then, pretty soon you will have a storage, jason, once you run this thing, okay. Let. Me see. Okay. Once you run it all right so why don't I let you guys do that can you guys copy and paste that code and, save it into Drive list and I'll give you a minute and then I'll. Continue. After, that. I'm. Gonna so, we're gonna try and wrap this whole code lab up in about six minutes so I can leave the last ten minutes to show you how. To use the other api's, because, what like I said before. The. All the code at the top stays, the same I'm. Just gonna change the bottom four lines to show you how to use calendar, and Gmail and sheets and slides and all those things so I want, to save the last ten minutes for that so if we can just do this save. It and then when you run it you're gonna see this show up okay. So when you run it it's. Gonna give you some kind of messages, it'll say something about a browser window has been open so you should actually see a browser window with guess what you're oh I flow. Okay. And. Then if your web, browsers on a different machine because you SSH, into a server then you actually have to run with a local web server all right so hopefully you guys can just do it on your own computer now. Okay. And then. You'll get this which. Is some you know the name of your tool. It. Wants to view your metadata, for your files in your Google Drive right, this is the typical. Auth but notice this time we had to write this code with, Apps Script it handled, it for you so, that made it a lot easier right but now we're going lower level, we have to do it ourselves, okay. And so, that's what the the OAuth dialogue is, here. Okay. When you're done you click allow guess. What, as. Soon as you click allow you're gonna see all first. 100, files in your Google Drive folder just pop out once, you get that working that should make you very happy because. They're your file, and you actually use the Google API because that's really exciting, right. So try and get this thing working so I'll give you guys like another. Minute or so to try this working, but. You. Know I'll. Continue afterwards. But try. And work with each other if you if somebody is stuck try and help out your neighbors okay. So please try and get this working if you need any help raise your hands and our helpers, are back there they'll, be help able to help you out okay, so. Raise your hands if you need help. And. Just. For fun I'm gonna try and run it in real life and see what happens. I have, ten thousand versions of this app so, I just want to make sure I run the right one so this looks pretty much exactly the same as the one I have in the code lab except. I moved the the, drive API line down here so it actually have four real lines of code it's. Really these lines, of code that are the same for every app so this is the version that I want to run so.

I Can run it in Python 3 and so. Right here because of the pause notice the pause okay. The reason why it's paused here, it's because it recognized. That my access, token has expired and, it's. Using the refresh token to ask the Google servers for a new access token so I paused, it as well - it. Should be done by now but I paused, it but you notice that there's a pause here - right so I'm gonna unpause, it. Okay. So here are my hundred files. Ok, so. It should work alright, how many people see, 100 of their drive files, really. Exciting, right right. You've got something working now you can actually do other stuff too okay so. Anyway. So that's pretty cool, ok. Awesome. Let's. Make, this bigger. Ok. Great, alright so so we've finished on time and we can get to doing the other parts, of the slides showing you how to do the other api's what can you do now you. Can read more about the, drive API, documentation. We. Have a REST, API which, we just used we also have an Android, SDK. And we also have iOS, version as well you, want to if you want to learn more about other g-suit api's there's the dots there if you want to learn about other, google api Azure dots there if you, want to again do this in different languages, go look at the page for our client libraries if you want to find out a lot, more about OAuth than you really want to know there's, this we, have 10,000. Pages on ooofff ok. So, if you're really if you really find difficult. To go to sleep at night read, the Olaf to documentation. It'll. Put you right to sleep and, you'll be very happy ok. It, makes me dizzy too. All right and, then let's see so I have a video a video. That, is about what we just did in the cut developers, console, I have, another video about the authorization. Code that I walked you guys through, okay. Also, this code lab has a video and a blog post as well okay. And then I have a whole bunch of other videos specifically. For the drive API so you can watch those if you want and then the other videos that I've done then are blog Twitter our newsletter, again okay so. That's it for the code lab, congratulations. What else can we do now all. Right the next step for you guys is we want to do something more serious with Google sheets do, the intermediate, code lab which is where you're, building a node.js app, that. Is a customized, reporting, tool for, a toy, order company okay. And then the the slides rest API has a code lab where you're, you looking. Analyzing. Github licenses. Using bigquery so, you're looking at all the open source projects, out there and finding out what are the licenses, they're using what open source licenses, are they using so using bigquery to do that and then what it does is Auto generates. A slide, presentation, for. Every license, that. It finds okay so that's pretty fun and then there's the mark down the slides converter that I told you guys about okay, cool so we are right on time, so. Let's move on and. Start. This back up again. Okay. So. There's the example. All. Right. Okay. So when you're done you should feel like this. Okay. Should be celebrating, okay so so. Now I want you to come go back and think about the first codelab that we did with Apps Script which is better. Right. It all depends, right you can do some pretty cool stuff with Apps Script you. Can write little apps that, directly, tie it to a spreadsheet a doc a form, you, can write add-ons, to further enhance, your Apps Script apps and to be able to, extend. What. Is available in you, know sheets Doc's lies Gmail, and all that kind of stuff but you can also take your existing, app and integrate that into our. Google applications, okay. But it's also a limiting, environment. In other words you have to do it in JavaScript you, have to use the code editor. You. Can't you're, constrained, environment, you don't have full control okay, again. But maybe you want to have more heavy lifting, you want more flexibility, you want to, use. Your own language your own platform, you want to be able to run through, your continuous, integration tools, you want to have source code, management.

Versioning. Okay, in that case you probably want to use the REST API it's, lower level, but it gives you more control so you kind of decide which, is better for your application. Right, somethin

2018-02-19 21:12

Show Video

Other news