Power Your Apps with Google APIs with Wesley Chun (GDD India '17)
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.
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.
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.
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