구글 로그인 적용시 유의할 점이 있다.
Callback URL 이나 접속 URL에 IP주소가 들어간 유동적 주소는 cb=gapi.loaded_0 이라는 오류와 함께 처리가 되지않아서
테스트시에는 http://localhost:8080 처럼 도메인 주소로 해야하더라.
구글 개발자센터에 나와있는 예제소스만으로는 적용이 안돼서,
구글링하다가 여기 페이지의 소스를 기본틀로 삼아 진행했다.
참고 : https://stackoverflow.com/questions/39203623/how-to-add-google-sign-in-oauth-to-my-website?noredirect=1&lq=1
loginView.jsp - 상단, 미리 선언해야 하는 소스
1
2
|
<script src="https://apis.google.com/js/client:platform.js?onload=renderButton" async defer></script>
<meta name="google-signin-client_id" content="{Client-APP-Id}.apps.googleusercontent.com"></meta>
|
cs |
loginView.jsp - javaScript, 핵심 소스
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
//구글 로그인
$(function(){
function onSuccess(googleUser) {
var profile = googleUser.getBasicProfile();
//console.log(profile);
}
$(".g-signin2").on("click", function(){
gapi.client.load('plus', 'v1', function () {
gapi.client.plus.people.get({
'userId': 'me'
}).execute(function (res) {
console.log(JSON.stringify(res));
res.id += "@g";
$.ajax({
url : "/user/json/checkDuplication/"+res.id,
headers : {
"Accept" : "application/json",
"Content-Type" : "application/json"
},
success : function(idChk){
if(idChk==true){ //DB에 아이디가 없을 경우 => 회원가입
console.log("회원가입중...");
$.ajax({
url : "/user/json/addUser",
method : "POST",
headers : {
"Accept" : "application/json",
"Content-Type" : "application/json"
},
data : JSON.stringify({
userId : res.id,
userName : res.displayName,
password : "google123",
}),
success : function(JSONData){
alert("회원가입이 정상적으로 되었습니다.");
$("form").attr("method","POST").attr("action","/user/snsLogin/"+res.id).attr("target","_parent").submit();
}
})
}
if(idChk==false){ //DB에 아이디가 존재할 경우 => 로그인
console.log("로그인중...");
$("form").attr("method","POST").attr("action","/user/snsLogin/"+res.id).attr("target","_parent").submit();
}
}
})
})
})
})//e.o.google.loginLogic
function onFailure(error) {
console("error : "+error);
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
self.location="/user/logout";
});
}
})//e.o.google
|
cs |
loginView.jsp - HTML 부분
1
2
3
4
|
<!-- 구글 로그인 추가 -->
<div id="googleLogin" align="center">
<div class="g-signin2" data-onsuccess="onSuccess" data-theme="dark"></div>
</div>
|
cs |
반응형
'Backend > JAVA' 카테고리의 다른 글
클라이언트와 서버 구축하기 (기본) (0) | 2018.06.05 |
---|---|
페이스북 로그인 API 적용 (0) | 2018.05.25 |
인스타그램 API 적용하여 게시물 가져오기 (0) | 2018.05.25 |
네이버 로그인 API 적용 (4) | 2018.05.25 |
카카오 로그인 API 적용 (0) | 2018.05.24 |
댓글