Getting Started With Google APIs For Python Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so if you are just getting started with using a cloud service such as google cloud amazon aws or microsoft azure for the first time then can be an intimidating experience not having a single idea on how to get started so in this tutorial i'm going to walk you to how to get started using google apis in python so google cloud has over 100 products vision ai cloud storage workspace and so on and when i say google apis i'm actually referring to the api under the google workspace on biola such as google sheets gmail google drive youtube and so on and as for the google cloud apis such as cloud storage cloud scheduler cloud function fusion ai video intelligence those services require different setup now let's look at the agenda for this video all right so for this video i'm going to first of all show you how to create google cloud project next i'm going to show you how to create all off page then we're going to create all up to account and we'll create the client file that ties to the of2 account then i'm going to show you how to enable google apis in the cloud console and finally we'll install the google api python clients library and i'll show you how to connect to those services in a python script now let's start with item number one which is going to be create google cloud project all right so open your browser then navigate to console.cloud.google.com and it's going to be the home page of the google cloud platform on the top we'll have this a drop down it's going to list out all the projects that is currently created in my google cloud platform account and right now i have let's see i have six projects and each project represents my app i'm going to create a new project and i'm going to give my project name and let's call this a project yosemite then i'm going to click on create and once the project is created i'm going to select the project right so we have done item number one the next step is we're going to create an or page all right so on the top right hand corner click on the navigation menu then go to apis and services and here i want to click on oauth consume screen on the user types page we can either set the app it's only available to internal users i can set the user type to external i'm going to choose the user type to external then i'm going to click on create i'm going to name my app yosemite app they want to provide the user support email and most likely this is going to be your email and we can provide app logo so if you intended to use google api service for commercial uh purpose then we need to fill the app domains information but for now i'm going to leave these three fields blank and for the authorized domains i'm going to skip that step as well and here we need to provide the developer's content function i'm going to use youtube at learndataanalysis.org email address then click on save and continue alright so on the scopes page i'm going to skip everything and click on save and continue and just because we can specify the scopes in our python script on the test users page iphone to grant other users to be included in this project then click on add users for now i'm going to click on save and continue review the or consent screen installation and once you verify everything is correct and we can move on to the next step and which is going to be creating an all up to account i want to click on navigation menu apis and services then i'm going to click on credentials all right so here we have two different types of account that we can create one is an api key and let's put this uh straightforward then we have the op 2.0 client ids and the other account type is the service account now go to the top and click on create credentials and i'm going to select off client id now i'm going to click on the application type drop down and here choose your application type for this demonstration i'm going to choose desktop app i found that using desktop app as the application type to be the easiest method to connect to workspace api or google apis now i'm going to give this account name and name this account jj yosemite account one then click on create now we have officially created an of client account and here's your client id and your client secret and make sure that you don't shoot this information with anyone i'm going to click on download json to as a download file i'm going to save the json file to my project folder i'm going to name the account account one unscore your 70 and click on ok to close the page and just in case if you lost your uh client file then you can go back to this page and click on this download icon to redownload the file right so have finished these two items now i'm going to show you how to enable google apis now click on navigation menu apis and services then click on library so before you can use the google api service we need to enable the service first so if i want to use gmail api service then i'll search for gmail and i'll click on gmail api then enable the service to make the service active and we need to do the same thing for google drive api so i'll search for drive and now enable google drive api now i want to go back to the home page so one of the questions a lot of people ask is how do you check your api usage if you want to check your api usage you can click on go to api overview and below this table here we're able to tells you the api usage for each api service and you can also change the interval to 30 days 14 days or even by the last 12 hours all right so the next item is going to be install google api python client library so here i'm going to launch my vs code and i'm going to start a new terminal so these three libraries are going to be the libraries that we need to connect to google api servers i'm going to concatenate the libraries into a single line so i can say pip install dash dash upgrade then i'll insert the libraries name space then i'm going to insert another library's name so that means that we'll be able to run one single command to install multiple packages and i'm going to take this command and i'll copy and paste enter and once the google python client library installed we can now write our python script to use different api services so from the import statement let me increase the font size from the import statement i'm going to import the os module then from google.transport that requests i'm going to import the request class next i'm going to import the credentials class from google.of2 credentials i'm going to import the credentials class and to launch the authentications page locally from google underscore off unscore o of loop dot flow i'm going to import the installer app flow class from google api client dot discovery i'm going to import the build function and just in case this one's optional but it's always good to input different air type so from google api client dot is i usually like to use the http error type to check my interception against here i'm going to create a variable called client file and it's going to be the file path point to the client file that will just download it now to go into let's do gmail's documentation all right so if we go to our gmail for developers page and if we go to reference documentation so anytime when you are connecting to a google api service you'll need to provide three things you'll need to provide the api name the api version and the scopes so on to uh scroll down towards the bottom shadow says all right so let me do a google search i'm going to search for gmail scopes all right so if you go to the choose of scopes page so here are all the scopes so by default the mail.google.com scopes will grant the app for access to the accounts mailbox now going back to gmail's api documentation so usually how i figure out the api name and the api version is by either going to the scopes page i can look at the url so here's the api name and for the version sometimes you'll just have to look at the resource version id and for gmail is going to be b1 and for google drive and for google drive it's going to be v3 and here's the api name so for demonstration purpose i'm going to show you how to connect to gmail api since the code is going to be the same except the api name and the api version plus the scopes is going to be different all right for the scope value i'm going to use the mail.google.com scope so i'm going to grab the scope value and it's going to be a list because you can provide multiple layers of scopes so here i'm going to create a variable called credentials and let's call the variable credits and i'll set the default value to now so this variable is going to store the access token that allows you to access the api so first we're going to check if a token file is generated and this will be token.json if the token.json file is not generated then we're going to go ahead and create the credential json file and for now all right let's assume that the token.json file is created so phone credentials the thumb authorize uses file we can simply supply the token.json file to till the api dial we already authenticated our account and we need to provide the scopes otherwise we're going to generate the token.json file and here i'm going to check if not credential file is not created or if the credentials object actually is going to be object not viewable so here i'm going to change this to nut credentials if the credentials object is not valid usually when the credentials object is invalid is most likely due to the credential is expired so here i'm going to insert another if statement if the credentials object is created but the credentials is expired and the request token here this should be credentials that refresh token exists they want to refresh the token i'm going to insert instance of the class class and this will be refresh otherwise i'm going to launch the authentication page using the install afflow class the phone client secrets file function also the claim file variable and the scopes list and i'll name the output as flow then we're going to create the credentials object so from float run unscored local server so here while using localhost as the default host and for the port number we're using a0 since i'm using a desktop app as the application type i can randomly assign a port number so i'm going to assign let's do 0 just so that there's no other port number is going to be complex with the a0 port number and once the credentials object is created so this credentials object is going to be the access token that allows the app to connect to different google apis so i'm going to save the credentials connection as a token file so i'm going to name the file token.json and it's going to be rightmo as token then we're going to create a file and we can do that by using the token.write and we can convert the credentials detail as a json file alright so if i go ahead and run this code block and it's going to launch this page and here's the app name yosemite app then i'm going to uh choose my account that i want to grant the app access to let's take a look all right so uh let me try again i feel like i'm missing something okay so this time it worked i don't know what happened the first time i guess uh it takes maybe a couple of minutes for the project to be fully activated all right so if i go into my project folder and here we have this token.json file created now this time if i run the python script again and it's asking me to authenticate my account okay so let me take a look oh i know why because i have a typo this should be credentials now if i press f5 to run script because we'll be authenticated our account and therefore we created this uh token.json file that's to our access so the next time when i try to access to an api i can simply use the token.json file to bypass the authentication step now let me go ahead and run this code block to create my credentials and to connect to an api service i'll just name this as service underscore gmail i can insert the build function and i'll insert the api name gmail version id and for the authentication from credentials we're going to provide the credentials option now go ahead and create a service gmail object if i use the third function to print the members we now have this user's member that basically reference yeah let me go back to reference page that basically reference the uh gmail resource which all starts from this user's resource or method so i just want to add one more thing so i created this app and it's only for windows unfortunately but basically i create this application here let me launch the application first basically this application will make a request code to google api's inventory database and it's going to return all the api services that are currently available so here we have the api api version and the api api name so when you want to use an api you can grab the api name in the api name column then you can grab the api version and copy these two properties to your python script so this is going to be everything i'm going to cover in this video and hopefully you guys found this video useful and as always thank you guys for watching i'll see you guys in the next video
Info
Channel: Jie Jenn
Views: 25,921
Rating: undefined out of 5
Keywords: google apis, google cloud, google api python
Id: PKLG5pfs4nY
Channel Id: undefined
Length: 22min 37sec (1357 seconds)
Published: Wed Apr 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.