Переменная область в вызове API Календаря Google с Node и Angular - PullRequest
0 голосов
/ 15 октября 2018

В настоящее время я пишу приложение, использующее Angular / Node / Express, которое будет извлекать данные из моего Календаря Google, используя вызов API Get.Сначала я сосредотачиваюсь на настройке сервера.Я следовал некоторым учебникам и настроил свой файл server.js соответствующим образом:

// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');

// Get our API routes
const api = require('./server/routes/api');

const app = express();

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));

// Set our api routes
app.use('/api', api);

// Catch all other routes and return the index file
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/newcal-angular/index.html'));
});

/**
 * Get port from environment and store in Express.
 */
const port = process.env.PORT || '3000';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));

Я следовал Страница быстрого запуска Google Node.js и получил их шаблонный блок кодав моем файле "server / routs / api.js":

const express = require('express');
const router = express.Router();

let events;

/* GET api listing. */
router.get('/', (req, res, Data) => {

  res.send("gonna start sending things");

  const fs = require('fs');
  const readline = require('readline');
  const { google } = require('googleapis');

  // If modifying these scopes, delete token.json.
  const SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
  const TOKEN_PATH = 'token.json';

  // Load client secrets from a local file.
  fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading client secret file:', err);
    // Authorize a client with credentials, then call the Google Calendar API.
    authorize(JSON.parse(content), listEvents);
  });

  /**
  * 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.
  */
  function authorize(credentials, callback) {
    const { client_secret, client_id, redirect_uris } = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, (err, token) => {
      if (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 console.error('Error retrieving access token', err);
        oAuth2Client.setCredentials(token);
        // Store the token to disk for later program executions
        fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
          if (err) console.error(err);
          console.log('Token stored to', TOKEN_PATH);
        });
        callback(oAuth2Client);
      });
    });
  }

  /**
  * Lists the next 10 events on the user's primary calendar.
  * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
  */
  function listEvents(auth) {

    const calendar = google.calendar({ version: 'v3', auth });
    calendar.events.list({
      calendarId: 'primary',
      timeMin: (new Date()).toISOString(),
      maxResults: 10,
      singleEvents: true,
      orderBy: 'startTime',
    }, (err, res) => {
      if (err) return console.log('The API returned an error: ' + err);
      this.events = res.data.items;
      if (this.events.length) {
        console.log('Upcoming 10 events:');
        this.events.map((event, i) => {
          const start = event.start.dateTime || event.start.date;
          console.log(`${start} - ${event.summary}`);
        });
      } else {
        console.log('No upcoming events found.');
      }
    });
  }
})

module.exports = router;

Похоже, все настоящее волшебство происходит внутри функции "listEvents (auth)" и переменной, которую я хотел бы вернутьВернемся к моей функции Angular после вызова Get, который содержится в переменной this.events.

Вопрос : Как мне это сделать?Насколько я могу судить, весь мой код выполняется правильно: когда я запускаю "node server.js", я вижу, что все правильные данные отображаются в моей консоли, и когда я перехожу к localhost: 3000, я вижу, что "собирается начать отправкувещи".Я просто не уверен, как вывести переменную this.events за пределы ее функции и отправить обратно.

Спасибо за вашу помощь!Я искал документацию по API Google в течение нескольких дней, и, к сожалению, в нем нет языка Node, и я все еще новичок в реализации асинхронных операций / ожидания / обещаний / и т. Д.

...