gpt4 book ai didi

node.js - 将事件插入谷歌日历nodejs

转载 作者:太空宇宙 更新时间:2023-11-03 23:20:08 25 4
gpt4 key购买 nike

我正在阅读有关将事件插入谷歌日历 API 的文档,但没有取得任何成功。我能够按照快速入门指南并从我的谷歌日历中提取事件列表,但现在我无法让插入工作。我保持环境设置不变,只是添加了插入方法,但我的错误是联系日历服务时出错:错误:权限不足

const fs = require('fs');
const readline = require('readline');
const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const TOKEN_PATH = 'credentials.json';

// Load client secrets from a local file.
try {
const content = fs.readFileSync('client_secret.json');
authorize(JSON.parse(content), insertEvents);
} catch (err) {
return console.log('Error loading client secret file:', err);
}

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
* @return {function} if error in reading credentials.json asks for a new one.
*/
function authorize(credentials, callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
let token = {};
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);

// Check if we have previously stored a token.
try {
token = fs.readFileSync(TOKEN_PATH);
} catch (err) {
return getAccessToken(oAuth2Client, callback);
}
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', code => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
try {
fs.writeFileSync(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to', TOKEN_PATH);
} catch (err) {
console.error(err);
}
callback(oAuth2Client);
});
});
}



function insertEvents(auth) {
const calendar = google.calendar({ version: 'v3', auth });
var event = {
summary: 'Google I/O 2015',
location: '800 Howard St., San Francisco, CA 94103',
description: "A chance to hear more about Google's developer products.",
start: {
dateTime: '2015-05-28T09:00:00-07:00',
timeZone: 'America/Los_Angeles'
},
end: {
dateTime: '2015-05-28T17:00:00-07:00',
timeZone: 'America/Los_Angeles'
},
recurrence: ['RRULE:FREQ=DAILY;COUNT=2'],
attendees: [{ email: 'lpage@example.com' }, { email: 'sbrin@example.com' }],
reminders: {
useDefault: false,
overrides: [
{ method: 'email', minutes: 24 * 60 },
{ method: 'popup', minutes: 10 }
]
}
};

calendar.events.insert(
{
auth: auth,
calendarId: 'primary',
resource: event
},
function(err, event) {
if (err) {
console.log(
'There was an error contacting the Calendar service: ' + err
);
return;
}
console.log('Event created: %s', event.htmlLink);
}
);
}

最佳答案

我认为您的范围对于使用calendar.events.insert()是正确的。那么这个修改怎么样?

  1. 我认为范围可能尚未反射(reflect)到访问 token 中。因此,请删除 credentials.json 一次,然后再次授权范围。这样,它会再次创建credentials.json。并且可以检索反射(reflect)范围的访问 token 和刷新 token 。
  2. 在您的脚本中,console.log('Event created: %s', event.htmlLink)event.htmlLink 变为 undefined 。因此,对于您的脚本,请修改为 console.log('Event created: %s', event.data.htmlLink)

关于node.js - 将事件插入谷歌日历nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50807375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com