Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Authentication using Google, Where must I obtain the id_token?

In the Firebase unity3D SDK When I try to obtain the credential it ask me for an id_token and an access_token .

I have guess that access_token must be null for some examples I have seen, but I have no idea of what to do with this id_token. Code example:

This is the call:

Firebase.Auth.GoogleAuthProvider.GetCredential(string id_token,string access_token);

This is an unity3D example code:

public void GoogleLogin(Action<bool> loginOK)
{
    string id_token = "90096201****-353hvgf63fecvvc3mi****s6140f98a.apps.googleusercontent.com";
    Firebase.Auth.Credential credential;
    credential = Firebase.Auth.GoogleAuthProvider.GetCredential(id_token,null);
    auth.SignInWithCredentialAsync(credential).ContinueWith (task => 
    {
        if (!task.IsCanceled && !task.IsFaulted)
        {
            loginOK(true);
        }
        else
        {
            loginOK(false);
        }

        if (task.Exception != null)
        {
            Debug.LogException(task.Exception);
        }
    });
}

I thought it would be the Oauth 2.0 token which comes from from the google console. But this seems to not be working . Google answer is telling me the next:

11-29 13:58:25.476 com.google.android.gms 2009 3225 I AuthChimeraService
"message": "Unable to parse Google id_token: 90096201****-353hvgf63fecvvc3mi****s6140f98a.apps.googleusercontent.com"

Any idea of what I'm doing wrong?

like image 902
A.Quiroga Avatar asked Nov 29 '16 13:11

A.Quiroga


People also ask

How do I get the JWT token from Firebase authentication?

To achieve this, you must create a server endpoint that accepts sign-in credentials—such as a username and password—and, if the credentials are valid, returns a custom JWT. The custom JWT returned from your server can then be used by a client device to authenticate with Firebase (iOS+, Android, web).

How do I get Firebase authentication key?

To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format. To generate a private key file for your service account: In the Firebase console, open Settings > Service Accounts. Click Generate New Private Key, then confirm by clicking Generate Key.

Where does Firebase Auth store token?

Token can be found in firebaseLocalStorageDB.


2 Answers

The firebase sample for google sign in in unity should help you with this. One of the first steps mentions:

Follow instructions for Android and iOS to get an ID token for the Google sign in.

The firebase documentation also has a subsection for manually verifying id tokens but they do advice against this.

like image 112
MX D Avatar answered Sep 30 '22 21:09

MX D


Yes, that is a Oauth 2.0 token that is required here,

for that you can use this.

n you can't provide your own token as it may change with respect to user.

private void getGoogleOAuthTokenAndLogin() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
   // Log.e("SahajLOG", "Login PREF ISSSSSSSS ONCREATE  "+prefs.getBoolean("AuthByGplus", AuthByGplus));
    if (!prefs.getBoolean("AuthByGplus", AuthByGplus)) {
        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            String errorMessage = null;

            @Override
            protected String doInBackground(Void... params) {
                String token = null;

                try {
                    String scope = String.format("oauth2:%s", Scopes.PLUS_LOGIN);
                    token = GoogleAuthUtil.getToken(MainActivity.this, Plus.AccountApi.getAccountName(mGoogleApiClient), scope);
                } catch (IOException transientEx) {
                /* Network or server error */
                    Log.e("SahajLOG", "Error authenticating with Google: " + transientEx);
                    errorMessage = "Network error: " + transientEx.getMessage();
                } catch (UserRecoverableAuthException e) {
                    Log.w("SahajLOG", "Recoverable Google OAuth error: " + e.toString());
                /* We probably need to ask for permissions, so start the intent if there is none pending */
                    if (!mIntentInProgress) {
                        mIntentInProgress = true;
                        Intent recover = e.getIntent();
                        startActivityForResult(recover, MainActivity.GOOGLE_SIGIN);
                    }
                } catch (GoogleAuthException authEx) {
                /* The call is not ever expected to succeed assuming you have already verified that
                 * Google Play services is installed. */
                    Log.e("SahajLOG", "Error authenticating with Google: " + authEx.getMessage(), authEx);
                    errorMessage = "Error authenticating with Google: " + authEx.getMessage();
                }

                return token;
            }

            @Override
            protected void onPostExecute(String token) {
                mGoogleLoginClicked = false;
                Intent resultIntent = new Intent();
                if (token != null) {
                    Log.e("SahajLOG", "TOKEN IS  " + token);
               //     firebaseAuthWithGoogle(token);


                    //onGoogleLoginWithToken(token);
                    resultIntent.putExtra("oauth_token", token);
                } else if (errorMessage != null) {
                    resultIntent.putExtra("error", errorMessage);
                }
                setResult(MainActivity.GOOGLE_SIGIN, resultIntent);
                finish();
            }
        };
        task.execute();
    }
    Log.e("SahajLOG", "oAuthCalled");
    /* Get OAuth token in Background */
}
like image 45
Sahaj Rana Avatar answered Sep 30 '22 21:09

Sahaj Rana