I am trying to send meeting request to gmail as well as registered or stored it in google calendar using googleapis and google-auth-library library. I am able to send events to google calendar but I am not able to send that event as a mail. So can anyone help me how to do this. I have tried the below code with different options but its not working.
我正在尝试向gmail发送会议请求以及使用googleapis和google-auth-library库在Google日历中注册或存储它。我可以将事件发送到谷歌日历,但我无法将该事件作为邮件发送。所以任何人都可以帮助我如何做到这一点。我尝试了下面的代码与不同的选项,但它不起作用。
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
const moment = require('moment');
var SCOPES = ['https://www.googleapis.com/auth/calendar'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.error('Error loading client secret file: ' + err);
return;
} else {
authorize(JSON.parse(content), createEvents);
}
});
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.error('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
}
function createEvents(authClient) {
var calendar = google.calendar('v3');
calendar.events.insert({
auth: authClient,
calendarId: "primary",
resource: {
start: {
dateTime: moment().add(1, "m").format(),
timeZone: "America/New_York"
},
end: {
dateTime: moment().add(3, "m").format(),
timeZone: "America/New_York"
},
attendees: [
{ email: "***********************" }
],
reminders: {
useDefault: false,
overrides: [
{ method: "email", minutes: 1 },
{ method: "popup", minutes: 1 }
]
},
summary: "Testing google apis",
description: "Testing apis "
}
}, function(err, something) {
if (err) {
console.error(err);
} else {
console.log(something);
}
});
}
1 个解决方案
#1
0
Create Events explains how to create calendar events and add them to your users' calendars.
“创建活动”介绍了如何创建日历活动并将其添加到用户的日历中。
To create or add an event using nodejs, you can use the code provided in the documentation:
要使用nodejs创建或添加事件,您可以使用文档中提供的代码:
// Refer to the Node.js quickstart on how to setup the environment:
// https://developers.google.com/google-apps/calendar/quickstart/node
// Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
// stored credentials.
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);
});
You can optionally add event metadata when you create a calendar event. If you choose not to add metadata during creation, you can update many fields using the
events.update()
; however, some fields, such as the event ID, can only be set during anevents.insert()
operation.您可以选择在创建日历事件时添加事件元数据。如果您在创建过程中选择不添加元数据,则可以使用events.update()更新许多字段;但是,某些字段(例如事件ID)只能在events.insert()操作期间设置。
You can also use live data to see the API request and response in try it.
您也可以使用实时数据查看API请求和响应。
#1
0
Create Events explains how to create calendar events and add them to your users' calendars.
“创建活动”介绍了如何创建日历活动并将其添加到用户的日历中。
To create or add an event using nodejs, you can use the code provided in the documentation:
要使用nodejs创建或添加事件,您可以使用文档中提供的代码:
// Refer to the Node.js quickstart on how to setup the environment:
// https://developers.google.com/google-apps/calendar/quickstart/node
// Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
// stored credentials.
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);
});
You can optionally add event metadata when you create a calendar event. If you choose not to add metadata during creation, you can update many fields using the
events.update()
; however, some fields, such as the event ID, can only be set during anevents.insert()
operation.您可以选择在创建日历事件时添加事件元数据。如果您在创建过程中选择不添加元数据,则可以使用events.update()更新许多字段;但是,某些字段(例如事件ID)只能在events.insert()操作期间设置。
You can also use live data to see the API request and response in try it.
您也可以使用实时数据查看API请求和响应。