gpt4 book ai didi

javascript - googleapis TypeError : authClient. 请求不是 nodejs 中的函数

转载 作者:行者123 更新时间:2023-11-30 11:40:33 25 4
gpt4 key购买 nike

我正在使用 googleapisgoogle auth对于谷歌日历。我已经获取了 calendar event 列表,但是当我尝试插入事件时它会抛出一个错误,即 TypeError: authClient.request is not a function in nodejs.

这是我的代码片段:-

//路由.js

...
app.get('/auth/google', passport.authenticate('google', {
scope: ['profile', 'email', 'https://www.googleapis.com/auth/calendar']
}));
...

//服务器/passport.js

... 
var google = require('googleapis');
var googleAuth = require('google-auth-library');
...
passport.use(new GoogleStrategy({
clientID: configAuth.googleAuth.clientID,
clientSecret: configAuth.googleAuth.clientSecret,
callbackURL: configAuth.googleAuth.callbackURL,
passReqToCallback: true
},
function(req, token, refreshToken, profile, done) {
...
var clientSecret = configAuth.googleAuth.clientSecret;
var clientId = configAuth.googleAuth.clientID;
var redirectUrl = configAuth.googleAuth.callbackURL;
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
var tokenObj = { access_token: token, refresh_token: refreshToken }
oauth2Client.credentials = tokenObj;

req.session.oauth2Client = oauth2Client;


var calendar = google.calendar('v3');
calendar.events.list({
auth: oauth2Client,
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err)
return;
}
var events = response.items;
var googleEvents = [];
if (events.length == 0) {
console.log('No upcoming events found.');
} else {
for (var i = 0; i < events.length; i++) {
var temp = {};
temp.info = events[i].summary;
temp.start = events[i].start.dateTime || events[i].start.date;
googleEvents.push(temp);
}
}
console.log(googleEvents); // it is work fine
});
...
...
}))

//服务器/ Controller /todo.js

....
....
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': '2017-03-20T09:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2017-03-23T17:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'recurrence': [
'RRULE:FREQ=DAILY;COUNT=2'
],
'reminders': {
'useDefault': false,
'overrides': [
{ 'method': 'email', 'minutes': 24 * 60 },
{ 'method': 'popup', 'minutes': 10 },
],
},
};


var calendar = google.calendar('v3');
calendar.events.insert({ //here ERROR 'TypeError: authClient.request is not a function' is generated
auth: req.session.oauth2Client,
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);
});

...
...

最佳答案

这不起作用,因为 req.session 存储为 json:

To store or access session data, simply use the request property req.session, which is (generally) serialized as JSON by the store, so nested objects are typically fine. https://github.com/expressjs/session

当存储为json时,所有的方法都从对象中剥离出来,只保存数据。

我通过创建一个包含所有 oauth 对象的单独的全局对象解决了这个问题,可通过 session ID 检索:

//Global object
var auths = {};

//...

//Save Client
auths[req.session.id] = oauth2Client;

//...

//use client
var calendar = google.calendar('v3');
calendar.events.insert({
auth: auths[req.session.id],
calendarId: 'primary',

关于javascript - googleapis TypeError : authClient. 请求不是 nodejs 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com