Access Notion API with 'OAuth2 for Apps Script' library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hey so uh this was my first time using the google apps script oauth2 library essentially i wanted to do something with the uh with the oauth2 authentication inside apps script and i've never used this before and given that notion just had released their api well a couple weeks ago i figured it might be a good opportunity to essentially test it out and that's what this video would be about if you're a first time uh oauth2 user inside apps script i think this video would be for you and uh um uh of course i'm no expert on this topic so i'm gonna copy and paste a whole bunch of code and be able and see if we can sort of get this done in a much easier way so okay couple things one we will we we of course get navigated to developers.notion.com just keep that open you can also open up your my integrations page because we will have to create a new app so to speak or create a new integration um you also need to have of course an uh a notion account so make sure you have one um perhaps keep the oauth to library uh github page open as well you'll have a lot of instructions here we'll be sort of working with a whole bunch of different um documents um or folders there so yeah let's get started you can create a standalone project for this you don't really need to open google sheets or docs or slides or anything of that sort let's name this i'm going to say notion api doesn't matter you can name it anything you want um what i'm going to do next is essentially go back here and uh so i actually went through the documentation already and i highly recommend you do that even if you don't understand it just make sure you read whatever is written get yourself familiarized with some of the steps that are involved in using the library some of the terminologies for example the authorization the redirect url the token user callback endpoints and a whole bunch of other jargons along the lines so just read the documentation it always helps to know what's what and you know where can you find what kind of resources and what's the word that you're looking for and where is it available it always just sort of helps um the same thing goes to the the developers documentation as well in our case we can navigate to the api reference since we are still trying to authenticate we can click authentication so it gives you a whole bunch of things here and we can go to the guide the guide is rather a better place to start um and so of course notion also provides you with a a bear token of sorts um so you can make use of the better token if in case you are uncomfortable with oauth 2 but this video would rather focus on overcoming those fears and actually making use of the oauth 2 authentication so i'm going to go to the authenticate authorization public integrations and that's what uses oauth 2.0 and of course make sure you read you know the documentation here you'll see we'll need some of these endpoints later on um there are a couple limitations since the the api is still in beta but that's okay it doesn't really matter for the purposes of our uh demo uh but but of course make yourself uh familiarized with the workflow in which you know they are saying okay the step one would be to rather you know go to this link once you do that you're asked you you're prompted to essentially select one of these options then you redirect you to another place and then a whole bunch of other things happen so it's it's always just good to sort of read through the documentation okay uh enough of rtfm um let's actually go and get something done so my approach since this was the first time i'm doing this um then i'm one of those people who's like rather lazy when it comes to writing the code from the scratch so what i did was i went on to samples and sort of you know eyeballed my way through what sort what kind of was you know is something it's easier um i couldn't figure that out so what i did next was i opened up a whole bunch of different um examples and sort of again this is my uh process you are free to apply your own process i still try and read the code as well just to see which one is the most basic and minimal and based on that sort of take a call for example in this case it's asking us for another access point key and we don't really have that and i don't think that's something we'll need at this point in time so i'm going to close that basecamp actually was one of the examples that i ended up using because the code seemed very minimal and i could sort of understand the uh authorization url the token url and a bunch of other things that that are fairly minimal to what we really need to achieve at this point in time so having said that and for the purposes of this video i'm going to take the example of the base camp uh code that already exists um so let's get started so let's say i'm going to go ahead and just copy the whole bunch of things uh right from top to bottom and just essentially delete this and paste it here i'm going to save the project so i just use the command s and okay so and this is something the client id decline secret are some things that that almost every single oauth um authentication wordpress for some authentication some apis ask for additional information like the the api key that we were looking at um and the way that we would be able to get that and you'll sort of find this information in the uh and the in the guide where is our guide in the guide here i suspect um somewhere around here so essentially you'll have to create your own integration um and essentially we will need a public of authentication so of course go through it but in our case you can just go to my integrations when you click that uh you'll be redirected to this page essentially you can click on your integration um let's give it a name i'm gonna say apps script or let's just say google apps script and yes so see your workspace let that be notion you can upload an image if you want um i don't have one right now so i'm gonna go ahead and hit submit uh it all automatically gives us uh the internal integration token that's essentially if you're trying to keep it um private but i want to make it a public integration and here is where a whole bunch of complexities sort of arise so there is this concept of a redirect uri if you're unfamiliar in very common or basic terms think about every time you have logged into any website um where you know you're logging in either using twitter google github or any other tool essentially once you have authorized the access to that particular application you are redirected back to the original uh page from which or the original tool from which um you would have actually asked for authorization right so this essentially means that and you will also get to know about that word in the o author documentation what i do when i hear a new terminology which is something for fancy or something scary which i don't understand i just do a command f and then c redirect and there you go so you do have an entire section covering redirect uri and the way you can get it so in our case um it is of course a very basic thing you can see here there is a schema um uh if you're a regular abstract user you might have seen this already but the only difference is you end up getting the user callback and you can also get this programmatically if you don't want to copy paste this thing the approach to get this programmatically is via this function it's the log redirect uri i suspect we have this function right about here as well already so i'm going to go ahead and say log redirect uri i'm going to select that and hit run uh give it a couple of seconds since this is our first execution it will ask this to authorize it with the new v8 and the new schema it just takes a little little time so i'm going to go ahead and review permissions uh this essentially is another example of an oauth workflow you see um you go ahead and give it a whole bunch of permissions and you redirect it back here ah my bad so before i could run anything the obvious thing that i had to do here for instance and then the instructions are here was to go to my resources libraries and essentially add this as a library of course the documentation here represents the the old version in the new version you have the libraries right here you can click plus the script id just paste it look it up um it's always better to use the latest version so i'm going to keep it to 41. do not change the identifier well you can but i would recommend not to because the entire code is sort of dependent on this identifier here so i'm going to go ahead and add it just give it a couple more seconds now you can see what two available you already have the logarithmic url selected hit run once again and bam you have now the url that you need to actually make stuff happen so i'm going to go ahead and copy this go back to my integration you know paste the redirect url right here website or home page so you do need to enter some of these information and of course some of these things are not really mandatory so i'm going to go ahead and type my um log your free to put whatever else you need well let's actually see if it if it's if it's except without adding any data no there you go right so it does require you to add something so the privacy is this i know this by heart because well i created these pages terms support i'm just gonna write sarah code.gs and go ahead and hit submit sure continue and now we have the client id and the client secrets i'm going to go ahead and copy this go to my don't worry i'm going to delete these anyways so it doesn't really matter if it's exposed so it asks me for the client secret so i'm gonna here continue copy go back here and sort of paste it that's that go back what else is needed right nothing nothing else is needed cool everything else looks good um and essentially this is part is rather step one you can of course uh remove these comments but authorize and make requests to the notion api i'm going to just replace that the next thing that we need to do uh you can er you can you can ignore the run function for now we will get to this in a minute uh let's start with the get service because this is the part where uh the the real ops to work begins so it says it starts with create service i'm going to replace basecamp with notion it asks us for something called as the authorization url and this is something which is a very common in almost all the other tools as well so if you go back to the guide you can just do a command f and say authorization check it out and let's see well there it is actually authorized right this is the url that we would need in order to to um get the authorization to work also authorize the app i'm going to go ahead and save that the next thing the other url which is also fairly common is the token url so you can just scroll down a little bit um keep going and essentially you'll find another url well notion has made it quite easier because they have documented a whole bunch of things really nice really well but you may not always find these links directly available so of course make sure that you read the documentation the best you can and and of course do a whole bunch of trial and error um as well and in our case we can actually leave the rest as is so we have the authorized url the token url what else right yeah everything looks everything else looks okay so i'm going to go ahead and essentially configure the run service now so the run service essentially is the run function is essentially once you do have access let's say you know first you you ask uh or or or you basically get the service details or rather instantiate this then you check if at all the service has access if it doesn't then it asks you to authorize um based on the url that it provides right so what we have here is basically a test of sorts um in terms of once you do have access what would you want the api or what do you want the service to do what api do you want to authorize once you get the credentials right so let's go back here and i'm going to go and you know essentially open up the api reference once again um let's see so what i really wanted originally was the list databases that's the end point i want to use it's simple get request doesn't really do doesn't really harm us in any way however it looks like this endpoint is rather no longer recommended and it's true if you try running this end point you basically get empty data sets you get empty results so it asks asks us to rather do the search instead so i'm going to go ahead and go to search so this is the end point let's i'm going to just place it here and this would be our url however this is not a simple get request it is rather a post request so what we would also want is war options and let's configure the options here we already have headers um i'm gonna go ahead and cut it and just paste it here i'm gonna delete these brackets right here and type in options instead one of the other things with post requests is we will also need to type method well actually let's you don't have to guess what you need i'm going to open up the documentation and let's look it up this is the one just give it a couple seconds it'll take a minute to show here yeah so you have the method as you know the the post um and the payload which is essentially the body of the post request that's also something that we can use or i'm not sure if we need to but let's actually check it out so i'm gonna go ahead and copy oops my auto automations very fine microsoft upload that's weird um so let's actually go ahead and copy this options part go back to the code as you can see i'm a big copy paster so i would copy paste everything if i can and avoid uh having to write any kind of code so headers i'm going to go there here delete these options put a comma so let's go back here and see if you really need the body parameters no the body parameters looks optional you're of course free to sort of add whatever you think is needed one of the things that you do need with um the notion apis is the notion version in the headers so i'm going to copy that or you can see this example right here we can actually copy this entire thing and let's go back here under headers i'm gonna start with that um put in those double quotes comma and that should do it uh there is no payload so i'm gonna go ahead and remove that and i'm gonna select all function f1 format document so that it looks a little bit nicer okay let's actually just quickly review this you have a post function um headers are these um and the options are being fed here yeah the ideally you should be this should this should really work in an ideal scenario but let's see um oh one of the other things of course is the mute http exceptions it's always good to use this parameter so that you can log the error that you get to see and i'm going to go ahead and add that as well uh let's say true and yeah yeah that's good so the next step is essentially to run it the first time you run uh of course this will be false because you do not have access so it is supposed to ask us or rather it is supposed to essentially log the authorization url that we'll need to you know go back to the browser and authorize it since this is server side authentication so i'm going to go ahead and hit run there you go open the following url and rerun the script so i'm going to copy this right click go to da da da and now bam there you can see google apps script that's the that's the app that we have configured here as you can see there it is and that's what it's asking for um you can of course read a whole bunch of things that it says here the links that we configured are right here i'm going to go ahead and say allow access and let's see what happens oh it says error retrieving token invalid client so this is something i already encountered so i'm not going to act surprised like this the first time i'm seeing it there's something i've already seen so one of the things um which after running into this i did was to go back and read the documentation once again and and it turns out if i go back to the authorization guide somewhere in and this happens during the token exchange and this is more of an intuition of what i think is the case because what we were supposed to get was once you had you know granted once you once we were prompted this page it was supposed to take us to another page where we could select a whole bunch of different pages that this particular integration would have access to and unfortunately it did not do that um so essentially i was sort of reading through a whole bunch of things and it turns out there is something to do during this particular token phase wherein the client id and client secret is essentially encoded in base64 format now all of this may sound a little more intimidating a little more fancy a little more confusing um however one of the other you know the best things about these things is anytime like i said anytime you see a new word anytime you sort of encounter something fancy that you're not aware of just copy paste and see where it sticks so what i did was so it says base64 and i already knew what base64 is to an extent i've worked with apis before so i went i copied that essentially went to the um uh the the home page of the library and did a command f command v and it turns out it's not that um uncommon for some of the apis to ask us for this information and you have this information readily available i want to add that right so thanks to whoever documented these things the good folks at google so you can see here setting additional token headers some services require uh the authorization header to have access token as well right and the way you can do that is by just literally copying this um go back and just scroll down to the get service function because that's the function um that we'll need what i what i'm a big believer of is do not disturb the existing ecosystem i'm just gonna hit couple enters uh paste my code okay it shows a small uh red underline here that's because it's already terminated i'm gonna go ahead and delete that and i think we should be good yeah just set the uh uh formatting a little bit save it um and let's hit run once again so i'm gonna go ahead run um it asks us to authorize once again because obviously we did not have um uh the authentication flow completed the first time around so i'm gonna go ahead and close this we no longer need that so i'm gonna go here again um let's say let's hit it um once again it is asking us to allow access let's say i hit allow access and this time around it did redirect to an end point called user callback and it is now displaying a success message to that that's good news to an extent right because the success is coming out of this part right here which is auth callback which is essentially what it means is once the authorization is done once the url has been redirected uh you do need to show uh it's it's customer to essentially show a success or denied of course some application just close this window i don't know if that's possible really but um it's always good to see okay if either the the approach was a success or if we were denied um but good now that it's succeeded we will have to rerun this once again and because now we have authorization let's see what comes up um now that we run it at least a couple things are clear that the search api was successful it did not header out and everything actually went really smoothly you actually do have access to the oauth flow that we just ran for the notion integration that you have created this is actually a success however of course you don't have the data yet and and it turns out that happens because the first time that we authorized or or we went through the flow um uh it essentially did create a better token or rather a token from the notion side and um uh the second time that we were running it doesn't create a new token because the one already exists and as you might recall oops i just closed the tab as you might recall uh we do need a flow where the first time you are prompted to allow access you basically select pages and then your redirect you are supposed to be redirected to a page where you can select a whole bunch of different pages to begin with um and this is a bit of a brain teaser because i wasn't sure what we could do here however let's say select pages pages i did read something let's see where can i find those resources again [Music] let's see let's see uh integration during the authorization page picker there it is right so this was a bit of a clue the first time around when i was going through the documentation so it says during reauthorization the page picker will not be shown um right so that's because we are sort of doing the reauthorization we already had authorized it the first time or kind of authorizes the first time and the second time around it doesn't show up and after doing a bit of trial and error the the solution in a way is you go to settings in your notion workflow click on integrations and bam you already have something called as you know integration here google apps scripts go ahead and just disconnect this um to begin with and now go back to your google apps script project first we will go ahead and reset the authentication that we've already received the the kind of um authorization uh token that we've already received let's go ahead and reset it so that's okay that's that takes a quick second here i'm gonna also close that go ahead and change this function to run i'm going to click on run again let's reauthorize this application um and this time around you now get to see an option to select pages and that's a good sign already so you can click that and you can select which of these pages you'd want to allow this integration to have access to you can of course select the entire thing but let's actually say this this and this and go ahead and click allow access give it a minute uh success good close this i'm gonna rerun the same function and let's see oh damn this time around as well we just have a list is that because there are no databases in these pages it could be i don't know uh let's see task list i don't remember if i authorized task list but let's do that once again i'm going to reset this and let's do run once again but it wouldn't because i already have this is a bit of a twist uh i did not anticipate this happening but let's see let's try and debug this together i'm gonna uh remove the authorization there uh click this run again get the new url go ahead and let's authorize everything and then see if something sticks so i'm gonna go ahead and say allow everything along access and let's hit run once again bam there you go okay so the first time around the pages that we selected just did not or might not have had any kind of information to share i don't know perhaps but now as you can see um the search option did actually get triggered you have all the data that you need uh right here so essentially this is what you can do to um authorize um notions api using google apps script and the oauth 2 library that's the beauty of this entire workflow and if you're if you're somebody who's just doing this for the first time of course please keep these tips in mind uh make sure you read the documentation copy the code as much as you can because there are some very nice samples available already um and in case you have any questions yeah you'll feel free to reach out to me i am reachable on code at script.gs or um essentially sorry about script.js you can reach out to me at any any of these any of these emails but uh yeah i hope this video helps in case it didn't or if it did feel free to let me know either ways thanks thanks for watching
Info
Channel: Sourabh Choraria
Views: 495
Rating: 4.75 out of 5
Keywords: google apps script
Id: LrFKAoyQwj4
Channel Id: undefined
Length: 27min 3sec (1623 seconds)
Published: Fri Jun 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.