1. 프로젝트 등록
https://console.cloud.google.com/projectselector2/apis/credentials?supportedpurview=project
Google Cloud Platform
하나의 계정으로 모든 Google 서비스를 Google Cloud Platform을 사용하려면 로그인하세요.
accounts.google.com
2. 구글 정보 요청 여기서 Scope란 구글에 어떤 항목에 정보를 요청할지 목록을 정하는 행동이다.
CLIENT_ID는 OAuth에서 가져온 Client ID, redirect에는 http://localhost를 적는다.
그리고 해당 url을 복사해서 인터넷 창에 넣으면 구글 로그인 요청 창이 뜬다. 권한을 확인하면 해당 url이
code=4/RAFvT5yuAQgJU3cAU3OP6nLOy5ys8Ol3QddPjXC0BJNKEp_eea5vS4QpXrBlPw92ahjya3Luta0g91W4Xp7I0EE
란 정보가 다음 변수까지 붙어있다. 그럼 4...부터 &이 나올때까지 전부 복사해서 System.Web.HttpUtility.UrlDecode로
디코딩하고 사용하도록 하자.
public static bool GoogleLogin() { string cashScope = "https://www.googleapis.com/auth/androidpublisher"; string profileScope = "https://www.googleapis.com/auth/userinfo.profile"; //Scope //https://developers.google.com/android/reference/com/google/android/gms/common/Scopes
string auth = string.Format("scope={0}&response_type=code&client_id={1}&access_type=offline&redirect_uri={2}", cashScope, CLIENT_ID, redircet); string url = string.Format("{0}{1}", "https://accounts.google.com/o/oauth2/v2/auth?", auth); //url 복사. //경로에 code가 돌아온다. //System.Web.HttpUtility.UrlDecode("4/RAFvT5yuAQgJU3cAU3OP6nLOy5ys8Ol3QddPjXC0BJNKEp_eea5vS4QpXrBlPw92ahjya3Luta0g91W4Xp7I0EE")로 decode return true; } |
3. 원하는 scope로 token 요청을 보내면 해당 정보를 받을 수 있다.(해당 프로젝트에 nuget으로 Newtonsoft.Json.dll을 추가하자.)
public static bool GetGoogleUserIdWithIdToken(string code, string id, string secret, out string access_token) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v4/token"); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST";
string auth = string.Format("code={0}&client_id={1}&client_secret={2}&grant_type=authorization_code&redirect_uri={3}", code, id, secret, redircet); UTF8Encoding utfenc = new UTF8Encoding(); byte[] bytes = utfenc.GetBytes(auth);
Stream os = null; try // send the post { webRequest.ContentLength = bytes.Length; // Count bytes to send os = webRequest.GetRequestStream(); os.Write(bytes, 0, bytes.Length); // Send it
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { StreamReader sr = new StreamReader(webResponse.GetResponseStream()); JObject product = JObject.Parse(sr.ReadToEnd().Trim());
access_token = product["access_token"].ToString();
return true; } } catch (Exception exception) { //Logger.Instance.Write(LogTypes.EXCEPTION, 0, "Failed GetGoogleUserIdWithIdToken, exception, message : {0}, exception : {1}" // , exception.Message, exception.StackTrace.ToString()); } finally { os.Close(); } access_token = string.Empty; return false; } |
4. accessToken으로 원하는 정보 요청
public static bool GetGoogleUserIdWithAccessToken(string accessToken, out string googleAccountSerial, out string googleClientId) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v3/tokeninfo"); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST";
string auth = string.Format("access_token={0}", accessToken); UTF8Encoding utfenc = new UTF8Encoding(); byte[] bytes = utfenc.GetBytes(auth);
try // send the post { webRequest.ContentLength = bytes.Length; // Count bytes to send using (Stream stream = webRequest.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); // Send it
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { StreamReader sr = new StreamReader(webResponse.GetResponseStream()); JObject product = JObject.Parse(sr.ReadToEnd().Trim());
googleAccountSerial = product["sub"].ToString(); googleClientId = product["aud"].ToString(); return true; } } } catch (Exception exception) { //Logger.Instance.Write(LogTypes.EXCEPTION, 0, "GoogleLoginUtil.GetGoogleUserIdV2 exception, message : {0}, exception : {1}" // , exception.Message, exception.StackTrace.ToString()); } googleAccountSerial = string.Empty; googleClientId = string.Empty; return false; } |
5. 테스트
private const string CLIENT_ID = "client_id"; private const string CLIENT_SECRET = "client_secret"; private const string redircet2 = "http://localhost";
static void Main(string[] args) { string code = System.Web.HttpUtility.UrlDecode("4/RAFvT5yuAQgJU3cAU3OP6nLOy5ys8Ol3QddPjXC0BJNKEp_eea5vS4QpXrBlPw92ahjya3Luta0g91W4Xp7I0EE"); //GoogleLogin을 사용해 들어온 키. //GoogleLogin(); //Get 형식이라 url로 그냥 호출. 테스트 용도 GetGoogleUserIdWithIdToken(code, CLIENT_ID, CLIENT_SECRET, out string accessToken); bool result = GetGoogleUserIdWithAccessToken(accessToken, out string googleAccountSerial, out string googleClientId); //https://www.daimto.com/google-apis-auth-simple/ //https://developers.google.com/identity/sign-in/web/server-side-flow //https://developers.google.com/api-client-library/python/auth/web-app } |
참고 사이트
https://www.daimto.com/google-apis-auth-simple
Google APIs Auth Simple
We cant always use the Google .net client library in every project. Google Auth simple is a quick and dirty way of enabling Google Authentication in your application.
www.daimto.com
https://developers.google.com/identity/sign-in/web/server-side-flow
Google Sign-In for server-side apps | Google Sign-In for Websites | Google Developers
To use Google services on behalf of a user when the user is offline, you must use a hybrid server-side flow where a user authorizes your app on the client side using the JavaScript API client and you send a special one-time authorization code to your serve
developers.google.com
https://developers.google.com/api-client-library/python/auth/web-app
Using OAuth 2.0 for Web Server Applications | API Client Library for Python | Google Developers
This document explains how web server applications use the Google API Client Library for Python to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, pas
developers.google.com
클라이언트에서 크로스 플랫폼을 사용하는데 id_token을 가져오는 방법이 없다고 해서 다시 찾아서 구현.
보고 최대한 빨리 기억을 되살리는 선에서 남겨야 겠다.
클라이언트 참고 사이트
Enabling Service on Native Platform · Cross-Platform-Native-Plugins-for-Unity
Game Services on Android rely on Google Play Services. You can enable Google Play Services by following below steps. Open Google Play Developer Console and Click on Game Services Tab. Click on Add New Game if you are setting up freshly. Specify If your gam
tutorials.cpnp.voxelbusters.com
'C#' 카테고리의 다른 글
22# CQRS (0) | 2019.08.09 |
---|---|
21# C# Mysql Connection 라이브러리 (0) | 2019.05.23 |
19# C#에서 Twitter 계정 검증 (0) | 2019.05.02 |
18# C#에서 아마존 S3 다운로드 하는법 (0) | 2019.04.03 |
17# C# Protobuf-net에서 List<List<T>> 사용법 (0) | 2019.04.03 |