I'm trying to set up the authorization for Google API use in order to make requests to the Webmaster Tools API, but I can't seem to understand how to trigger the authentication. I have set up a route, that should trigger the authentication permissions calls when accessed, but I get a continual load signal with a no data received response. I'm using Google's library, https://github.com/google/google-api-nodejs-client/, and have my OAuth2 client ID credentials redirect uri pointing to http://localhost:3000/
because I want to test the setup locally.
我正在尝试设置Google API使用授权,以便向网站站长工具API发出请求,但我似乎无法理解如何触发身份验证。我已经设置了一个路由,它应该在访问时触发身份验证权限调用,但是我得到一个持续的加载信号,没有数据接收响应。我正在使用Google的库,https://github.com/google/google-api-nodejs-client/,并让我的OAuth2客户端ID凭据重定向uri指向http:// localhost:3000 /因为我想要测试本地设置。
Here is my code:
这是我的代码:
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);
Here is my route.js:
这是我的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;
1 个解决方案
#1
2
I think you should redirect to the authorization url provided by the generateAuthUrl. Something like:
我认为你应该重定向到generateAuthUrl提供的授权网址。就像是:
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
});
res.redirect(url);
});
This will take the user to the consent page where s/he can accpet the scopes and then be redirected to your redirect url. I don't have it in front of me but I think that's how I did it.
这将使用户进入同意页面,在那里他/她可以使用范围,然后重定向到您的重定向网址。我没有在我面前,但我认为这就是我做到的。
#1
2
I think you should redirect to the authorization url provided by the generateAuthUrl. Something like:
我认为你应该重定向到generateAuthUrl提供的授权网址。就像是:
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
});
res.redirect(url);
});
This will take the user to the consent page where s/he can accpet the scopes and then be redirected to your redirect url. I don't have it in front of me but I think that's how I did it.
这将使用户进入同意页面,在那里他/她可以使用范围,然后重定向到您的重定向网址。我没有在我面前,但我认为这就是我做到的。