Unity3D

Unity Firebase Auth with Google Play ( Unity에서 Google Play 게임 서비스를 사용하여 인증하기)

일등하이 2019. 2. 12. 11:29
반응형

Unity에서 Google Play 게임 서비스를 사용하여 인증하기



https://comeng.tistory.com/entry/Firebase%ED%8C%8C%EC%9D%B4%EC%96%B4%EB%B2%A0%EC%9D%B4%EC%8A%A4-%EC%97%B0%EB%8F%99-1-%EC%8B%9C%EC%9E%91%ED%95%98%EA%B8%B0


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
using System;
using Firebase.Auth;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
    public Button btn;
 
    // Start is called before the first frame update
    void Start()
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .RequestIdToken()
            .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
 
        btn.onClick.AddListener(() =>
        {
            Social.localUser.Authenticate((bool success) => {
                Debug.Log($"-------------------------> Authenticate: {success}");
 
                StartCoroutine(this.LoginImpl());
            });
        });    
    }
 
    //https://mongyang.tistory.com/143?category=967904
    private IEnumerator LoginImpl()
    {
        string idToken = string.Empty;
        while (true)
        {
            var playGamesLocalUser = (PlayGamesLocalUser)Social.localUser;
            idToken = playGamesLocalUser.GetIdToken();
            if (!String.IsNullOrEmpty(idToken))
                break;
            yield return null;
        }
 
        Debug.Log($"--------------------> token: {idToken}");
        var auth = FirebaseAuth.DefaultInstance;
        Debug.Log($"auth: {auth}");
        Credential credential = GoogleAuthProvider.GetCredential(idToken, null);
 
        auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
        {
            Debug.Log($"task.Status: {task.Status}");
 
            if (task.IsCanceled)
            {
                Debug.LogError("SignInWithCredentialAsync was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
                return;
            }
 
            Firebase.Auth.FirebaseUser newUser = task.Result;
            Debug.LogFormat("User signed in successfully: {0} ({1})",
                newUser.DisplayName, newUser.UserId);
        });
    }
}
 
