FirebaseError: Invalid authentication credentials provided -- error when setting up google sign in on Android

425 views
Skip to first unread message

Micah Simmons

unread,
Jul 6, 2015, 12:02:34 PM7/6/15
to fireba...@googlegroups.com

I've been trying to set up a google login for my app.

I followed the tutorial here:

https://developers.google.com/identity/sign-in/android/start-integrating

I did everything. I can sign in sign in and sign out.

Then I added an Async task to get a token, which it seems to successfully retrieve. It is implemented as follows:

private class GetIdTokenTask extends AsyncTask<Void, Void, String> {


            private static final  String SERVER_CLIEdNT_ID = "749433126040-ca4gfj7ucuh0m2suo3230u03o3d7doni.apps.googleusercontent.com";

        @Override
        protected String doInBackground(Void... params) {

            String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
            Account account = new Account(accountName, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
            String scopes = "audience:server:client_id:" + SERVER_CLIEdNT_ID; // Not the app's client ID.
            try {
                return GoogleAuthUtil.getToken(getApplicationContext(), account, scopes);
            } catch (IOException e) {
                Log.e(TAG, "Error retrieving ID token.", e);
                return null;
            } catch (GoogleAuthException e) {
                Log.e(TAG, "Error retrieving ID token. Google exception", e);
                return null;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            Log.i(TAG, "ID token: " + result);
            if (result != null) {

                idToken = result;

                Toast.makeText(context, "token is " + result, Toast.LENGTH_LONG).show();


            } else {
                // There was some error getting the ID Token
                // ...
                Toast.makeText(context, "token is null", Toast.LENGTH_LONG).show();
            }
        }

    }

So after running the method and getting the token, I then run the generic firebase code to connect to Firebase (having already set up a google app, put the client ID in firebase, enabled it etc.) I got the code from https://www.firebase.com/docs/android/guide/login/google.html

And implemented it as follows:

 public void loginFireBase() {


    Firebase ref = new Firebase("https://boiling-fire-944.firebaseio.com");
    ref.authWithOAuthToken("google", idToken, new Firebase.AuthResultHandler() {


        @Override
        public void onAuthenticated(AuthData authData) {
            // the Google user is now authenticated with your Firebase app

            Toast.makeText(context, "user succesfully authenticated with firebase", Toast.LENGTH_LONG).show();
            Toast.makeText(context, idToken, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onAuthenticationError(FirebaseError firebaseError) {
   ///////THIS IS THE PART WHERE THE ERROR IS GENEREATED  FROM//////////

            Log.v("firebase problem", firebaseError.toString());
            Toast.makeText(context, "I'm an authenticated error" + "id Token was " + idToken, Toast.LENGTH_LONG).show();


        }
    });
}

}

So ultimately, I login, I get a token, and then pass it to the firebase authWithOAuthToken method, and then I get the error:

FirebaseError: Invalid authentication credentials provided.

Can anyone see any problems in my code? I have a feeling the token is not valid but can't find a way to check its validity.

Tell me if you need me to post more, I was trying to keep it brief.

Thanks to anybody who can help !

Frank van Puffelen

unread,
Jul 6, 2015, 12:21:28 PM7/6/15
to fireba...@googlegroups.com
Cross-post from http://stackoverflow.com/questions/31210402/firebaseerror-invalid-authentication-credentials-provided-error-when-setting

Please always indicate when cross-posting.

My answer from StackOverflow:

Whenever I need to authenticate with Firebase on Android, I go back to the Firebase Android login demo. Here's how that app gets the OAuth token for Google authentication:

String scope = String.format("oauth2:%s", Scopes.PLUS_LOGIN);
token = GoogleAuthUtil.getToken(
                                GoogleOAuthActivity.this, 
                                
                                Plus.AccountApi.getAccountName(mGoogleApiClient), 
                                scope
                                scope);

It looks like you're getting the cross-client id for the user, which is not a valid OAuth token.

Reply all
Reply to author
Forward
0 new messages