유니티 Webview 카카로 로그인
Unity3D 2019. 1. 14. 13:04반응형
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class Main : MonoBehaviour { public Button btn; public Button btnUnlink; public Button btnLogout; public GameObject contentGo; public Text txtUserName; private UniWebView webView; private string url; private string serverPath = "http://smilejsu82.cafe24app.com"; private bool isShowWebview; // Start is called before the first frame update void Start() { var uds = Uri.UnescapeDataString("http://smilejsu82.cafe24app.com/profile?id=1006112778&profile_image=http%3A%2F%2Fk.kakaocdn.net%2Fdn%2Fbqkwkh%2FbtqrOW5IvCF%2F8aj4HwnAv0z3AZk575OwjK%2Fprofile_640x640s.jpg&nickname=%EC%9E%A5%EC%83%81%EC%9A%B1&thumbnail_image=http%3A%2F%2Fk.kakaocdn.net%2Fdn%2Fbqkwkh%2FbtqrOW5IvCF%2F8aj4HwnAv0z3AZk575OwjK%2Fprofile_110x110c.jpg"); Uri unparsedUrl = new Uri(uds); // Grabs the query string from the URL: string query = unparsedUrl.Query; // Parses the query string as a NameValueCollection: var queryParams = System.Web.HttpUtility.ParseQueryString(query); var result = queryParams["id"] + ", " + queryParams["profile_image"] + ", " + queryParams["nickname"] + ", " + queryParams["thumbnail_image"]; Debug.Log(result); this.btn.gameObject.SetActive(true); this.contentGo.SetActive(false); this.btnUnlink.onClick.AddListener(() => { StartCoroutine(this.Get("http://smilejsu82.cafe24app.com/unlink", (resultData) => { UniWebView.ClearCookies(); Application.Quit(); })); }); this.btnLogout.onClick.AddListener(() => { StartCoroutine(this.Get("http://smilejsu82.cafe24app.com/logout", (resultData) => { UniWebView.ClearCookies(); Application.Quit(); })); }); btn.onClick.AddListener(() => { if (!isShowWebview) { this.isShowWebview = true; var uri = "/login"; var url = string.Format("{0}{1}", this.serverPath, uri); Debug.Log(url); var uniWebViewGo = new GameObject("UniWebView"); webView = uniWebViewGo.AddComponent<UniWebView>(); webView.OnPageFinished += OnPageFinishedEventHandler; webView.BackgroundColor = Color.black; //webView.OnMessageReceived += WebView_OnMessageReceived; //webView.OnMessageReceived += (view, message) => { // print(message.RawMessage); //}; //webView.Load("uniwebview://action?key=value&anotherKey=value"); //webView.OnMessageReceived += (view, message) => { // Debug.LogFormat("[OnMessageReceived] {0} {1}", view, message); // if (message.Path.Equals("profile")) // { // var id = message.Args["id"]; // var nickname = message.Args["nickname"]; // var thumbnail_image = message.Args["thumbnail_image"]; // Debug.LogFormat("{0} {1} {2}", id, nickname, thumbnail_image); // // Restart the game // } //}; webView.Frame = new Rect(0, 0, Screen.width, Screen.height); webView.Load(url); webView.Show(true); } }); } private void WebView_OnMessageReceived(UniWebView webView, UniWebViewMessage message) { Debug.LogFormat("[WebView_OnMessageReceived] {0} {1}", webView, message); } private void OnPageFinishedEventHandler(UniWebView webView, int statusCode, string url) { //var arrUniWebViews = GameObject.FindObjectsOfType<UniWebView>(); Debug.LogFormat("------------> {0} {1} {2}", webView, statusCode, url); if (statusCode == 200 && url.Contains("http://smilejsu82.cafe24app.com/profile")) { Debug.Log(url); var uds = Uri.UnescapeDataString(url); Uri unparsedUrl = new Uri(uds); // Grabs the query string from the URL: string query = unparsedUrl.Query; // Parses the query string as a NameValueCollection: var queryParams = System.Web.HttpUtility.ParseQueryString(query); var result = queryParams["id"] + ", " + queryParams["profile_image"] + ", " + System.Web.HttpUtility.UrlDecode(queryParams["nickname"]) + ", " + queryParams["thumbnail_image"]; Debug.Log(result); this.thumbUrl = queryParams["thumbnail_image"]; webView.Hide(true); webView.OnPageFinished -= OnPageFinishedEventHandler; //this.isShowWebview = false; //Destroy(webView); Debug.Log("------------ Destroy Webview"); this.btn.gameObject.SetActive(false); this.contentGo.SetActive(true); this.txtUserName.text = System.Web.HttpUtility.UrlDecode(queryParams["nickname"]); StartCoroutine(this.GetTexture()); } } private IEnumerator Get(string url, System.Action<string> onResponse) { //http서버에 요청 Debug.Log(url); UnityWebRequest www = UnityWebRequest.Get(url); yield return www.SendWebRequest(); onResponse(www.downloadHandler.text); } #region Get private IEnumerator Get(string uri, System.Action<string> onResponse, string headerName = null, string headerVal = null) { //http서버에 요청 var url = string.Format("{0}/{1}", serverPath, uri); Debug.Log(url); UnityWebRequest www = UnityWebRequest.Get(url); Debug.Log(headerName); Debug.Log(headerVal); if (!string.IsNullOrEmpty(headerName) && !string.IsNullOrEmpty(headerVal)) { www.SetRequestHeader(headerName, string.Format("Bearer {0}", headerVal)); } yield return www.SendWebRequest(); Debug.Log(www.downloadHandler.text); onResponse(www.downloadHandler.text); } #endregion private string thumbUrl; public Image image; IEnumerator GetTexture() { var url = this.thumbUrl; UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); yield return www.SendWebRequest(); Texture myTexture = DownloadHandlerTexture.GetContent(www); this.image.gameObject.SetActive(false); this.image.material.mainTexture = myTexture; this.image.gameObject.SetActive(true); } // Update is called once per frame void Update() { } } | cs |
서버코드 (web.js)
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | var dbConf = require('./db_conf'); var mysql = require('mysql'); var passport = require("passport"); var express = require("express"); var request = require('request'); var localStorage = require('localStorage'); var url = require('url'); var KakaoStrategy = require('./lib/passport-kakao').Strategy; const kakaoConf = require('./kakao_conf'); const NodeCache = require( "node-cache" ); var myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } ); // var appKey = "YOUR_APP_REST_API_KEY"; // var appSecret = 'aqLW623GI9kEzIq1xxKv9nftGR86pIlq '; // passport 에 Kakao Oauth 추가 let _profile = new NodeCache( { stdTTL: 100, checkperiod: 120 } ); passport.use( new KakaoStrategy({ clientID: kakaoConf.restAPIKey, //clientSecret: appSecret, // callbackURL: "http://localhost:8002/oauth" callbackURL: "http://smilejsu82.cafe24app.com/oauth" }, function(accessToken, refreshToken, params, profile, done){ // authorization 에 성공했을때의 액션 //localStorage.setItem('accessToken', accessToken); var obj = { id: profile.id, accessToken: accessToken }; myCache.set( "myAccessToken", obj, function( err, success ){ if( !err && success ){ console.log( success ); // true // ... do something ... } }); _profile.set('profile', profile,(err, success)=>{ if(!err && success){ console.log(success); } }); console.log( "accessToken :" + accessToken ); console.log( "사용자 profile: " + JSON.stringify(profile._json) ); save(accessToken, refreshToken, profile); return done(null, profile._json); }) ); passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(obj, done) { done(null, obj); }); var pool = mysql.createPool({ connectionLimit : 20, host: '10.0.0.1', user: 'smilejsu82', password: dbConf.password, database: 'smilejsu82' }); // var pool = mysql.createPool({ // connectionLimit : 20, // host: 'localhost', // user: 'root', // password: '123456', // database: 'smilejsu82' // }); // express 앱 설정 var app = express(); app.use(passport.initialize()); app.use(express.json()); app.get("/login", passport.authenticate('kakao',{state: "myStateValue"})); app.get("/oauth", passport.authenticate('kakao'), function(req, res){ // 로그인 시작시 state 값을 받을 수 있음 //res.redirect('/profile?'); var result = _profile.get('profile');//{cmd: 901, profile: _profile.get('profile')}; //console.log(result._raw); var profile_info = JSON.parse(result._raw); console.log(profile_info.properties); var nickname = profile_info.properties.nickname; var thumbnail_image = profile_info.properties.thumbnail_image; res.redirect(url.format({ pathname:"/profile", query: { id: profile_info.id, profile_image : profile_info.properties.profile_image, nickname: profile_info.properties.nickname, thumbnail_image: profile_info.properties.thumbnail_image } })); }) app.get('/profile', (req, res)=>{ res.send(req.query); }); app.get('/unlink', (req, res)=>{ myCache.get('myAccessToken', (err, val)=>{ if(!err){ if(val == undefined){ console.log('key myAccessToken is not found.'); }else{ var options = { url: 'https://kapi.kakao.com/v1/user/unlink', headers: { 'Authorization':`Bearer ${val.accessToken}` } }; request(options, (error, r, body)=>{ // console.log(error); // console.log(response); console.log(body); //console.log(r); res.send(body); }); } } }); }); app.get('/', (req, res)=>{ res.send('서버 8002'); }); app.get('/logout', (req, res)=>{ console.log('---------------->>>' + localStorage.getItem('accessToken')); myCache.get('myAccessToken', (err, val)=>{ if(!err){ if(val == undefined){ console.log('key myAccessToken is not found.'); }else{ var options = { url: 'https://kapi.kakao.com/v1/user/logout', headers: { 'Authorization':`Bearer ${val.accessToken}` } }; request(options, (error, r, body)=>{ // console.log(error); // console.log(response); console.log(body); //console.log(r); res.send(body); }); } } }); }); app.listen(8002, ()=>{ console.log('서버 시작 포트 8002'); }); // 사용자 구현 부분 function save(accessToken, refreshToken, profile){ //save 로직 구현 console.log('save'); pool.getConnection((err, con)=>{ var sql = `insert into auth(user_id, access_token) select ${profile.id}, '${accessToken}' from dual where not exists( select user_id from auth where user_id = ${profile.id} );`; con.query(sql, (err, results, fields)=>{ con.release(); if(err) { console.log(err); throw err; }else{ console.log('저장완료: ' + profile.id, accessToken, profile.id); } }); }); } | cs |
반응형
'Unity3D' 카테고리의 다른 글
100 UNITY TIPS!!! 🔥 (0) | 2019.01.15 |
---|---|
Unity UI에 관한 최고의 최적화 팁 (0) | 2019.01.15 |
nodejs restful api server with unity (0) | 2019.01.04 |
nodejs restful api unity (0) | 2018.12.31 |
Unity에서 .NET 4.x 사용 (0) | 2018.12.27 |