cs

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898):  [Play Games Plugin DLL] 02/12/19 11:36:59 +09:00 DEBUG: Invoking Callbacks: System.Action`2[System.Boolean,System.String]

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898): -------------------------> Authenticate: True

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898): --------------------> token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjdjMzA5ZTNhMWMxOTk5Y2IwNDA0YWI3MTI1ZWU0MGI3Y2RiY2FmN2QiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIzMTk4MzUzNTEyOTEtcDkycjdwNGdydHJpYm8ydWVuMWRzNGthNms2bGMzN3EuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiIzMTk4MzUzNTEyOTEtcjc3MDFhMnZwNHZudmNvNWFsNThlZG9hNjNrZHYzYWwuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTU4NDU5NTk5MjEyMjcwODI3MjgiLCJlbWFpbCI6InNtaWxlanN1ODJAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsIm5hbWUiOiLsnqXsg4HsmrEgKOydvOuTse2VmOydtCkiLCJwaWN0dXJlIjoiaHR0cHM6Ly9saDYuZ29vZ2xldXNlcmNvbnRlbnQuY29tLy1hOFhFN2tXdGpoYy9BQUFBQUFBQUFBSS9BQUFBQUFBQUNwZy9uZW44RzJiNkNqdy9zOTYtYy9waG90by5qcGciLCJnaXZlbl9uYW1lIjoi7IOB7JqxIiwiZmFtaWx5X25hbWUiOiLsnqUiLCJpYXQiOjE1NDk5MzgxODQsImV4cCI6MTU0OTk0MTc4NH0.qxU5AXw3oehWAQ4yJbRkD3XhV00EQwRejpatTNZulQ-FGl328JhM0HB_dFDtgHJnXd8I4wZwi8tSi6Q74PkJB6CfAj9EzzoE0ZXsv30PIDDfaSKD9tV-

I/Unity   ( 5898): Firebase App initializing app com.smilejsu.testgpgs2 (default 1).

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898): auth: Firebase.Auth.FirebaseAuth

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898):  [Play Games Plugin DLL] 02/12/19 11:37:00 +09:00 DEBUG: Invoking user callback on game thread

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898): task.Status: RanToCompletion

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):

I/Unity   ( 5898): User signed in successfully: 장상욱 (일등하이) (AVHIcC1XCPd5dyw5aMJ3)

I/Unity   ( 5898):

I/Unity   ( 5898): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5898):























https://firebase.google.com/docs/auth/unity/play-games?hl=ko


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
using System;
using Firebase.Auth;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
    public Button btn;
 
    // Start is called before the first frame update
    void Start()
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            //.RequestIdToken()
            .RequestServerAuthCode(false /* Don't force refresh */)
            .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
 
        btn.onClick.AddListener(() =>
        {
            Social.localUser.Authenticate((bool success) => {
                Debug.Log($"-------------------------> Authenticate: {success}");
                if (success)
                {
                    var authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
 
                    Debug.Log($"-------------------------> authCode: {authCode}");
 
                    Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
 
                    Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);
 
                    auth.SignInWithCredentialAsync(credential).ContinueWith(task => {
                        if (task.IsCanceled)
                        {
                            Debug.LogError("SignInWithCredentialAsync was canceled.");
                            return;
                        }
                        if (task.IsFaulted)
                        {
                            Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
                            return;
                        }
 
                        Firebase.Auth.FirebaseUser newUser = task.Result;
                        Debug.LogFormat("User signed in successfully: {0} ({1})",
                            newUser.DisplayName, newUser.UserId);
                    });
                }
            });
        });    
    }
 
}
 
cs

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660):  [Play Games Plugin DLL] 02/12/19 11:33:12 +09:00 DEBUG: Populating Achievements, status = VALID

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660):  [Play Games Plugin DLL] 02/12/19 11:33:12 +09:00 DEBUG: Found 5 Achievements

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660):  [Play Games Plugin DLL] 02/12/19 11:33:12 +09:00 DEBUG: Maybe finish for Achievements

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660):  [Play Games Plugin DLL] 02/12/19 11:33:12 +09:00 DEBUG: Auth finished. Proceeding.

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660):  [Play Games Plugin DLL] 02/12/19 11:33:12 +09:00 DEBUG: Invoking Callbacks: System.Action`2[System.Boolean,System.String]

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660): -------------------------> Authenticate: True

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660): -------------------------> authCode: 4/7wBe0ueDT41l9ElS78jQNmj42Jav6SzcXcORFFRJkIRYqbun0WFvbMvU699xWQJ_u-Vh5YSINfk6lJj2Ze285UM

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660): Firebase App initializing app com.smilejsu.testgpgs2 (default 1).

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

I/Unity   ( 5660):  [Play Games Plugin DLL] 02/12/19 11:33:13 +09:00 DEBUG: Invoking user callback on game thread

I/Unity   ( 5660):

I/Unity   ( 5660): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I/Unity   ( 5660):

E/Unity   ( 5660): SignInWithCredentialAsync encountered an error: System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> Firebase.FirebaseException: The supplied auth credential is malformed or has expired. [ Error getting access token from playgames.google.com, OAuth2 redirect uri is: http://localhost, response: OAuth2TokenResponse{params: error=invalid_client&error_description=Unauthorized, httpMetadata: HttpMetadata{status=401, cachePolicy=NO_CACHE, cacheDuration=null, cacheImmutable=false, staleWhileRevalidate=null, filename=null, lastModified=null, headers=HTTP/1.1 200 OK

E/Unity   ( 5660):

E/Unity   ( 5660): , cookieList=[]}} ]

E/Unity   ( 5660):    --- End of inner exception stack trace ---

E/Unity   ( 5660):    --- End of inner exception stack trace ---

E/Unity   ( 5660): ---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> Firebase.FirebaseException: The supplied auth credential is malformed or has expired. [ Error getting access token from playgames.google.com, OAuth2 redirect uri is: http://localhost, resp




반응형