我正在尝试使用 google oauth api 登录到我的网站。我正在使用 HTTP 请求,但我得到一个 302 http 代码(重定向/临时移动),我不知道该做什么以及如何去做。我想我应该使用新位置发出另一个 http 请求,但我不知道如何获取它。
这是我正在使用的核心:
var redirectUri = encodeURIComponent("http://localhost:3000/user/login/oauth2");
var clientId = "322263763991.apps.googleusercontent.com";
var clientSecret = "********************";
var dataPath = "/o/oauth2/token"
+ "?code=" + cod
+ "&client_id=" + clientId
+ "&client_secret=" + clientSecret
+ "&redirect_uri=" + redirectUri
+ "&grant_type=authorization_code";
var options = {
host: 'accounts.google.com',
path: dataPath,
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('X_BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write('data\n');
req.write('data\n');
req.end();