1
我正在嘗試設置Google API使用授權,以便向網站站長工具API發出請求,但似乎無法理解如何觸發身份驗證。我建立了一條路線,在訪問時應該觸發認證權限調用,但是我得到一個連續的負載信號,並且沒有收到數據響應。我正在使用Google的圖書館https://github.com/google/google-api-nodejs-client/,並且我的OAuth2客戶端ID憑據重定向URI指向http://localhost:3000/
,因爲我想在本地測試設置。Google APIs NodeJS Library OAuth2請求
這裏是我的代碼:
index.js
var express = require('express');
var app = express();
var handlebars = require('hbs');
var google = require('googleapis');
var webmastertools = google.webmasters('v3');
app.set('port', process.env.PORT || 3000);
// Handlebars View Engine
app.set('view engine', handlebars);
app.use(express.static(__dirname + '/public')); // set the public directory
app.listen(app.get('port'), function(){
console.log('Server on port'+app.get('port'));
});
var routes = require('./app/routes');
app.use(routes);
這裏是我的route.js:
var express = require('express');
var router = express.Router();
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2('*client-id*', '*client-secret*', 'http://localhost:3000/');
router.get('/', function(req, res){
res.render('index.hbs');
});
router.get('/auth', function(req, res){
//generater a url that asks permissions for Webmaster Tools
var scopes = [
'https://www.googleapis.com/auth/webmasters'
];
var url = oauth2Client.generateAuthUrl({
access_type: 'online',
scope: scopes
});
});
router.get('404', function(req, res){
res.type('text/plain');
res.status(404);
res.send('404 - Not Found');
});
router.get('500', function(err,req, res, next){
console.error(err.stack);
res.type('text/plain');
res.status(500);
res.send('500 - Server Error');
});
module.exports = router;