npmopen in new window vuejs3open in new window

VueGapi

Google API Client Libraryopen in new window wrapper for Vue.jsopen in new window

⚠️ Deprecation

The Google Sign-In JavaScript Platform Library is deprecatedopen in new window and will be fully retired on March 31, 2023. This plugin will not be receiving new features.

We would encourage you to migrate your application to Vue3 Google Sign-inopen in new window which exposes a number of Vue 3 composables built on the new Google Identity Servicesopen in new window library.

Requirements

Version 2 requires Vue 3open in new window.

If you are looking for a Vue 2open in new window compatible version, use Version 1open in new window.

Installation

npm install vue-gapi

Usage

Install the plugin with gapi.client.initopen in new window configuration parameters:

import { createApp } from 'vue'
import VueGapi from 'vue-gapi'

const app = createApp({})

app.use(VueGapi, {
  apiKey: '<YOUR_API_KEY>',
  clientId: '<YOUR_CLIENT_ID>.apps.googleusercontent.com',
  discoveryDocs: ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
  scope: 'https://www.googleapis.com/auth/spreadsheets',
})

Composition API

Inject the GoogleAuthServiceopen in new window instance via useGapi:

import { useGapi } from 'vue-gapi'

export default {
  setup() {
    const gapi = useGapi()

    function login() {
      gapi.login().then(({ currentUser, gapi, hasGrantedScopes }) => {
        console.log({ currentUser, gapi, hasGrantedScopes })
      })
    }

    return { login }
  },
}

Options API

Reference the $gapi global propertyopen in new window accessible inside the application:

export default {
  methods: {
    login() {
      this.$gapi.login().then(({ currentUser, gapi, hasGrantedScopes }) => {
        console.log({ currentUser, gapi, hasGrantedScopes })
      })
    },
  },
}