npm install googleapis --save
using System; using System.Collections; using GooglePlayGames; using GooglePlayGames.BasicApi; using UnityEngine; public class GoogleAuth:MonoBehaviour { /// <summary> /// /// </summary> /// <param name="callback"></param> public void Auth(Action<string> callback) { InitAuth(() => { GetServerToken(callback); }); } private void InitAuth(Action callback) { var config = new PlayGamesClientConfiguration.Builder() .AddOauthScope("profile") .AddOauthScope("email") .Build(); PlayGamesPlatform.InitializeInstance(config); PlayGamesPlatform.Activate(); Social.localUser.Authenticate((success, str) => { if (success) callback(); else Debug.Log("Error on Social Authenticate: " + str); }); } private void GetServerToken(Action<string> callback) { StartCoroutine(ReadToken((serverToken,empty) => { callback(serverToken); })); } private IEnumerator ReadToken(Action<string, string> callback) { yield return null; if (!PlayGamesPlatform.Instance.IsAuthenticated()) // { PlayGamesPlatform.Instance.Authenticate((result, msg) => // { if (!result) { PlayGamesPlatform.Instance.Authenticate( (result2, msg2) => // googleplay { if (!result2) { PlayGamesPlatform.Instance.GetIdToken( val => // IdToken { StartCoroutine(ReadToken(callback)); }); } }, false); } }, true); } else { PlayGamesPlatform.Instance.GetServerAuthCode( (status, code) => // { if (status != CommonStatusCodes.Success || string.IsNullOrEmpty(code)) StartCoroutine(ReadToken(callback)); else callback(code, null); }); } } }
<uses-permission android:name="android.permission.GET_ACCOUNTS"/> <uses-permission android:name="android.permission.USE_CREDENTIALS"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
"use strict"; const fs = require('fs'); const https = require("https"); const google = require('googleapis'); /** * GoogleCloud */ const _clientSecret = "client_secret.apps.googleusercontent.com.json"; const _userInfoUrl = "https://www.googleapis.com/games/v1/players/me"; let oauth2Client; /** * */ class GooglePlayAuth { /** * * @param callback */ static Init(callback) { fs.readFile(_clientSecret, function (err, content) { if (err) { callback(err); return; } const credentials = JSON.parse(content); oauth2Client = new google.auth.OAuth2(credentials.web.client_id, credentials.web.client_secret, credentials.web.redirect_uris[0]); callback(null); }); } /** * * @param serverToken * @param callback */ static Verify(serverToken,callback) { oauth2Client.getToken(serverToken, function (err, token) { if (err) { callback(`Error while retrieve access token: ${err}`); return; } oauth2Client.credentials = token; // require("https").get(`${_userInfoUrl}?access_token=${token.access_token}`, function (res) { res.on("data", function (d) { let userData = {}; try { userData = JSON.parse(d); } catch (er) { callback(`Error parse user data ${er}`); return; } if (userData.playerId == null) { callback(`Error read playerId: ${userData}`); return; } // - callback(null, userData); }); }).on("error", function (err) { callback(`Fail request: ${err}`); }); }); } } module.exports = GooglePlayAuth;
Source: https://habr.com/ru/post/325900/
All Articles