๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ API ์—ฐ๋™ ์‹œ, ์—๋Ÿฌ ๋ฐœ์ƒ ๋ฐ ํ•ด๊ฒฐ(Web Service)

    ๋ฐ˜์‘ํ˜•

    ๋‹ค์Œ ๋‚ด์šฉ์€ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ API ๋ฅผ ์—ฐ๋™ํ•˜๋ฉด์„œ ๋ฐœ์ƒํ–ˆ๋˜ ๋ฌธ์ œ์  ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ๋ณต๊ธฐํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

    ๊ธฐ์กด ์—ฐ๋™ ๊ธ€ : lucete-stellae.tistory.com/5?category=905216

     

    1. ๋กœ๊ทธ์ธ๊ณผ ํšŒ์›๊ฐ€์ž… ๋ถ„๋ฆฌ ์ƒํ™ฉ์—์„œ์˜ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ

     ์œ„ ๊ธ€๊ณผ ๊ตฌ๊ธ€ API ๋ฌธ์„œ์—์„œ ์ œ๊ณตํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, ํ•œ ํŽ˜์ด์ง€์— ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ๋ฐ ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ์ด ๋™์‹œ์— ์กด์žฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์˜ ์˜ˆ์ œ๋ฅผ ์‹ค์Šตํ•˜๊ธฐ์—๋Š” ์ข‹์ง€๋งŒ, ์ด๋Š” ํ‰๊ท ์ ์ธ ํ™ˆํŽ˜์ด์ง€ ๋กœ๊ทธ์ธ ๋ฐ ํšŒ์›๊ฐ€์ž…์—์„œ SNS ์—ฐ๋™ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ถ€์ ํ•ฉํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋”ฐ๋ผ์„œ, ์ œ๊ฐ€ ์—ฐ๋™ ์˜ˆ์ œ ์‹ค์Šต ํ›„ ์‹ค์ œ ์—ฐ๋™ํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ตฌ๊ธ€๋กœ ๋กœ๊ทธ์ธํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ํšŒ์›๊ฐ€์ž…ํ•˜๋Š” ๋ถ€๋ถ„ 2๊ฐœ์˜ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ์—ฐ๋™ ๋ฒ„ํŠผ์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    - ๋กœ๊ทธ์ธ : ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ํ˜ธ์ถœ -> ๋กœ๊ทธ์ธ ์ •๋ณด ๋ฐ ์„œ๋น„์Šค ๋‚ด ํšŒ์›์ •๋ณด ๋น„๊ต -> ์ •๋ณด ์กด์žฌ ์‹œ, ๋กœ๊ทธ์ธ ์•„๋‹Œ ๊ฒฝ์šฐ ๋น„๋กœ๊ทธ์ธ.

     

    - ํšŒ์›๊ฐ€์ž… : ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ํ˜ธ์ถœ -> ๋กœ๊ทธ์ธ ์ •๋ณด์—์„œ ๊ตฌ๊ธ€ ์œ ์ € ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€ ์ž์‹์ฐฝ์— ์„ธํŒ… -> ์„œ๋น„์Šค ํšŒ์›๊ฐ€์ž… ์ง„ํ–‰.

     

     ์œ„ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ์— ์žˆ์–ด์„œ ํ•œ๋ฒˆ ๋กœ๊ทธ์ธ์„ ํ˜ธ์ถœ ํ•˜๋ฉด, ๋กœ๊ทธ์ธ์ด ๋œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ๋˜์–ด 

     

    function signOut () {
    	var auth2 = gapi.auth2.getAuthInstance();
    	auth2.signOut().then( function () {
    		console.log( 'User signed out.' );
    	} );
    };

     

    ์œ„ ๋กœ๊ทธ ์•„์›ƒ์„ ํ˜ธ์ถœํ•˜๊ธฐ ์ „ ๊นŒ์ง€๋Š” ์ž๋™์œผ๋กœ ๋กœ๊ทธ์ธ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    ์ด ๋ฌธ์ œ๋ฅผ ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ, 

     

    function onSignIn(googleUser) {
      var profile = googleUser.getBasicProfile();
      console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
      console.log('Name: ' + profile.getName());
      console.log('Image URL: ' + profile.getImageUrl());
      console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
    }    
    <div class="g-signin2" data-onsuccess="onSignIn"></div>

     

    ์ด๋Ÿฐ ์‹์œผ๋กœ, div ํƒœ๊ทธ ์•ˆ์— ๋ฒ„ํŠผ์ด ์ƒ์„ฑ๋˜๊ฒŒ ๋˜๋Š”๋ฐ, data-onsuccess ์— onSignIn ์ด ๋งคํ•‘๋˜์–ด ์žˆ์–ด, ๋ฒ„ํŠผ์ด ๋ Œ๋”๋ง ๋˜๊ณ  ๋‚˜์„œ, ๋ฒ„ํŠผ์„ ํ•œ๋ฒˆ์ด๋ผ๋„ ํด๋ฆญํ•˜๊ณ  ๋กœ๊ทธ์•„์›ƒ์„ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, ๋‹ค์‹œ ์ ‘์†ํ•ด๋„ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋˜๊ฒŒ๋” ๋˜์–ด์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด, ๋กœ๊ทธ์ธ ์ƒํ™ฉ์—์„œ๋Š” ๋กœ๊ทธ์ธ ํผ์— ํƒœ์šฐ๊ณ , ๋กœ๊ทธ์•„์›ƒ์„ ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.

     

    function onSuccess ( googleUser ) {
    	console.log( googleUser );
    	var profile = googleUser.getBasicProfile();
    	signOut();
    	var $form = $( "#snsLoginForm" );
    	$form.find( "input[name='id']" ).val( profile.getEmail() );
    	$form.find( "input[name='snsType']" ).val( "google" );
    	$form.attr( "action" ,"/comm/member/googleLogin.do" );
    	$form.submit();
    }

     

     ํšŒ์› ๊ฐ€์ž… ์ƒํ™ฉ์—์„œ๋„ ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์‹คํ–‰ ํ›„, ๋กœ๊ทธ์•„์›ƒ์„ ์‹œํ‚ต๋‹ˆ๋‹ค.

    function onSuccess ( googleUser ) {
    	var profile = googleUser.getBasicProfile();
    	// ๊ตฌ๊ธ€ ๊ฐ€์ž… ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, ๊ฐ€์ž…์ •๋ณด ์ฒดํฌ
    	$.ajax( {
    		"url" : "/comm/member/snsSignChk.do" ,
    		"dataType" : "json" ,
    		"data" : {
    			"id" : profile.getEmail() ,
    			"password" : profile.getEmail() ,
    			"snsType" : "google"
    		} ,
    		"success" : function ( data ) {
    			console.log( data );
    			if ( data.result > 0 ) {
    				$.alert2( "์ด๋ฏธ ๊ฐ€์ž…๋œ ๊ณ„์ •์ž…๋‹ˆ๋‹ค." ,{
    					"close" : function () {
    						signOut();
    					}
    				} );
    				signOut();
    				return false;
    			} else {
    				$( "#removeSection1" ).remove();
    				$( "#removeSection2" ).remove();
    				$( "#removeSection3" ).remove();
    				var $form = document.signupForm;
    				$form.snsLoginYn.value = "Y";
    				$form.email.value = profile.getEmail();
    				$form.snsType.value = "google";
    				$( "#snsLoginId" ).val( profile.getEmail() );
    				$( "#snsLoginPw" ).val( profile.getEmail() );
    				signOut();
    			}
    		}
    	} );
    }

     

    ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ค‘๋ณต ํ˜ธ์ถœ ๋ฌธ์ œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋กœ๊ทธ์ธ ํ›„, ๋กœ๊ทธ์•„์›ƒ๋„ ์„œ๋น„์Šค์—์„œ๋งŒ ๋กœ๊ทธ์•„์›ƒ ์‹œํ‚ค๋ฉด ๋๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

     

    2. gapi Cannot read property 'style' of null

    - ์ œ์ผ ์‚ฝ์งˆ ๋งŽ์ดํ•œ ๋ฌธ์ œ๋กœ์จ ๋ฒ„ํŠผ์„ 2๊ฐœ์ด์ƒ ๋งŒ๋“œ๋‹ˆ ๋™์ž‘์„ ์•„์˜ˆ ์•ˆํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊น”๊ณ ๊ฐ€์•ผํ•  ๋‚ด์šฉ์€ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ์—ฐ๋™ ๋ฒ„ํŠผ์— ์ ์šฉ๋˜๋Š” ๋ฒ„ํŠผ ๋ Œ๋”๋ง๋ช…์ด ๊ฒน์น˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

     

    ๊ด€๋ จํ•œ ๋‚ด์šฉ์œผ๋กœ๋Š” ๋‹ค์Œ์„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค. 

    github.com/google/google-api-javascript-client/issues/281

     

    Cannot read property 'style' of null · Issue #281 · google/google-api-javascript-client

    First, I already try to find solution on SO, but I think this is a bug. I am using Angular2 application. (For backend I use ASP.NET core 1.1.) I also create SignIn button following official tutoria...

    github.com

    ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” "<div class="g-signin2" data-onsuccess="onSignIn"></div>" ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๋ฐ ๋™์ผํ•œ ํด๋ž˜์Šค๋ช…์„ ๊ฐ–๋Š” ๋ฒ„ํŠผ์„ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ๋จนํ†ต์ด ๋ฉ๋‹ˆ๋‹ค.

     

    ๋”ฐ๋ผ์„œ, ํ•ด๋‹น ๋ฒ„ํŠผ๋“ค์„ ์ปค์Šคํ…€ ๋ Œ๋”๋ง์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

     

    ์ปค์Šคํ…€ ๋ Œ๋”๋ง์€ ๊ตฌ๊ธ€ ๋ฌธ์„œ์˜ ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

    developers.google.com/identity/sign-in/web/build-button

     

    Building a custom Google Sign-In button  |  Google Sign-In for Websites

    Customizing the automatically rendered sign-in button (recommended) To create a Google Sign-In button with custom settings, add an element to contain the sign-in button to your sign-in page, write a function that calls signin2.render() with your style and

    developers.google.com

    ์•„๋ž˜๋Š” ์ œ๊ฐ€ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

     

    - ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€

    <script src="https://apis.google.com/js/platform.js?onload=renderButtonLogin" async defer></script>
    <meta name="google-signin-client_id" content="380583811608-sqj8rb44q1sd2vfac8sq75bp7af966pt.apps.googleusercontent.com">
        
    <script type="text/javascript">
    	function onSuccess ( googleUser ) {
    		console.log( googleUser );
    		var profile = googleUser.getBasicProfile();
    		signOut();
    		var $form = $( "#snsLoginForm" );
    		$form.find( "input[name='id']" ).val( profile.getEmail() );
    		$form.find( "input[name='snsType']" ).val( "google" );
    		$form.attr( "action" ,"/comm/member/googleLogin.do" );
    		$form.submit();
    	}
    	function onFailure ( error ) {
    		console.log( error );
    	}
    	function renderButtonLogin () {
    		gapi.signin2.render( 'my-signin1' ,{
    			'scope' : 'profile email' ,
    			'width' : 100 ,
    			'height' : 50 ,
    			'longtitle' : true ,
    			'theme' : 'dark' ,
    			'onsuccess' : onSuccess ,
    			'onfailure' : onFailure
    		} );
    	}
    	function signOut () {
    		var auth2 = gapi.auth2.getAuthInstance();
    		auth2.signOut().then( function () {
    			console.log( 'User signed out.' );
    		} );
    	};
    </script>
    
    
    <!-- ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ -->
    <div id="my-signin1"></div>
    <!-- ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ -->

     

    - ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€

    <script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>
    <meta name="google-signin-client_id" content="380583811608-sqj8rb44q1sd2vfac8sq75bp7af966pt.apps.googleusercontent.com">
    
    <script type="text/javascript">
    function onSuccess ( googleUser ) {
    			var profile = googleUser.getBasicProfile();
    			// ๊ตฌ๊ธ€ ๊ฐ€์ž… ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, ๊ฐ€์ž…์ •๋ณด ์ฒดํฌ
    			$.ajax( {
    				"url" : "/comm/member/snsSignChk.do" ,
    				"dataType" : "json" ,
    				"data" : {
    					"id" : profile.getEmail() ,
    					"password" : profile.getEmail() ,
    					"snsType" : "google"
    				} ,
    				"success" : function ( data ) {
    					console.log( data );
    					if ( data.result > 0 ) {
    						$.alert2( "์ด๋ฏธ ๊ฐ€์ž…๋œ ๊ณ„์ •์ž…๋‹ˆ๋‹ค." ,{
    							"close" : function () {
    								signOut();
    							}
    						} );
    						signOut();
    						return false;
    					} else {
    						$( "#removeSection1" ).remove();
    						$( "#removeSection2" ).remove();
    						$( "#removeSection3" ).remove();
    						var $form = document.signupForm;
    						$form.snsLoginYn.value = "Y";
    						$form.email.value = profile.getEmail();
    						$form.snsType.value = "google";
    						$( "#snsLoginId" ).val( profile.getEmail() );
    						$( "#snsLoginPw" ).val( profile.getEmail() );
    						signOut();
    					}
    				}
    			} );
    		}
    		function onFailure ( error ) {
    			console.log( error );
    		}
    		function renderButton () {
    			gapi.signin2.render( 'my-signin2' ,{
    				'scope' : 'profile email' ,
    				'width' : 100 ,
    				'height' : 50 ,
    				'longtitle' : true ,
    				'theme' : 'dark' ,
    				'onsuccess' : onSuccess ,
    				'onfailure' : onFailure
    			} );
    		}
    </script>
    
    <!-- ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ -->
    <div id="my-signin2"></div>
    <!-- ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ -->
    
    

     

     

     

    728x90
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€