如何使用Google-api和nodejs发送会议请求?

时间:2017-10-11 18:50:46

标签: node.js google-api google-calendar-api google-api-js-client

我正在尝试向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 个答案:

答案 0 :(得分:0)

Create Events说明了如何创建日历活动并将其添加到用户的日历中。

要使用nodejs创建或添加活动,您可以使用documentation中提供的代码:

// 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);
});
  

您可以选择在创建日历时添加事件元数据   事件。如果您在创建过程中选择不添加元数据,则可以   使用events.update()更新许多字段;但有些人   字段,例如事件ID,只能在期间设置   events.insert()操作。

您还可以使用实时数据查看try it中的API请求和响应。