0

私はgmailに会議出席依頼を送信しようとしていますが、googleapisとgoogle-auth-libraryライブラリを使用してGoogleカレンダーに登録または保存しています。 Googleカレンダーに予定を送信できますが、その予定をメールとして送信できません。だから誰も私にこれをやる方法を助けることができます。私はさまざまなオプションで以下のコードを試しましたが、動作しません。Google-apiとnodejsを使用して会議出席依頼を送信するにはどうすればよいですか?

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

答えて

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': '[email protected]'}, 
    {'email': '[email protected]'}, 
    ], 
    '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()操作中にのみ設定できます。

ライブデータを使用してAPIリクエストと応答をtry itに表示することもできます。

関連する問題