离子框架与谷歌日历api

时间:2015-12-17 08:51:49

标签: ionic-framework google-calendar-api

我正在使用Ionic框架来创建一个类似于组织的会议室预订的应用程序。我是Ionic框架的新手。 我需要你们帮助以下思考 1).Oututh for Google signin 2)通过带有访问令牌的发送请求访问Google日历 3)需要获取请求的JSON响应。 4)而且,重要的是需要获取资源日历(房间,投影仪等)

请指导我如何用Ionic框架做到这一点。现在我还没有得到任何好的教程。

先谢谢!!!!

3 个答案:

答案 0 :(得分:2)

@arun,我在应用程序中开发了一个使用ionic2和集成谷歌日历的小型混合应用程序。您可以看到应用@ Google calendar in Ionic 2 app demo on android device的快速演示。 如果这是您正在寻找的,我相信您在问题中提到的谷歌oauth登录,访问日历和发送邀请的内容都包括在内。我不确定你是否正在使用Ionic2进行开发。如果是,请参阅演示视频和详细步骤。 Demo and steps to integrate google calendar in the Ionic 2 app

答案 1 :(得分:1)

离子框架是AngularJS的集成,是跨平台的最佳框架。其中有大量支持 ngCordova插件

我有一些链接

  1. 对于Google oAuth - http://blog.ionic.io/oauth-ionic-ngcordova/
  2. 对于Google日历 - http://www.raymondcamden.com/2015/09/18/integrating-the-calendar-into-your-ionic-app
  3. 你可以参考Intel @ XDK是最好的工具之一。我用它来开发交叉开发应用程序,它支持IONIC的多个框架。

答案 2 :(得分:0)

我一直在寻找将谷歌日历中的数据整合到Ionic中的时间,但没有任何运气。

我现在已经能够通过我在node.js中的后端将它集成到我的离子应用程序中,我建议你也这样做。

这是我在后端使用的代码

let eventCategories = require('./event.model');

let fs = require('fs');
let readline = require('readline');
let google = require('googleapis');
let googleAuth = require('google-auth-library');

// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-nodejs-quickstart.json
let SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
let TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
    process.env.USERPROFILE) + '/.credentials/';
let TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';


module.exports = {
  getEventCategories,  
  getEvents,
  setResponseStatus
};

function getEventCategories(){
  return eventCategories.find({});
}

function getEvents(calendarId) {
  return loadClientSecrets()
    .then(res => authorize(JSON.parse(res))
                    .then(response => listEvents(response, calendarId)))

}

function loadClientSecrets(){
  return new Promise(function (fulfill, reject){
    fs.readFile('client_secret.json', function processClientSecrets(err, content){
      if (err){
        console.log('Error loading client secret file: '+ err);
        reject(err);
      }
      else fulfill(content);
    }) 
  })
}



/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 */
function authorize(credentials) {
  return new Promise(function (fulfill, reject){
    let clientSecret = credentials.installed.client_secret;
    let clientId = credentials.installed.client_id;
    let redirectUrl = credentials.installed.redirect_uris[0];
    let auth = new googleAuth();
    let oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, function(err, token) {
      if (err) {
        fulfill(getNewToken(oauth2Client));
      } else {
        oauth2Client.credentials = JSON.parse(token);
        fulfill(oauth2Client);
      }
    });
  })
}

/**
 * Get and store new token after prompting for user authorization, and then
 * execute the given callback with the authorized OAuth2 client.
 */
function getNewToken(oauth2Client) {
  new Promise (function (fulfill, reject){
    let authUrl = oauth2Client.generateAuthUrl({
      access_type: 'offline',
      scope: SCOPES
    });
    console.log('Authorize this app by visiting this url: ', authUrl);
    let 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.log('Error while trying to retrieve access token', err);
          reject(err);
        }
        oauth2Client.credentials = token;
        storeToken(token);
        fulfill(oauth2Client);
      });
    });
  })
}

/**
 * Store token to disk be used in later program executions.
 */
function storeToken(token) {
  try {
    fs.mkdirSync(TOKEN_DIR);
  } catch (err) {
    if (err.code != 'EEXIST') {
      throw err;
    }
  }
  fs.writeFile(TOKEN_PATH, JSON.stringify(token));
  console.log('Token stored to ' + TOKEN_PATH);
}

/**
 * Lists the next 10 events on the user's primary calendar.
 */
function listEvents(auth, calendarId) {
  return new Promise(function ( fulfill, reject){
    var calendar = google.calendar('v3');
    calendar.events.list({
      auth: auth,
      calendarId: calendarId,
      timeMin: (new Date()).toISOString(),
      maxResults: 10,
      singleEvents: true,
      orderBy: 'startTime'
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        reject(err);
      }
      var events = response.items;
      if (events.length == 0) {
        console.log('No upcoming events found.');
      } else {
        console.log('Upcoming 10 events:');
        for (var i = 0; i < events.length; i++) {
          var event = events[i];
          var start = event.start.dateTime || event.start.date;
          console.log('%s - %s', start, event.summary);
        }
        // return events
        fulfill(events)
      }
    });
  })

  function setResponseStatus(){

  }
}