我一直在努力寻找解决以下错误的方法:
TypeError: authClient.request is not a function",
at Object.createAPIRequest
(/app/node_modules/googleapis/build/src/lib/apirequest.js:179:20)",
at Object.watch (/app/node_modules/googleapis/build/src/apis/gmail/v1.js:108:37)",
at Strategy.GoogleStrategy [as _verify] (/app/lib/oauth2.js:96:28)",
at /app/node_modules/passport-oauth2/lib/strategy.js:193:24",
at /app/node_modules/passport-google-oauth20/lib/strategy.js:122:5",
at passBackControl (/app/node_modules/oauth/lib/oauth2.js:134:9)",
at IncomingMessage.<anonymous>
(/app/node_modules/oauth/lib/oauth2.js:157:7)",
at IncomingMessage.emit (events.js:165:20)",
at endReadableNT (_stream_readable.js:1101:12)",
at /app/node_modules/async-listener/glue.js:188:31",
at process._tickCallback (internal/process/next_tick.js:152:19)"
我正在使用nodejs和googleapis库。
当我调用
时发生错误gmail.user.watch();方法
这是我的 app.js
当用户点击/登录时,我有设置会话和oauth2
'use strict';
const path = require('path');
const session = require('express-session');
const express = require('express');
const MemcachedStore = require('connect-memcached')(session);
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const uid = require('uid-safe')
const passport = require('passport');
const config = require('./config');
const logging = require('./lib/logging');
const app = express();
app.disable('etag');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('trust proxy', true);
// Add the request logger before anything else so that it can
// accurately log requests.
app.use(logging.requestLogger);
// Configure the session and session storage.
const sessionConfig = session({
genid: function(req) {
return uid.sync(18) // use UUIDs for session IDs
},
resave: false,
saveUninitialized: true,
secret: config.get('SECRET'),
signed: false,
cookie: {
secure: false
},
store: new MongoStore({ mongooseConnection: db })
});
app.use(sessionConfig);
// Mongo
// OAuth2
app.use(passport.initialize());
app.use(passport.session());
app.use(require('./lib/oauth2').router);
app.use(express.static(path.join(__dirname, './public')));
oauth2.js
'use strict';
const express = require('express');
const config = require('../config');
const client = require('./client/client');
const session = require('express-session')
const mongoose = require('mongoose');
// Mongoose
mongoose.connect('mongodb://admin:outrigger@ds227168.mlab.com:27168/outrigger')
var connection = mongoose.connection;
console.log('Connected!');
// [START setup]
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const google = require('googleapis')
// Configure the Google strategy for use by Passport.js.
//
// OAuth 2-based strategies require a `verify` function which receives the
// credential (`accessToken`) for accessing the Google API on the user's behalf,
// along with the user's profile. The function must invoke `cb` with a user
// object, which will be set at `req.user` in route handlers after
// authentication.
var oauth2 = new GoogleStrategy({
clientID: config.get('OAUTH2_CLIENT_ID'),
clientSecret: config.get('OAUTH2_CLIENT_SECRET'),
callbackURL: config.get('OAUTH2_CALLBACK'),
accessType: 'online'
}, (res, req, profile, cb) => {
// Extract the minimal profile information we need from the profile object
// provided by Google
cb(null, extractProfile(profile));
});
passport.use(oauth2);
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((obj, cb) => {
cb(null, obj);
});
// [END setup]
const router = express.Router();
function onSignIn(){
client.outrigger();
}
// Begins the authorization flow. The user will be redirected to Google where
// they can authorize the application to have access to their basic profile
// information. Upon approval the user is redirected to `/auth/google/callback`.
// If the `return` query parameter is specified when sending a user to this URL
// then they will be redirected to that URL when the flow is finished.
// [START authorize]
router.get(
// Login url
'/auth/login',
// Save the url of the user's current page so the app can redirect back to
// it after authorization
(req, res, next) => {
if (req.query.return) {
req.session.oauth2return = req.query.return;
}
next();
},
// Start OAuth 2 flow using Passport.js
passport.authenticate('google', { scope: [
'email',
'profile',
'https://www.googleapis.com/auth/calendar',
'https://mail.google.com/',
'https://www.googleapis.com/auth/contacts'
]},
function (err, user, info){
if (err){
console.log("Error: " + err);
}
else if(!user) {
console.log('User not authed');
}
}));
// [END authorize]
// [START callback]
router.get(
// OAuth 2 callback url. Use this url to configure your OAuth client in the
// Google Developers console
'/auth/google/callback',
// Finish OAuth 2 flow using Passport.js
passport.authenticate('google'),
// Redirect back to the original page, if any
(req, res) => {
// Save token
const redirect = req.session.oauth2return || '/';
delete req.session.oauth2return;
res.redirect(redirect);
console.log(req.session.id);
onSignIn();
//})
}
);
// [END callback]
当我调用client.outrigger()时;回调部分中的方法。它会调用
gmail.user.watch({
'userId': 'me',
'auth': oauth2, // imported above
resource: {
topicName: "projects/outrigger/topics/gmail",
labelIds: ["INBOX"]
}
});
我一直在互联网上寻找一个无济于事的解决方案。我希望我能弄清楚这一点。
您可以在https://outrigger.herokuapp.com/
找到该网站我见过有人说检查范围。我们使用的范围将根据gmail api的googles文档进行工作。