0

Google 캘린더 API를 사용하여 일정을 만들려고하는데 인증에 문제가 있습니다. 당신이 볼 수 있듯이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; 
} 

은 필자가 일부 코드를 사용하여 시도 : 나는 구글 로그인, 나는 구글 캘린더에 연결에 대해 갈 수있는 가장 좋은 방법 확실하지 않다 그래서 다른 방법을 만들어,이 내 hwapi 파일입니다 goog api가 제공 한 것만으로도 내가 그것에 접속할 수 있는지 확인하십시오. 내 코드가 멈추는 부분은 calendar.event.create 부분에서 auth : auth를 전달하면 믿을 수 있습니다. 그것은 나에게 오류를 준다 : authClient.request는 함수가 아니다. 모든 조언 덕분에 도움이 될 것입니다! JavaScript 샘플 다음

답변

0

시도 :이 코드에서

/** 
     * 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);는 어떤 상태 변경을 수신합니다. updateSigninStatus 함수는 initClient()이 성공적으로 로그인했는지 여부를 처리합니다. 예인 경우 listUpcomingEvents() 함수를 호출합니다.이 경우 이벤트 작성 함수를 호출합니다.

다음은 JS 클라이언트 라이브러리 코드 구현에 도움이되는 related SO post입니다.

희망이 도움이됩니다.