Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I do server authentication with Google Play Services #222

Closed
jjduran121 opened this issue Sep 12, 2014 · 6 comments
Closed

How can I do server authentication with Google Play Services #222

jjduran121 opened this issue Sep 12, 2014 · 6 comments

Comments

@jjduran121
Copy link

I'm trying to implement server authentication through Google Play Services. I need to log in a client and pass a token to a server so our server can verify the user is who they say they are. Currently I am able to get the user authenticated, but all I receive back is the full name and a userID.

This plugin doesn't seem to respond with a session token. How am I supposed to authenticate the user? Ideally, I would like to receive a token that I can pass to our server, and have our server confirm the session is valid with Google. Am I missing something?

@is-blackhole
Copy link

Actually, I get it working with just some changes in c# plugin code.
https://developers.google.com/accounts/docs/CrossClientAuth describe how to do this.
GoogleAuthUtil.getToken(context, accountName, scope) will do the trick.

First, you need to obtain accountName - thats not a PlayGamesPlatform.Instance.GetUserDisplayName() will return.
You can obtain logged user accountName (actually email) via call to java Plus.AccountApi.getAccountName(apiClient). So, you need acces to actual google api client, which you can get with GameHelperManager.GetApiClient().

I have added this code into GooglePlayGames.Android.AndroidClient (since it holds GameHelperManager internal class instance) and call it in AndroidClient.RetrieveUserInfo method

        private void RetrieveUserEmail() {
            using (AndroidJavaClass jc_plus = new AndroidJavaClass("com.google.android.gms.plus.Plus")) {
                using (AndroidJavaObject jo_plusAccountApi = jc_plus.GetStatic<AndroidJavaObject>("AccountApi")) {
                    mUserEmail = jo_plusAccountApi.Call<string>("getAccountName", mGHManager.GetApiClient());
                    Logger.d("Player email: " + mUserEmail);
                }
            }
        }

Next, get a token

    string getToken() {
        string token = null;
        string email = "email you get in some way from AndroidClient.mUserEmail ";
        string scope = "audience:server:client_id:" + CLIENT_ID;
        using (AndroidJavaClass jc_unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"),
            jc_gau = new AndroidJavaClass("com.google.android.gms.auth.GoogleAuthUtil")) {
            using(AndroidJavaObject jo_Activity = jc_unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
                token = jc_gau.CallStatic<string>("getToken", jo_Activity, email, scope);
            }
        }
        Debug.Log("Token " + token);
        return token;
    }

The important part here is scope (see link above for details) and CLIENT_ID; you need to get OAuth2 Client ID for your web app in Google Developer Console, so android app can get tokens for your web app. Ah, dont forget to incluide <uses-permission android:name="android.permission.GET_ACCOUNTS" /> in your adnroid manifest.

Then you can POST token with other info to your server and verify;
I have checked whole path and can decode token with python oauth2client.client

Keep in mind, this is correct for plugin version before doge release (which seems to use c++ sdk).

@cloudjubei
Copy link

I've managed to get this successfully working pre-Doge release, but would be great if there was some way to get it working out of the box and especially with the new version.

@hex
Copy link

hex commented Jan 6, 2015

Will there be any support for this in the next version?

@claywilkinson
Copy link

Consolidating the issues, so closing this as a duplicate of #181

@ghost
Copy link

ghost commented Feb 18, 2015

Just curious, will this be added into an upcoming version of the unity plugin? The plugin works great for Android and IOS, so it would be wonderful to be able to use the leaderboards, saved games, and achievements without having to rewrite my games for the Unity Web Player. Thanks in advance for any information...

@rohitvishwakarma1819
Copy link

whats the update anyone ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants