Read Gmail Using NodeJs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is absal ali so in this video we're gonna see how we can read gmail using node.js in my previous video we saw how we can read the gmail inbox messages using um postman using the pre-request script where we can just generate the access token then set into uh into the environment variable no need to copy refresh token or access token again and again so we automated in my previous video if you do not have a link just i'll be providing this link in my description okay just to go and look uh before watching this particular video okay so for this video what we're gonna do do is uh first we'll generate access token okay programmatically using node.js then after that we'll be reading reading the messages using this uh apis okay okay get uh messages then we use uh this search api okay using this um we'll search for the emails in the inbox right so after that we'll be just getting the thread id from here and then after that we have this endpoint users user id then messages then this is a thread id or message id so we need to provide this then you can see uh in detail uh in in the in inbox message details on the email details you can see here that is in encoded uh in the base64 format you need to decrypt it right then from the base64 then we can see the actual messages over here so yeah this this all things that we'll be doing uh using node.js right so let's get started in the uh vs code okay first of all uh what we need to do is just open vs code then after that just open um any folder or just create a new folder over here then what i'll be doing is i'll just open the terminal all right so once you open the terminal so make sure you already have a node.js installed in your machine all right so i have already installed if you do not have install already you need to have it okay before starting this so what we need to do is first npm init so we need to create a project npm uh project okay so the package name i'll just make it a default the person also default uh description i can just put something gmail api okay so entry point is index.js then test command you can put uh i can just put node uh then index i'll show you why i have written this okay enter enter uh i can export this okay so license okay just click on okay so you'll see a package.json um file is being created and you can see here uh this is being um this is being created with index main is the index dot js okay the test command here is node dot node index.js basically will be running uh this index dot js file uh using command line command so uh if uh if you see here we do not have any file called index.js so just create that file all right so i have a javascript file so we'll be running this javascript file using npm run command right so yeah first of all what we need to do is um we need to go to postman from postman also you can get the code how to uh you know this um node.js code also you can generate from the postman show you this so this is the get access token api we already have in my previous videos right so if you see here there is a code section a code link here just click on this code okay and you can see there are a lot of options available a lot of uh code snippet available for other languages as well like cs c c sub uh http then java we have uh node.js right then we have php python etc so you can just copy this thing and uh that will work for your respective languages right so in our case what i'll be doing is we'll be using node.js access so access is a is a very good library for apis all right so what i'll do just copy this everything whatever you see here then we'll just paste it in this index.js all right so before that if you see here there is two thing is being written as required so this requires basically is uh dependency right so you need to install this dependency in order to uh it will work for your program all right so yeah so what we do just install this npm i or maybe install axios and also space qs so this is also another dependency uh you can write like this and this will install this two dependency and you can see those dependencies being added in the dependency sections axios and qs right so yeah make sure this works for you all right so the next thing that we want to do is uh we can run this and we can see whether um this is working or not so we have here you can see uh console.log and stringify so basically here we are printing the response right whatever the response you get so this is the axis we um we normally uh use for uh basically this is a library right so this will give us the response okay so let's understand what exactly uh it it's doing all right so this is the import uh the dependency then we have a data so basically uh we are passing some uh data that is uh in the url encoded format that we have provided over here if you see in the body section x url uh form url encoded this is a body that will be providing as a data yeah right so we are creating this client id secret client secret and refresh token grant type etc uh within this data right and then we have a config config is basically a configuration for axis we can define in here also but it's a better to you know separate it for more readable purpose so within the config we have method so which kind of method is it's a post api call so if you see here this is post api call and this is url the the url that you'll be using and the headers so headers also uh like this in the header section whatever you can see um so those thing will be provided so i'll just remove this cookies thing it's not needed all right so yeah and then send config thing and also the in the config itself this data in this data we are using the first one right yeah the body thing and this config that we are passing in the axis all right so once you uh you have done that uh then then section it will give us the response of that right so once you get the response and in case of any error it will go to this particular catch block all right so let's see whether this works or not so how we can do so one way is to uh because uh we are in this uh gmail uh node folder you can see also in this files index.js so you can do like this uh node.index.js all right so if you run this also it works perfectly fine so yeah you can see this access token is being generated right and this is being generated in here uh uh um writing over here all right so yeah this is one way and other way we can run is also uh npm run test so in this package.json uh they said and the script section i have written uh test then nodeindex.js so this will also do the same thing all right so if you see it's access token is being generated all right so yeah so this is how we we generated the access token so one thing we can do is we can just uh reuse this access token we can save this access token and reuse uh for our future uh usage so what i can do is uh basically i can just use uh some variable and just store this access token all right so what we can do is let's um put a variable over here all right so i can just use one variable over here then after that uh what i'll do i'll just use one variable all right this response then data dot access token so from this response object i'll just access the access token and save it in a variable all right so let's see whether this works or when i print this access token all right and you can see here this access token uh we can print it okay we can we extracted the access token from this response and we can save it for the further usage all right so the next thing is uh we use another api all right so for searching the email so this is the one we'll be using for searching our messages all right from the inbox uh so similar way i'll just use copy paste same thing okay i will do it in here so once you get the access token all right so no need to import again because already they are and this is the config for this one and here if you see authorization we used in uh in the authorization section access token so in this case we already know the the access token so we don't need to uh we don't need to uh regenerate the access token so we just reuse the same this access token from here to here all right so let's see how we can do i'll just use this one all right so i use all right so i'm just reading the access token from the previous one okay so once that is being done um here um [Music] same uh thing uh this this is my query okay search query from this email so basically what i'm doing is i'm just searching in here and the same thing so whichever the emails coming from from this user it will uh so okay so there is only one so basically it will show only one um there is only one response all right so let's see whether this works all right so you can see there are some error okay i'll show you why the error occur let's see all right so if you see here uh the access token the initial access token is being printed because of this console all right then the second one is access token uh because of this print but after that it got four zero one okay unauthorized meaning this access token is not able to get it from the previous one all right it is because the asynchronous nature of javascript so before getting this access token basically it's trying to execute the second part that is this one and that's why it's giving 401 unauthorized access token okay so how to resolve so to test this also you can also see here for example i i do one thing i just okay try to print this one and let's see whether uh this is printing this access token or not okay so if you see here all right so if you see here the access token new is being uh printed but there is no uh access token because it's a empty string that's why it's not printing anything but after that it is printing the access token the first one all right so this is the asynchronous nature of javascript that's why it's failing so how to fix it so let's uh make it a synchronous okay let's handle that yeah so we can handle in many ways so for in my case what i'll do i'll just create a new class then we'll define everything there and we'll just reuse that access token and everything from that class to here all right so the class name i would say gmail api maybe gmail api dot js all right so it's a javascript file then class gmail api okay so this is my class then here what i do i write method name get access token all right so this is my method get access token okay and then what i'll do i'll just copy everything uh present inside this get access token thing all right um [Music] till here i think yeah so what i do just copy everything and paste inside here and also the need to import those two thing as well okay so it is also needed there all right so this is my method now okay so yeah and this is my data uh you you change your client id client secret request token based on your uh requirement okay and then this is my config and here the data then here i have the method access token okay i'll be using this access token in here then finally what i'll do i'll just return access token from here all right so before that uh one thing we need to do is we'll make it asynchronous okay so if you see here i have made it as as synchronous and i can use weight and here i can use the async okay so yeah uh that should work and i can just remove this particular person i need it all right so yeah this is my method uh get access token okay so how we can use from this particular class okay from particular uh this uh class in my index.js here all right so let's see uh what we need to do is we need to um import it all right okay so yeah you can import like this this is your class gmail api.js so no need to write the dot js okay if you write noise you so yeah so before uh using it here so you need to do one more thing uh in in here so what i do here is i'll just export this particular class okay maybe i'll just export uh the body sorry the object itself okay so here what i'll do uh module okay dot exports all right equals i'll say new gmail api so this is my uh i'm just exporting uh the gmail this class object itself from this particular uh file all right and i'm using here so basically this gmail is the object of that whole class and we can access any method inside that all right so let's see how we can use use the method so for now let's just comment this particular section for now all right so once you have this gmail all right gmail dot get access token if you can see this method is available and you can go to that class of that method also from here all right so basically this is returning us uh this access token as a string uh yeah so what i can do is i can also print this value okay from there um yeah so to use it i can say const access token all right so this is my access token and or maybe i can directly use here console.log all right okay so same thing i need to npm run test and you can see uh access token is there all right initially the form is the pending then after that it got your access token all right so then the next thing uh what we need to do is um we'll use uh the other method also to search for the email all right so similar way you can use this other zone you can get the code from here and just use in another method okay so what i'll do i'll just create another method over here okay so this one is for searching um gmail okay search gmail okay uh similar way you can use or you make it as a saying and like this and you can just copy and paste uh other things as well from the postman all right i'll just copy this from here okay paste it here in the search gmail and this is same thing and in the pr token section what we need to do is i'll just use uh this is asynchronous way so i'll just use await then this dot get access token so this is the method that we already written the first method all right so i'll be using in here right so once you use that and you can see the query parameter also is there from here all right so you need this and just remove from here and a couple of things that we need to do uh here as well uh you can use uh wait uh don't need i think yeah for the response also uh we'll see how we can uh we'll see what are the response in here as well all right so we'll print it here so let's see how we can uh search this gmail uh we will use this particular one so in here what i do is gmail dot search gmail okay on need this one now so let's see whether this works okay so the access token and you can see the message basically this is the one it's giving so also i can see i can write something here to distinguish it all right so you will see this message over here okay search result okay so basically after search uh you can see this is the id thread id and uh the id of that particular search result uh you can see in here okay so basically what we need to do is we need to get this id from here okay we using this we can get the id from here and store into some variable and we can return that thread id from here so what we need to do is we can just use one variable okay we can say thread id something like that then i would say okay to read this message what we need to do is response and dot data from the data you need to get this message all right so like this then after that uh actually this is the array all right if you see this is the array in this second section this one all right if it is the ra what you need to do i will get the first one okay then dot id so you can get dot id or you can get the dot threat id okay so i'll just get the id from here and i'll save it into uh this thread id and we can we need to use this await all right and here also i need to use async all right so i'll just remove this here all right so this one basically will return thread id all right from this particular method and one more thing we can do is uh here this query parameter we can parameterize uh this also so what we can do is uh we can write over here search item all right and i can just replace it from here and i can see search item okay whatever you search from here uh you can and you will append over here and based on that you can you know use this so now what we need to do is uh i need to use this uh search uh in here okay so we need to change the parameter all right so i'll just clear it yeah and i done so now also it works okay um the search let's see yeah so yeah when i print the thread id and you can see the thread id uh we can retrieve from the response all right so this is the one um method so uh yeah so after this what we need to do is uh we need to basically read the inbox based on this uh thread id so how to do that okay to do that uh basically we need to use uh this api okay messages then thread id so this will give us the uh detailed uh uh detail inbox of the email content okay so this content we can only get a from here okay so what to do so same way just get the code from here everything is okay same all right after that i need to write one more method all right so the one more method name is i can say read gmail content all right so yeah same way i can use async then right and just copy and paste everything uh whatever you saw there all right yeah so after pasting here so if you can see here already gmail content and here uh we need to parameterize this third id okay we got it from the previous this search gmail okay so this one i can pass it in here okay in the function itself then uh that will um search for that particular trade all right and here uh this dot get access token okay this is basically the first one get access token it will go and search for the access token and then we have uh this axios method here we're just receiving the response and returning that the same response from here right and the read gmail uh content all right so once that is being done uh basically this will give us the in detail uh in detail uh uh inbox all right in box detail this whole thing uh you will get in the response from this this particular return data so what i'll do i'll just create one more method okay and that will contain these three all all three uh method combining and that will give us the result so what i do okay i'll say read inbox content basically you can do within the same method also yeah for simplicity i'll just do another one all right and the fight arrow here what we do is uh let's let's do um first the search in box one i'll just use the same um same uh methods that is being used okay uh sorry no need to okay i'll just use uh use this methods over here all right so that we we don't need to call all this uh method uh again and again or multiple times there so what we do we just create one single method that will be called uh in in the client and this three will be used within one method okay in this class so what we'll do okay um i'll say search inbox first search gmail all right and here the search context uh maybe i'll say search text all right whichever the text that you will be passing it will be passed to here and i would say and basically this will return us this will return as the thread id right so what i can do here i can use the thread id and i need to use this uh await then the next thing is the message okay message detail so this read gmail content will give us the uh the whole uh message body all right i can say message is equal to await this dot read uh gmail content all right not inbox content all right so this one will also take uh the one parameter okay that is message id or and that is the thread id that we got it from the previous one all right um what's the error okay i forgot this spelling mistake oh wait all right so once you get the message the next thing uh what we need to do is uh we need to get this data all right in in this response body from here we need to go to um within the payload okay within the payload i need to go to let's see uh you need to go to this uh parts i think yeah after paths there's something called the body and within the body this data so this is the data that we need so basically this is in base64 format we need to decode it right then we can get it in the string format so basically the idea is to read this uh value so how we can do is uh we can read uh from this message basically all right so let's see how we can do that so yeah this is the way that we can access that data okay this is the message then parts and this is the array then body then data all right so this is how we can read this all right and then i can just use um somewhere maybe i can say cons encoded message and to use a wait over here then after once you receive this encoded message you need to also decode it so for decoding it using base64 uh you can use uh buffer all right so this one will uh decode it all right this encoded message and you can get your desired uh response or the body or whatever you see over here you can see using this string all right so basically this is all that you need to do all right and then also you can if you want you can also return from here in the same text maybe i can log also here console.log all right so that decoded a string you can log here and let's see whether if everything is okay first you need to get the get access token and each method these two are reusing this get access token in these two right like this get access token and after that uh just you got the uh from the response body you got the encoded message then decoded it and then that's all so yeah so we can use this read inbox content in the here all right so no need to write uh all other things what i can do is gmail dot all right console i can actually move because already done all right so inbox content all right so this is a read inbox content so you can rename it a little more meaningful i believe it's not a more readable yeah we can just rename it later on so read inbox content uh this method we can just call over here and you can use this query string search query string from here and this should work let's see whether this is working or not uh maybe i'll just clear everything and yeah and you can see uh this is working fine all right and to see this particular thing you need to get the email okay i sent one email to this particular mailbox okay so if i search for this from this all right and the first thread it will get this all informations so let's see whether this is giving us or not all right let me clear it npm all right so you can see um yeah dear uh aws customer this is the aws account this is this all right so everything is you can see uh in here all right and you can reuse this uh anywhere like this all right so yeah that's all and also i can just show you a little more information about this search query string so it's not only from uh you can use many other things all right you can go in the developer.google.com and then you can just search for advanced search syntaxes so there i have used only for from and any email and you can also use using two and you can also use the subject right the subject name of the email uh meaning uh using this this one uh or you can use there are a lot of other methods uh labels uh using or hash attachment or something you can use all right this all yeah so this is how you can use this gmail uh api using node.js and you can implement it also one more thing you can do is you can also save this get access token somewhere in this um in here and you don't need to uh you know call this access token again and again uh for a reusable uh point of view so what you can do you can just create a constructor so let's do that as well okay so i created uh i created this uh constructor all right so uh this constructor when we create the object of this particular class and this constructor will call once right so that time only it will generate the access token and store the access token into this uh variable all right so once you uh you have done this and you can reuse this access token uh in all the methods uh without calling the let's see where it is and this is the get access token donate to that this one so instead of calling this dot get access token i'll just use uh the variable all right that we have already used so this dot access token so basically this will uh reduce uh api calls basically to generate the access token again again so let's see whether this is working or not all right so you can see uh this is working perfectly fine all right so yeah this is how you can do so i'll be uploading this um uh this piece of code into github also i'll be sharing the link in the video description okay yeah so that's all uh for this video please subscribe my channel okay yeah and also you can comment anything you want to know okay yeah bye for now
Info
Channel: OpenSourceAutomation - Afsar Ali
Views: 19,603
Rating: undefined out of 5
Keywords: gmail api, gmail using nodejs, read gmail inbox, read gmail inbox programmatically
Id: GHXT59krs5E
Channel Id: undefined
Length: 41min 56sec (2516 seconds)
Published: Sat Sep 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.