尝试获取Google Calnedar事件列表,但收到错误UnhandledPromiseRejectionWarning:错误:权限不足
下面是我代码示例
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET
);
oauth2Client.setCredentials({
refresh_token: REFRESH_TOKEN
});
/**
* Lists the next 10 events on the user's primary calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function listEvents(oauth2Client) {
const calendar = google.calendar({ version: "v3", auth: oauth2Client });
const response = await calendar.events.list({
calendarId: "primary",
timeMin: new Date().toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: "startTime",
});
const events = response.data.items;
if (!events || events.length === 0) {
console.log("No upcoming events found.");
return;
}
console.log("Upcoming 10 events:");
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
console.log(`${start} - ${event.summary}`);
});
}
listEvents(oauth2Client);
1条答案
按热度按时间ecfdbz9o1#
我认为你的oauth2Client有问题,你没有请求用户授权。
试试我的样品,它应该开箱即用。