node.js - 错误:列出用户谷歌日历事件时未设置访问、刷新令牌或 API 密钥
问题描述
我正在实现一个代理,它从他的谷歌日历帐户获取用户事件,以使用节点 js 提醒他即将发生的事件。我浏览了如何实施授权以访问用户数据的指南。但是当调用一个函数来列出事件时,我得到了这个错误。这是我的配置文件
const port = 3002;
const baseURL = `https://a2727d9a7058.ngrok.io`;
module.exports = {
// The secret for the encryption of the jsonwebtoken
JWTsecret: 'mysecret',
baseURL: 'https://a2727d9a7058.ngrok.io',
port: port,
// The credentials and information for OAuth2
oauth2Credentials: {
client_id: "xxxxxxxxxxxxxxxxxx",
project_id: "xxxxxx", // The name of your project
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_secret: "xxxxxxxxxxxx",
redirect_uris: [
`https://a2727d9a7058.ngrok.io`
],
scopes: [
'https://www.googleapis.com/auth/calendar.readonly'
]
}
};
这是我的主要文件
const express = require('express');
const google = require('googleapis').google;
const jwt = require('jsonwebtoken');
const dfff = require('dialogflow-fulfillment')
// Google's OAuth2 client
const OAuth2 = google.auth.OAuth2;
// Including our config file
const CONFIG = require('./config');
// Creating our express application
const app = express();
// Allowing ourselves to use cookies
const cookieParser = require('cookie-parser');
app.use(cookieParser());
// Setting up EJS Views
app.set('view engine', 'ejs');
app.set('views', __dirname);
console.log(dfff)
app.get('/', function (req, res) {
// Create an OAuth2 client object from the credentials in our config file
const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
// Obtain the google login link to which we'll send our users to give us access
const loginLink = oauth2Client.generateAuthUrl({
access_type: 'offline', // Indicates that we need to be able to access data continously without the user constantly giving us consent
scope: CONFIG.oauth2Credentials.scopes // Using the access scopes from our config file
});
return res.render("index", { loginLink: loginLink });
});
app.get('/auth_callback', function (req, res) {
// Create an OAuth2 client object from the credentials in our config file
const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
if (req.query.error) {
// The user did not give us permission.
return res.redirect('/error');
} else {
oauth2Client.getToken(req.query.code, function(err, token) {
if (err)
return res.redirect('/');
// Store the credentials given by google into a jsonwebtoken in a cookie called 'jwt'
res.cookie('jwt', jwt.sign(token, CONFIG.JWTsecret));
return res.redirect('/');
});
}
});
app.post('/', express.json(),(req,res)=>{
//if (!req.cookies.jwt) {
// We haven't logged in
//return res.redirect('/');
//}
const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
const calendar = google.calendar({version: 'v3' , auth:oauth2Client});
const agent = new dfff.WebhookClient({
request : req,
response : res
})
function welcome(agent){
agent.add("Hi, I'm Rem. Please click on remind me button if you want to be reminded on your upcoming events!")
}
function listEvents(agent){
return calendar.events.list({
'calendarId': 'primary',
'auth':oauth2Client,
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 1,
'singleEvents': true,
'orderBy': 'startTime'
}).then((err,response)=> {
let events = response.result.items;
if (events.length !== 0) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
agent.add('Be ready for '+ event.summary + ' event '+ 'at ' + when )
}
else {
agent.add('You dont have any upcoming events.');
}
});
}
let intenMap = new Map()
intenMap.set('Default_Welcome_Intent',welcome)
intenMap.set('Remind_me',listEvents)
agent.handleRequest(intenMap)
});
// Listen on the port defined in the config file
app.listen(CONFIG.port, function () {
console.log(`Listening on port ${CONFIG.port}`);
});
知道为什么吗?
解决方案
推荐阅读
- javascript - 使 ESLisnt 规则在函数中不使用 async/promise
- c# - StreamWriter 不刷新
- firebase - 跨所有选项卡获取 Firebase 会话
- git - 使用 SSH 的 Maven JgitFlow 插件身份验证失败
- python - 无法使用 python 脚本获取 api 密钥
- c - c语言作业与incr/decr运算符
- datetime - 日期小部件/输入显示保存不一致
- java - 如何断言某个字符串至少包含列表中的一个值
? - python - 当我将列表传递给另一个方法时,它返回 NoneType。我怎样才能解决这个问题?
- nativescript - 在 nativescript 中按下标签时,在标签或 css 类开关上达到 css 悬停效果的最佳方法是什么(角度)