检查您的 google-services 插件配置,未填充 default_web_client_id 字符串

Check your google-services plugin configuration, the default_web_client_id string wasn't populated

我刚开始使用 Firebase,并设计了一个简单的程序来测试 FirebaseUI 登录。我收到一个异常,即未填充默认 Web 客户端 ID 字符串。

Github https://github.com/firebase/FirebaseUI-Android/issues/1381 上有一个关于此的问题,但它似乎被我遗弃了。我已经将 gradle 依赖项更新到最新版本,但仍然没有成功。

代码如下:

(MainActivity.java)

package uk.ac.le.cityTourPlanner;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private FirebaseAuth mFirebaseAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;
    private static int RC_SIGN_IN = 1 ;

    List<AuthUI.IdpConfig> providers = Arrays.asList(
            new AuthUI.IdpConfig.EmailBuilder().build(),
            new AuthUI.IdpConfig.GoogleBuilder().build()
    );
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFirebaseAuth=FirebaseAuth.getInstance();
        mAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if(user!=null){
                    Toast.makeText(MainActivity.this, "user signed in!", Toast.LENGTH_LONG).show();
                }
                else{
                    startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(providers).
                            setIsSmartLockEnabled(false).build(),RC_SIGN_IN);
                }
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        mFirebaseAuth.addAuthStateListener(mAuthStateListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
    }

    public void signOut(View view) {
        AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Toast.makeText(MainActivity.this, "user signed out!", Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }
}

(MainActivity.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerInParent="true"
        android:textSize="25dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:id="@+id/button"
        android:text="Sign Out"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textSize="25dp"
        android:onClick="signOut"/>

</RelativeLayout>

(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="uk.ac.le.cityTourPlanner">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最后

(google-services.json)

{
  "project_info": {
    "project_number": "921658610309",
    "firebase_url": "https://city-tour-planner.firebaseio.com",
    "project_id": "city-tour-planner",
    "storage_bucket": "city-tour-planner.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:921658610309:android:0a33483e26b227f2",
        "android_client_info": {
          "package_name": "uk.ac.le.cityTourPlanner"
        }
      },
      "oauth_client": [
        {
          "client_id": "921658610309-10djmoc7i9k3doou7sq8sg4aog8v78jf.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "uk.ac.le.cityTourPlanner",
            "certificate_hash": "fff5b002b82089efa22081bac552cc6b8ccab44c"
          }
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyD6OWj9Roi1bm40jiD54B3V-h5FsgqaJ-0"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "921658610309-4dhq16mrdgfd45jkrt0vo9hmlr1m4ju7.apps.googleusercontent.com",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

我的情况也可以参考这个类似的question (same error, I answered

基本上,只需将 default_web_client_id 添加到您的 string.xml 文件中,如下所示:

<string name="default_web_client_id" translatable="false">webClientId.apps.googleusercontent.com</string>

或者像这样将它添加到 google-services.json 文件的 Oauth 部分(通常你不应该这样做,但对于这种情况,我在 answer 中解释了原因:

"oauth_client": [
    ...,
    {
         "client_id": "CLIENT_ID (ends with apps.googleusercontent.com)",
         "client_type": 3
    }
]

您可以在 GCP console like this

中找到 default_web_client_id

go to API & Services > Credentials and under OAuth 2.0 client IDs you will find a ID with the name Web Client

同时请确保 google-services.json 文件已放在 app 目录中,而不是放在顶级目录中。