0

我想用google日曆api創建事件,但是我遇到了授權問題。我創建了一個谷歌登錄,以不同的方式,所以我不知道去了解連接到谷歌日曆的最佳方式,這是我的hwapi文件:authClient.request不是函數

var Homework = require('../models/homework'); 
var mongoose = require('mongoose'); 
var google = require('googleapis'); 
var jwt = require('jsonwebtoken'); 
var secret = 'check123'; 
var googleAuth = require('google-auth-library'); 
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; 


var googleAuth = require('google-auth-library'); 

// function authorize(credentials, callback) { 
// var clientSecret = credentials.installed.client_secret; 
// var clientId = credentials.installed.client_id; 
// var redirectUrl = credentials.installed.redirect_uris[0]; 

// var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 

// // Check if we have previously stored a token. 
// fs.readFile(TOKEN_PATH, function(err, token) { 
//  if (err) { 
//  getNewToken(oauth2Client, callback); 
//  } else { 
//  oauth2Client.credentials = JSON.parse(token); 
//  callback(oauth2Client); 
//  } 
// }); 
// } 
//mongoose.connect('mongodb://localhost:27017/test'); 
var auth = new googleAuth(); 


    var clientSecret = '4etHKG0Hhj84bKCBPr2YmaC-'; 
    var clientId = '655984940226-dqfpncns14b1uih73i7fpmot9hd16m2l.apps.googleusercontent.com'; 
    var redirectUrl = 'http://localhost:8000/auth/google/callback'; 
    var auth = new googleAuth(); 
    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 
    //console.log(auth);  



module.exports = function(hwRouter,passport){ 

    hwRouter.post('/homeworks', function(req, res){ 
     var homework = new Homework(); 
     homework.summary = req.body.summary; 
     homework.description = req.body.description; 
     homework.startDate = req.body.startDate; 
     homework.endDate = req.body.endDate; 
     if(req.body.summary == null || req.body.summary == '' || req.body.description == null || req.body.description == '' || req.body.startDate == null || req.body.startDate == '' || req.body.endDate == null || req.body.endDate == ''){ 
      res.send("Ensure all fields were provided!"); 
     } 
     else{ 
      homework.save(function(err){ 
       if(err){ 
        res.send('Homework already exists!'); 
       } 
       else{ 
        res.send('Homework created successfully!'); 
       } 
      }); 
     } 
    }) 


    var calendar = google.calendar('v3'); 
    hwRouter.get('/retrieveHW/:summary', function(req,res){ 
     Homework.find({},function(err,hwData){ 
      console.log(hwData); 
      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}, 
       ], 
       }, 
      }; 

console.log(auth) 
     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); 
     }); 

      res.json({success: true, message: "successfull retrieved the homework!"}); 
     }); 

    }) 

    return hwRouter; 
} 

正如你可以看到伊夫使用一些代碼的嘗試goog api提供的只是爲了確保我可以連接到它。我的代碼卡住的部分是我相信當我將它傳遞給calendar.event.create部分中的auth:auth時。它給了我錯誤:authClient.request不是一個函數。任何建議將有助於感謝!

回答

0

嘗試JavaScript樣品以下:

/** 
     * Initializes the API client library and sets up sign-in state 
     * listeners. 
     */ 
     function initClient() { 
     gapi.client.init({ 
      discoveryDocs: DISCOVERY_DOCS, 
      clientId: CLIENT_ID, 
      scope: SCOPES 
     }).then(function() { 
      // Listen for sign-in state changes. 
      gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); 

      // Handle the initial sign-in state. 
      updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); 
      authorizeButton.onclick = handleAuthClick; 
      signoutButton.onclick = handleSignoutClick; 
     }); 
     } 

     /** 
     * Called when the signed in status changes, to update the UI 
     * appropriately. After a sign-in, the API is called. 
     */ 
     function updateSigninStatus(isSignedIn) { 
     if (isSignedIn) { 
      authorizeButton.style.display = 'none'; 
      signoutButton.style.display = 'block'; 
      listUpcomingEvents(); 
     } else { 
      authorizeButton.style.display = 'block'; 
      signoutButton.style.display = 'none'; 
     } 
     } 

在該代碼中,運行initClient()之後,gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);監聽任何狀態變化。如果initClient()成功登錄或不成功,updateSigninStatus函數會處理。如果是的話,它會調用listUpcomingEvents()函數,在你的情況下你會調用create事件函數。

這是一個related SO post,可以幫助您與JS客戶端庫代碼實現。

希望這會有所幫助。