VueJS + Google Calendar API
Example
by Andris Vilde
HTML
<script src="https://apis.google.com/js/api.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<div class='authentification'>
<h2>VueJS + Google Calendar Example</h2>
Authentification
<button v-if='!authorized' @click="handleAuthClick">Sign In</button>
<button v-if='authorized' @click="handleSignoutClick">Sign Out</button>
</div>
<hr>
<button v-if='authorized' @click="getData">Get Data</button>
<div class="item-container" v-if="authorized && items">
<pre v-html="items"></pre>
</div>
</div>
CSS
body {
font-family: "Open Sans", sans-serif;
font-size: 14px;
font-weight: 300;
line-height: 1em;
}
.authentification {
margin: 20px;
text-align: center;
}
hr {
border: 1px solid black;
margin: 30px;
}
pre {
outline: 1px solid #ccc;
padding: 5px;
margin: 5px;
overflow-x: auto;
}
.string {
color: green;
}
.number {
color: purple;
}
.boolean {
color: blue;
}
.null {
color: magenta;
}
.key {
color: black;
}
JavaScript
// Client ID and API key from the Developer Console
const CLIENT_ID = '<YOUR_CLIENT_KEY>';
const API_KEY = '<YOUR_API_KEY>';
// Array of API discovery doc URLs for APIs used by the quickstart
const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest'];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
const SCOPES = 'https://www.googleapis.com/auth/calendar.readonly';
new Vue({
el: '#app',
data() {
return {
items: undefined,
api: undefined,
authorized: false
}
},
created() {
this.api = gapi;
this.handleClientLoad();
},
methods: {
/**
* On load, called to load the auth2 library and API client library.
*/
handleClientLoad() {
this.api.load('client:auth2', this.initClient);
},
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
initClient() {
let vm = this;
vm.api.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(_ => {
// Listen for sign-in state changes.
vm.api.auth2.getAuthInstance().isSignedIn.listen(vm.authorized);
});
},
/**
* Sign in the user upon button click.
*/
handleAuthClick(event) {
Promise.resolve(this.api.auth2.getAuthInstance().signIn())
.then(_ => {
this.authorized = true;
});
},
/**
* Sign out the user upon button click.
*/
handleSignoutClick(event) {
Promise.resolve(this.api.auth2.getAuthInstance().signOut())
.then(_ => {
this.authorized = false;
});
},
/**
* Print the summary and start datetime/date of the next ten events in
* the authorized user's calendar. If no events are found an
* appropriate message is printed.
*/
getData() {
let vm = this;
...