My overall goal is to use an API to populate a google sites page with current bookings for study spaces. The service uses oath2 and Authorization: Bearer ACCESSTOKEN. I'm embedding Ajax - jQuery in the google site to do this.
So far, I can use the authorization endpoint to get the token back and send it to console.log. But how do I store it into a variable that I can then send along with my next request to the actual data endpoint? Obviously, I'm completely new to this and I'm stuck.
Here's what works
$.ajax
({
type: "POST",
url: "my.url/oauth/token",
data:
{client_id:"715",
client_secret:"secret",
grant_type:"client_credentials"},
success: function(data){
console.log(data)},
});
The console then shows something like the following data:
"\"access_token\":\"78098453jkdg0ehgjf09etuca117bed9430740a08ac2b3e97c3d0\",\"expires_in\":3600,\"token_type\":\"Bearer\",\"scope\":\"rm_r sp_r\"}"
For the life of me, I can't figure out how to get that into a variable instead of the console. I've tried declaring a variable and storing it, and I've tried sessionStorage.setItem. My next step would be (I think) to store that response into a variable ACCESSTOKEN and then use it in my next request.
$.ajax
({
type: "GET",
url: "myurl.com/endpoint",
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ACCESSTOKEN');
},
success: function(data) {console.log(data)
},
});
At that point, I'll need to figure out to take it from the console again and have it output the data into a table or something.
Any and all help would be greatly appreciated! kmk