playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.46k stars 967 forks source link

Accessing Auth Token? #10

Closed MrTinto closed 9 years ago

MrTinto commented 10 years ago

Is there any way to access the Auth Token that was used when authenticating the user? I need to use it to authenticate the user with my own server.

seaders commented 9 years ago

Awesome work, @claywilkinson! This, honest to God, is such a relief for us to be able to have the fully legit plugin in. Thanks so much again (closing this thread will be a pleasure, I'd say :P ).

illa3d commented 9 years ago

I've encountered some issues with 0.9.21 with the updated token support. Manually cleaned the old version of the plugin (0.9.20) and installed the 0.9.21.

ERROR: first when i try to setup the android it says it's missing "template-GameInfo.txt" ERROR: two errors appear in unity when trying to compile:

Assets/GooglePlayGames/Platforms/Native/NativeClient.cs(378,26): error CS0117: `GooglePlayGames.GameInfo' does not contain a definition for `WebClientIdInitialized'
Assets/GooglePlayGames/Platforms/Native/NativeClient.cs(386,68): error CS0117: `GooglePlayGames.GameInfo' does not contain a definition for `WebClientId'

(RESOLVED - get latest .unity file)

UPDATE: Login & token from Android working perfectly.

You can get the token and e-mail by calling:

string email = PlayGamesPlatform.Instance.GetUserEmail();
string accessToken = PlayGamesPlatform.Instance.GetAccessToken();
claywilkinson commented 9 years ago

Thanks for the feedback. Looks like I missed a couple files. I'll rebuild the unitypackage today. The corrected GameInfo.cs can be copied from https://github.com/playgameservices/play-games-plugin-for-unity/blob/master/source/PluginDev/Assets/GooglePlayGames/GameInfo.cs

The Template is at https://github.com/playgameservices/play-games-plugin-for-unity/blob/master/source/PluginDev/Assets/GooglePlayGames/Editor/template-GameInfo.txt

illa3d commented 9 years ago

@claywilkinson While AccessToken works I'm having difficulties accessing the idToken via: string accessToken = PlayGamesPlatform.Instance.GetIdToken(); After user login it just hangs.

Android log:

08-04 17:16:25.135: D/GamesUnitySDK(26847): Performing Android initialization of the GPG SDK
08-04 17:16:25.405: E/GamesNativeSDK(26847): Exception in dalvik/system/DexClassLoader.loadClass: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.games.NativeSdkEntryPoints" on path: /data/data/com.aarace.free/app_.gpg.classloader/de80b70ed0da0dfe988a41fa560612ee.jar.
08-04 17:16:25.430: I/GamesNativeSDK(26847): Auth operation started: SIGN IN
08-04 17:16:25.430: I/GamesNativeSDK(26847): Connecting to Google Play...
08-04 17:16:25.735: V/AuthAccountOperation(9516): offline access is not requested
08-04 17:16:25.735: V/BaseAuthAsyncOperation(9516): All scopes had been granted in the past, skip access token fetching
08-04 17:16:25.780: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 17:16:25.900: V/GamesNativeSDK(26847): Play Games callback indicates connection.
08-04 17:16:25.925: I/GamesNativeSDK(26847): Successfully connected to Google Play.
08-04 17:16:26.120: I/Unity(26847): Login was successful!
08-04 17:16:26.160: I/Unity(26847): jo_plusAccountApi: UnityEngine.AndroidJavaObject
08-04 17:16:26.165: I/Unity(26847): Calling GetApiClient....
08-04 17:16:26.280: V/AuthAccountOperation(9516): offline access is not requested
08-04 17:16:26.310: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 17:16:26.455: I/Unity(26847): Done GetApiClient is UnityEngine.AndroidJavaObject
08-04 17:16:26.455: I/Unity(26847): apiClient: UnityEngine.AndroidJavaObject
08-04 17:16:26.460: I/Unity(26847): GOOGLE EMAIL: mymail@gmail.com
08-04 17:16:26.465: I/Unity(26847): jo_plusAccountApi: UnityEngine.AndroidJavaObject
08-04 17:16:26.465: I/Unity(26847): Calling GetApiClient....
08-04 17:16:26.485: V/AuthAccountOperation(9516): offline access is not requested
08-04 17:16:26.485: V/BaseAuthAsyncOperation(9516): All scopes had been granted in the past, skip access token fetching
08-04 17:16:26.525: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 17:16:26.570: I/Unity(26847): Done GetApiClient is UnityEngine.AndroidJavaObject
08-04 17:16:26.570: I/Unity(26847): apiClient: UnityEngine.AndroidJavaObject
08-04 17:16:26.640: I/Unity(26847): Before GetEmail
08-04 17:16:26.640: I/Unity(26847): jo_plusAccountApi: UnityEngine.AndroidJavaObject
08-04 17:16:26.640: I/Unity(26847): Calling GetApiClient....
08-04 17:16:26.680: V/AuthAccountOperation(9516): offline access is not requested
08-04 17:16:26.680: V/BaseAuthAsyncOperation(9516): All scopes had been granted in the past, skip access token fetching
08-04 17:16:26.740: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 17:16:26.750: I/Unity(26847): Done GetApiClient is UnityEngine.AndroidJavaObject
08-04 17:16:26.750: I/Unity(26847): apiClient: UnityEngine.AndroidJavaObject
08-04 17:16:26.755: I/Unity(26847): After GetEmail email: mymail@gmail.com
08-04 17:16:26.835: I/GLSUser(2880): [GLSUser] getTokenFromGoogle [account: <ELLIDED:5906>, callingPkg: com.aarace.free, service: audience:server:client_id:434824177801-q5p0394959fm1j40jj8s6o3t7451h3nes.apps.googleusercontent.com
08-04 17:16:27.750: I/GLSUser(2880): [GLSUser] getAuthtoken(<ELLIDED:5906>, audience:server:client_id:434824177801-q5p039959fm1j40jj8s6o3t7451h3nes.apps.googleusercontent.com) -> status: UNKNOWN)
08-04 17:16:27.750: I/GLSUser(2880): [GLSUser] Extracting token using key: Auth
08-04 17:16:27.750: W/GLSActivity(2880): gms.StatusHelper Status from wire: INVALID_AUDIENCE status: null
08-04 17:16:27.870: I/Unity(26847): AndroidJavaException: com.google.android.gms.auth.GoogleAuthException: Unknown
08-04 17:16:27.870: I/Unity(26847):   at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at UnityEngine.AndroidJNISafe.CallStaticStringMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at UnityEngine.AndroidJavaObject._CallStatic[String] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at UnityEngine.AndroidJavaObject.CallStatic[String] (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at GooglePlayGames.Native.NativeClient.GetIdToken () [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at GooglePlayGames.PlayGamesPlatform.GetIdToken () [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at GooglePlusManager.DoServerLoginWithGooglePlus () [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at GooglePlusManager.OnAuthenticate (Boolean success) [0x00000] in <filename unknown>:0 
08-04 17:16:27.870: I/Unity(26847):   at GooglePlayGames.Native.NativeClient+<InvokeCallbackOnGameThread>c__Ano
claywilkinson commented 9 years ago

This line gms.StatusHelper Status from wire: INVALID_AUDIENCE status: null means you have not configured the web app client ID in the setup dialog. The steps are:

  1. On the Play Console, select your game and make sure you have a linked app which is the type "Web".
  2. copy the client id from the web app and paste it into the web client Id field in the setup dialog.

Also make sure you update the two files that need to be patched (GameInfo.cs and template-GameInfo.txt)

illa3d commented 9 years ago

I've already set the web app client ID. Now I've set the client id again and I have the same issue with both updated gameinfo files (+NativeClient.cs:378,386 method name fixes).

When I try to get IDToken only, it hangs with the above log.

When I try to get AccessToken only, login works and I get the access token. This is the log I get with the same setup only requesting AccessToken:

08-04 18:24:51.295: D/GamesUnitySDK(32436): Performing Android initialization of the GPG SDK
08-04 18:24:51.835: E/GamesNativeSDK(32436): Exception in dalvik/system/DexClassLoader.loadClass: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.games.NativeSdkEntryPoints" on path: /data/data/com.aarace.free/app_.gpg.classloader/de80b70ed0da0dfe988a41fa560612ee.jar.
08-04 18:24:51.920: I/GamesNativeSDK(32436): Auth operation started: SIGN IN
08-04 18:24:51.920: I/GamesNativeSDK(32436): Connecting to Google Play...
08-04 18:24:52.725: V/AuthAccountOperation(9516): offline access is not requested
08-04 18:24:52.730: V/BaseAuthAsyncOperation(9516): All scopes had been granted in the past, skip access token fetching
08-04 18:24:52.825: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 18:24:53.475: V/GamesNativeSDK(32436): Play Games callback indicates connection.
08-04 18:24:53.505: I/GamesNativeSDK(32436): Successfully connected to Google Play.
08-04 18:24:53.845: I/Unity(32436): Login was successful!
08-04 18:24:53.930: I/Unity(32436): jo_plusAccountApi: UnityEngine.AndroidJavaObject
08-04 18:24:53.945: I/Unity(32436): Calling GetApiClient....
08-04 18:24:54.170: V/AuthAccountOperation(9516): offline access is not requested
08-04 18:24:54.220: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 18:24:54.420: I/Unity(32436): Done GetApiClient is UnityEngine.AndroidJavaObject
08-04 18:24:54.420: I/Unity(32436): apiClient: UnityEngine.AndroidJavaObject
08-04 18:24:54.430: I/Unity(32436): GOOGLE EMAIL: mymail@gmail.com
08-04 18:24:54.435: I/Unity(32436): jo_plusAccountApi: UnityEngine.AndroidJavaObject
08-04 18:24:54.435: I/Unity(32436): Calling GetApiClient....
08-04 18:24:54.470: V/AuthAccountOperation(9516): offline access is not requested
08-04 18:24:54.470: V/BaseAuthAsyncOperation(9516): All scopes had been granted in the past, skip access token fetching
08-04 18:24:54.540: V/BaseAuthAsyncOperation(9516): access token request successful
08-04 18:24:54.545: I/Unity(32436): Done GetApiClient is UnityEngine.AndroidJavaObject
08-04 18:24:54.545: I/Unity(32436): apiClient: UnityEngine.AndroidJavaObject
08-04 18:24:54.675: I/Unity(32436): GOOGLE ACCESS TOKEN: ya29.xQE4kfk345lk23541kj5334l5j234l54kj23T3f8v7eTg_O4RV6-Ui235gfg4I-DFBtrNNY
claywilkinson commented 9 years ago

With the update GameInfo files, you should not need to modify NativeClient. - This is the correct statement: string scope = "audience:server:client_id:" + GameInfo.WebClientId;

I think what is happening is the references in NativeClient to the iOS client you added are not set (since you are running Android) and it is incorrectly trying to get the id token for null.

illa3d commented 9 years ago

I reverted to the original NativeClient.cs and everything works! Thanks a BUNCH for clarifying!

Keep up the excellent work and thanks for this token & webview feature which we were dearly missing :) :+1:

illa3d commented 9 years ago

iOS report:

BUILD ERROR: Error occured during the iOS build, a missing reference. FIX: CustomWebViewApplication.mm:21 #import <GTMOAuth2Authentication.h> changed to #import <GoogleOpenSource/GTMOAuth2Authentication.h>

Login on iOS working!

seaders commented 9 years ago

I always convert the localUser, or in fact just reference the actual Google User for fear that Social.localUser may be referring in fact to the iOS GameCenter user.

While it doesn't look like your problem, it may be worth trying. Debugging those classes in iOS to see what's happening might be worth trying too. Just set a few breakpoints in CustomWebApplication.mm.

For the class that interacts with that side of things, I've this helper function,

private PlayGamesPlatform GooglePlayUser
{
    get { return PlayGamesPlatform.Instance; }
}

Which is then used like,

override protected void _Login()
{
    GooglePlayUser.Authenticate(Login);
}
illa3d commented 9 years ago

EDIT: everything works, I messed up the client id in play console :\

That sadly didn't help either... Still getting FALSE.

So far tried the original from documentation: PlayGamesPlatform.Activate(); Social.localUser.Authenticate(OnAuthenticate); and the instance from documentation: PlayGamesPlatform.Instance.localUser.Authenticate(OnAuthenticate); and yours: PlayGamesPlatform.instance.Authenticate(OnAuthenticate);

I don't know what I'm missing :\

claywilkinson commented 9 years ago

Can you post the log?

illa3d commented 9 years ago

EDIT: everything works, I messed up the client id in play console :\

On first run:

2015-08-07 21:24:34.888 free[443:66986] INFO: Auth operation started: SIGN IN
2015-08-07 21:24:34.901 free[443:66986] INFO: Auth operation started: SIGN IN
GOOGLE AUTHENTICATE: False

on any other attempt:

2015-08-07 21:25:52.035 free[443:67136] INFO: Auth operation started: SIGN IN
GOOGLE AUTHENTICATE: False

I tried building both with cocoapods .xcworkspace and normal .xcodeproj with manual addition of gpg libraries with both success builds.

illa3d commented 9 years ago

EDIT: everything works, I messed up the client id in play console :\

I also tried the combinations:

1. PlayGamesPlatform.Activate(); Social.localUser.Authenticate (OnAuthenticate); 2. PlayGamesPlatform.Activate(); PlayGamesPlatform.Instance.localUser.Authenticate(OnAuthenticate); 3. PlayGamesPlatform.Activate(); PlayGamesPlatform.Instance.Authenticate(OnAuthenticate); Debug.Log for 1,2,3:

Social.Active: GooglePlayGames.PlayGamesPlatform
Social.localUser.authenticated: False
PlayGamesPlatform.Instance.IsAuthenticated(): False
PlayGamesPlatform.Instance.authenticated: False

4. Social.localUser.Authenticate (OnAuthenticate); 5. PlayGamesPlatform.Instance.localUser.Authenticate(OnAuthenticate); 6. PlayGamesPlatform.Instance.Authenticate(OnAuthenticate); Debug.Log for 4,5,6:

Social.Active: UnityEngine.SocialPlatforms.GameCenter.GameCenterPlatform
Social.localUser.authenticated: False
PlayGamesPlatform.Instance.IsAuthenticated(): False
PlayGamesPlatform.Instance.authenticated: False
seaders commented 9 years ago

Just covering all the basic basics, you've obviously run, and have the correct values in for the iOS Setup stuff within Unity? And haven't changed some of those values since the last time you've done a build?

Also, if you're comfortable with it, I'd be happy to test for you, if you want to pass your details my way privately?

seaders commented 9 years ago

Email sent from my SixMinute company email.

illa3d commented 9 years ago

Oooo-kay. I'm a total tool. Although I have checked everything a bunch of times, one thing I completely missed is to check the Play console client id for the iOS application, which was wrong. Sorry for the time taken. /runs away

Thanks for this new feature set and solving the iOS rejection issue! Much appreciated @seaders @claywilkinson

Shall we eliminate last redundant posts so we don't clutter space?

seaders commented 9 years ago

Thing is, I find that's something that trips everyone (myself included) many times. I think it's the messaging that comes back is the worst part of it. On Android, if there's an incorrect key signing, it can give you no information at all, and just give you a continuous spinny wheel, and no information on screen, or in the logs. But then sometimes, it'll do like this, everything seems fine, but nothing actually works.

I know on Facebook, when you run that with debug mode, it spits loads of real information at you that really help get through issues like that, and it's one area where the main Google Play login stuffs would really help out with.

Marurban commented 8 years ago

Did anyone find a way to easily get authentication code? Methods with invoking Android java methods doesn't work, getToken(...) is deprecated and I get an exception.

I found method:

public string GetAuthorizationCode(string serverClientID)

in source/PluginDev/Assets/GooglePlayGames/Platforms/Android/AndroidTokenClient.cs, when it will be implemented?

W4der commented 8 years ago
using GooglePlayGames.BasicApi;
using GooglePlayGames;

string authCode; 
    void GetAuthCode()
    {
        Debug.Log("GetAuthCode");

        Social.localUser.Authenticate((bool success) =>
        {
            PlayGamesPlatform.Instance.GetServerAuthCode((CommonStatusCodes status, string code) =>
            {
                Debug.Log("Status: " + status.ToString());
                Debug.Log("Code: " + code);
                authCode = code;
            }
            );
        });
    }