我有一个火力地堡的web应用程序,
并希望这样的任何用户可以登录并授权我的web应用程序来访问他的谷歌日历(读/写)在客户端和服务器端(管理日历,当用户在线时,当他的下线).
在客户端.
在Google开发者控制台上创建API密钥和OAuth 2.0客户端ID(Web应用程序)后,我实现了以下代码:
首先,通过Firebase Authentification登录Google
firebase.initializeApp({apiKey: 'MY_WEB_APP_API_KEY'})
var provider = new firebase.auth.GoogleAuthProvider()
provider.addScope('https://www.googleapis.com/auth/calendar')
firebase.auth().signInWithPopup(provider)
Run Code Online (Sandbox Code Playgroud)
并且,要求授权我的Web应用程序可以使用Google Api JS客户端访问用户的Google日历
// https://apis.google.com/js/api.js
gapi.load('client:auth2', () => {
gapi.auth2.authorize({
scope: 'https://www.googleapis.com/auth/calendar',
client_id: 'MY_WEB_APPL_CLIENT_ID',
prompt: 'none',
access_type: 'offline',
include_granted_scopes: true
}, () => {
gapi.client.init({
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest']
}).then(() => {
return gapi.client.calendar.events.list({
calendarId: 'ANY_USER_CALENDAR_ID' // example: …Run Code Online (Sandbox Code Playgroud) javascript google-api google-authentication google-api-nodejs-client firebase-authentication