Google API - OAuth 2.0 - Get AccessToken and Refresh Token Part-1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is absolutely so in this video you're gonna see about how we can access the Gmail Google Drive Google see it at Sur try using cool API is right Gmail API is Google Drive API will seed API everything so we'll discuss about that in this using Java how we can access those okay so to access the Gmail this cool driver we will see it we need to use OAuth 2.0 to access Google API if you go to this website developers.google.com then identity protocol or two so this particular site if you see there are steps already available there so you need to follow those steps and you will be able to enable both - authorization and so the first step is obtain worth 2.0 credential from the Google API console so first you need to go to this particular Google API console then we do get a credential from there we'll see how we can get that and then second one is obtain access token from Google authorization server so basically when we hit the Gmail API so we need access token or authorization token right so in that case we'll be getting this from in the next step and after that this access token to an API so basically we'll get the access token in here we'll we'll be setting some scope and everything so we'll see that and then when we hit the API we need to send the access token in that and the Refresh token refresh token is basically something that we can generate our token a new token using this refresh token so this would be constant for that particular account that gmail account that will be using suppose I'll be using this one right so it will have something called one or refresh token and there will be another call something called auth token okay so this auth token it will be expired each time okay like if you sign into Google right so after sometime like 30 minute or one hour when you try to rephrase that your account got locked locked out right because this auth token is expired for that session or maybe session as well you'll get so this Refresh token will be used to generate a phrase auth token right so we will see how we can do that as well okay okay so now the first step is create a project so to create the project so just go to the same URL this URL I'll be giving this URL in the description as well and in here in step one first a visit Google API console I'll just follow this documentation as well and go to consoled a developer dot google.com okay in here if you already logged in with your account so this is the new account I created so I will be using this training Gmail okay 8900 gmail.com so I logged in already in Chrome browser so it's asking me it's already logged in right so if it is not logged in in your case so you know to login using your username and password then you will see this particular page like API and services page that that is the consult or developer go to google.com okay so this is the page now the next thing that we need to create is we need to create a project over here so if you have already a project then it will ask you to select from here but we don't have any project over here so what we'll do we'll create a project that means this means this one I think your free trial is awaiting something we don't need it now that's fine so just click on create and it will create a project maybe any name you can provide a YouTube training okay organization is fine I created on project now ice creating you okay so now it seems one project is being created and he said enable API and services okay you can see in here YouTube training my project and this also you can see your project and you know to click on this enable api's and services so if I click on that it will ask you which API is you want to enable for this particular project so you can do anything like gee sweet like all Google Drive API a calendar API Gmail API or Google site API so all the API is available you can select from here I'll hang out so those are the API is available and or also you can select the YouTube API as well Google+ API so for now what we'll do we'll just enable for Google API okay enable this you fine so it will enable the Gmail API okay so you can say saying disability that means it's already and I will fine if I go back you can see here the Gmail already go to here service a dashboard then I can see the Gmail already there so whatever the API enable it you can see in the list over here in this particular section right so this is now quick now I need to go to create a credential as well okay up then the first one done then obtained access token from the Google authorization service so within this console the developer you can see the reset credential section as well within our newly created project Europe training so within that you can see in here credentials okay so the credential you need to enable the credential so just click on this create credential and you need to click on this earth client ID okay so if I click on this so it will ask you to select something but the first time it is disabled because you need to configure consent some some concern you know to configure so click on that consent or maybe just first go to the or the consent screen then go to the credential okay so in here you need to select whether its internal or external okay available to any user with a Google account you can say like that or maybe if you have a your domain or something you can specify like within my internal company you will be able to access this API or this Gmail thing alright so for now I will be just submitting sectional and then create and now it will go to here and you can just provide any name like the project application name if you have already one application you can do it or maybe I can just write anything training okay and then analyst click on save over here and now I got this one consider what you've done so the next thing I'll just go to the credential again go to the what client so now it's enabled so I'll select the web application maybe this one a YouTube application something like that or any name you want to give you can provide there then here authorized redirect URL so if you the new application already you can provide your application URL or if you don't have then maybe just write localhost so it will redirect to the localhost only okay so this is turn then create click on create so now you should see this your client ID and your client secret so just save this it is needed the latter steps so just copy the first one client ID and I can say this is my client ID and other one is client secret okay so this is my client secret so this is done so then go to this again and go will calm this whole place OS - then you need to go to here this one using OAuth 2.0 for web server application so we need to create some credential as well so just go to this particular link okay so once you go there so you can do using different language go java.net noches PHP Python Ruby etc programmatically you can create but it's this simple if you do from command line so I'll be using command line first to generate those credential and auth token and refresh token yeah so that would be easy for us so for that what you need to do is this go till here HTTP or arranged API so this is my URL you can say this is the base URL and these are the parameters that we need to provide okay the first thing what we can do if you go maybe just get the one example over here so I will just copy this example okay and and paste here here okay so in here you can see the different things different parameters okay like scold access type include grant something maybe it'll just make it that is larger so now it is increased her you can see here so here what you need to do this cope scope is something I need to get it from here if I go to the web page again so for school you can see something called here in this section just open the scope API scopes okay so in here you can see for the API scopes which API that will be using will be using Gmail API right so I can say in Gmail API scoop okay so jemelle API so in here you can see the lot of scopes scope means something what do you want to restrict a particular user it's suppose in your Gmail you say you can say only someone can send the email or maybe only they can read the email they cannot send the email right or maybe they can only see the email only read on the access you want to provide so kind of the restricts and what you want to give this is only if you want to only send message only no read or modify privilege on a mailbox something like that and the last one is full access so they can do each and everything what you can do after logging in your Gmail right so everything you can do and yeah there are different different scopes available so I'll be using the last one okay and just paste that here okay after that it'll be and then access type let it be offline okay include granted scopes is a true and the state state parameter passed through value and the this one would be HTTP redirect URL will be the localhost HTTP localhost and the response type would be code client ID and client ID you know to provide client ID we already called it from here you can see this is my client ID okay so I can eat use that finally here and the response type 1 code will be generated right so fine what we can do just copy this thing and go to I can use the cuddle command or what you can do we can just use in browser Isis so if I go to Chrome browser and in here I'll just paste the same credential that whatever we copied ok and just hit enter so oh this way this is ask you first to sign sign in so once you give the your credential and you are able to sign in then this page will come up this app is not verified or something go to youtube training app on Sep so just click on that and safe one so here energy is provided allow okay so once you provide allow and you can say you are in this page here also no two provinces allow okay so now you can see in here at the redirect URL provided the local host in case if you have provided your website then it will go to your website just copy whole thing from here okay and go to the text and here just paste that one so this is your code you can see the code that we wanted we got it from here already okay so now the next step so once that is being generated second so in here we follow this this one or what code we got already right in the last step here this one so then the next step would be to generate our refresh token and your auth token so using this particular step file okay so refresh and access token we need to get it using this what thing so what you need to do is just copy this thing I'm here okay and I'm just skipping here we'll just form one after request fine so the cord here I need to just change the code code would be this one you can see in here this is my code till here and okay the code I got it from there find the client ID so the client ID we already got it here the first one just change your fine ID then time secret I hear I got the client secret as well and the redirect URI so the redirect URI I can say the same as localhost okay so this is my redirect URI and the authorization code grant type is authorization code so this will give me the odd code okay so then what I need to do this if I go here and just would curl command okay in the from the command line okay so in the postman so I just put this one the best URL would be HTTP over to door Google API comm slash token and in the body okay the body section just click on the raw and paste this rest code from here okay once you question over here and make sure you and I have a selected application xwu form URL encoded okay once that is being done this click on send so in here I can see I'm getting some issue or this is a bad request it seems because it's URL encoded when we send some special character in the URL it should be encoded right so I don't know maybe some issue with URL encode I encode so for that so what I have done is I'll be doing the same post command it should have been a post man I'll be doing enough here in the terminal so you have to use this curl command so this curl requested the post and then data and here it should be the authentication code from the holding the code that we received from here the first one okay once we received that code just paste it here and the client ID we already know the client ID we got the first one and the client secret also we know the second one okay and then I you then you need to provide the redirect URL okay so just put anything I in our case HTTP localhost so same thing I have done in here you can see so first is the code the second one is the client ID then laughter on is the time secret so once you get the client secret okay then we redirect you are a little release then grant type authorization code so just paste this whole thing okay so this one I'll be providing in on description as well this copy and paste in your command line so once you paste over here and enter then you will get the access token and the Refresh token okay so I'll just put it in here the response of these so we got the access token this is the auth token and this is expired in this 3600 millisecond or second I don't know okay then hurry first I think it's a one hour validity one hour validity and in every one hour it will be changed so in a to get the new access token right and using this refresh token so this is more important so you if you have this refresh token you can get the access token any time okay so we'll see how it will get that so till now this is good so we got the access token and the Refresh token as well okay so now you can see we are here till now okay and this here what's true for server-side web app so we got the access token token type II error refresh token then the next step would be like you can call the Gmail okay get this so before that let me just show you another one if this token the token that we received okay this access token it is a expire then how to get the new refresh token okay a new access token so in here if you go down refreshing an access token offline access so in here what I need to do is if you have this a client idea we know we have a client secret we know and that if flash token we already know so if we give this three information it will give us the access token so this access token you can use reuse again okay so we'll see how we can do that as well okay so for getting a new access token using refresh token this is the curl command so this one also put in the description okay so it's also post request a client ID you know to provide the client wood client ID here the client secret okay this is client secret then the Refresh token that we received from the previous thing previous request okay and the response so this is the access token this is my refresher one so this would be the constant so this will not change okay so once I provide this refresh token over here you can see the Refresh token and this one will give me the access token right so let me just see in in here you can see let me just use this if I copy here and pass this then I'll be getting new access token each time okay so this is my access token I can reuse so suppose I want to call any API maybe let me just check for testing what you can do just go to [Music] yeah get response we will ap is a drive Albu to access and we have the access token as well so this thing what I can do oh maybe I'll use the Accord command for this this one I'll let me find thee they stood here I think they responded before one hour okay so this will last for one hour and let me just put this one sorry and this one this is for tripe we will drive but we need for Gmail right so in the next video see how we can use this access token to get access to the Gmail inbox and how to send email and these are the Google API II you can see in the Google API mmm you can find a lot of informations maybe sample okay so in here the differences you can see these are the HTTP requests either a region profile is already stopped user ID watch the there are a lot of rest API is available for that okay so we'll use some of them and in the next video okay so thanks bye for now
Info
Channel: OpenSourceAutomation - Afsar Ali
Views: 116,978
Rating: undefined out of 5
Keywords: Google api, gmail, gmail api, gdrive api, gmail oauth2.0, send email using gmail api, gmail automation
Id: tGDn3V-mIOM
Channel Id: undefined
Length: 28min 5sec (1685 seconds)
Published: Tue Dec 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.