我试图通过OAuth2.0在Google中以电子方式登录Google
我读了这篇评论,所以我也做了server.js
Google登录API仅在运行中的Web服务器上起作用。
这些我开始一个项目时遇到的错误
未捕获的错误:收听EADDRINUSE ::: 8070未捕获的uO {消息: “无效的cookiePolicy”,堆栈:“ gapi.auth2.ExternallyVisibleError: 无效的cookieP…pis.google.com / js / platform.js?onload = init:18:356)“}
未捕获的错误:监听EADDRINUSE ::: 8070
server.js
console.log("server start")
var http = require('http');
//create a server object:
var server =http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end('test'); //end the response
}).listen(8070); //the server object listens on port 8080
server.on('listening',function(){
console.log('ok, server is running');
});
index.html
<script>
var gauth;
function checkLoginStatus() {
var loginBtn = document.querySelector('#loginBtn');
var nameTxt = document.querySelector('#name');
if (gauth.isSignedIn.get()) {
console.log("logined");
loginBtn.value = 'Logout'
var profile=gauth.currentUser.get().getBasicProfile();
nameTxt.innerHTML='Welcome <strong>'+profile.getName()+'</strong'
}
else {
console.log("loged out");
loginBtn.value = 'Login'
nameTxt.innerHTML='';
}
}
function init() {
console.log("init")
gapi.load('auth2', function () {
console.log("auth2");
gauth = gapi.auth2.init(
{client_id: '1067161273424-gud6khml7r3bssi35u2rj5lchepj25bo.apps.googleusercontent.com'}
).then(function () {
console.log("gapi done");
})
gauth.then(function () {
console.log("google auth success");
checkLoginStatus();
}, function () {
console.log("google auth faill");
})
});
}
</script>
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
<span id="name"></span>
<input type="button" id="loginBtn" value="checking.." onclick="
if(this.value==='Login'){
gauth.signIn().then(function() {
console.log('gauth.signIn()');
checkLoginStatus();
});
}
else {
gauth.signOut().then(function() {
console.log('gauth.signOut()');
checkLoginStatus();
});
}
">
<script src="https://apis.google.com/js/platform.js?onload=init" async defer></script>