java - 在 Android 上设置谷歌登录时出现 "FirebaseError: Invalid authentication credentials provided"错误

标签 java android authentication oauth-2.0 firebase

我一直在尝试为我的应用设置谷歌登录。

我按照这里的教程:

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

我做了一切。我可以登录登录和退出登录。

然后我添加了一个异步任务来获取 token ,它似乎已成功检索。它的实现如下:

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();
            }
        }

    }

所以在运行该方法并获取 token 后,我运行通用的 firebase 代码以连接到 Firebase(已经设置了一个谷歌应用程序,将客户端 ID 放入 firebase,启用它等)我从https://www.firebase.com/docs/android/guide/login/google.html

并实现如下:

 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();


        }
    });
}

}

所以最终,我登录,我得到一个 token ,然后将它传递给 firebase authWithOAuthToken 方法,然后我得到错误:

FirebaseError: Invalid authentication credentials provided.

有人能看出我的代码有什么问题吗?我感觉 token 无效,但找不到检查其有效性的方法。

如果您需要我发布更多信息,请告诉我,我尽量保持简短。

感谢任何可以提供帮助的人!

最佳答案

每当我需要在 Android 上使用 Firebase 进行身份验证时,我都会返回到 Firebase Android login demo .这是该应用程序的方式 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);

看起来你得到了 cross-client id for the user ,这不是有效的 OAuth token 。

更新 (20160308):现在我改为查看 FirebaseUI library ,其中包括此功能并且是最新的。

关于java - 在 Android 上设置谷歌登录时出现 "FirebaseError: Invalid authentication credentials provided"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210402/

相关文章:

android - 在 Android 上捕获视频的最大持续时间

php - 为什么$_SESSION不携带数据到单独的PHP文件?

java - 使用 Facebook 的 Java 登录获取用户 accessToken

authentication - 带有自定义 header 名称的 Swagger/OpenAPI Bearer 身份验证

java - 我应该在学习Android之前学习Java吗

java - SwipeListView 使用 NullPointerException

java - 哪个 JAXB bean 对应于这个 JSON?

android - 在不使用 MediaRecorder 的情况下使用 Camera2 API 进行录制

java - 我想将图像序列转换为 SD 卡中的视频

java - PHP在Java中的range函数