Cannot resolve symbol default_web_client_id in Firebase's Android Codelab

北慕城南 提交于 2019-12-03 07:22:34

问题


I am trying to learn Firebase, so I went through the Android Codelab. The project they gave me however, had an error:

Cannot resolve symbol default_web_client_id

And I didn't know how to solve it, since I didn't know the value of default_web_client_id or what it is. It is in the onCreate() method: SigninActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);
    mFirebaseAuth = FirebaseAuth.getInstance();

    // Assign fields
    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);

    // Set click listeners
    mSignInButton.setOnClickListener(this);

    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

I have no idea what it is, what's its value is, and why is it giving me this error. I haven't changed anything so far except for adding the google-services.json. I have added my SHA-1 and enabled Google in the console.


回答1:


Sometimes there is issue while parsing google-services.json. I have reported this issue with to concerned team.

Meanwhile follow below step to fix this issue to get going further -

1) Open google-services.json file -> client -> oauth_client -> client_id

2) Copy this client ID and hardcode this .requestIdToken("your ID")

It would allow to request "IdToken" via GoogleSignInAccount post successful google login and to authorize your credential with firebase.

EDIT

Try deleting and recreating the project and re-importing new google-service.jsonin your Android project




回答2:


A more generic solution would be to add the google-services.json into the app's root directory.
And add

apply plugin: 'com.google.gms.google-services at the end of build.gradle file.

Explanation

When the app builds the key value pair strings from google-services.json config file are then placed into the values.xml file to make them globally available for use from anywhere in your code. This saves us from hard coding the client_id in your code.

Note

Do not add the default_web_client_id with client_id as its value in the strings.xml in order to avoid the error of duplication, Error: Duplicate resourceslater on when you run your code.




回答3:


Apparently R.string.default_web_client_id is generated from the IDE build

I had assumed we are supposed to manually add it - time consuming mistake

https://developers.google.com/android/guides/google-services-plugin

The google-services plugin has two main functions: 1) Process the google-services.json file and produce Android resources that can be used in your application's code.

~~~~

The main result of the JSON processing is to produce two XML files which you can reference as Android resources in your Java code.

And so - after successful build, if you search the IDE for string default_web_client_id , you will see one result is values.xml under the /generated folder, and there it has the values for your firebase config, like the example below.

Actually seeing that file, helped to clarify things here

<resources>
    <string name="default_web_client_id" translatable="false">123.apps.googleusercontent.com</string>
    <string name="firebase_database_url" translatable="false">https://123.firebaseio.com</string>
    <string name="gcm_defaultSenderId" translatable="false">123</string>
    <string name="google_api_key" translatable="false">123</string>
    <string name="google_app_id" translatable="false">123</string>
</resources>



回答4:


AN UPDATE TO THIS ANSWER. **The main issue with this right now is to make sure to download the json file from the same location. If the initial one came from the firebase console do not use the api console to get the file, and vise versa. The files are not the same **

I have the same error as before but it does not stop my build. What's more, it seems to resolve itself when I build a signed bundle, I checked it is just looking in the wrong path for the string.

none of the above answers resolved my issue.

I found this and the following steps in the build gradle files:

in the app level file (at the end)

} apply plugin: 'com.google.gms.google-services'

and in the project level file:

 dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath 'com.google.gms:google-services:3.1.0'

In addition to that my json file needed to be in the app folder; though there are options to have both a debug and release version of this file.

The only actual error I got was once the gradle files were right. Then it complained that it could not find the json file.




回答5:


I already have google-services.json downloaded and parsed, but still it doesn't find the string.

I noticed that my oauth_client had a key with client_type of 1 and that's all. In the Google API console, I only had an Android key.

So, you need to go to the API console and generate a Web Server key. Then, download your google-services.json again, and you'll have a oauth_client with a type of 3.

Now, the plugin will generate a string called default_web_client_id.




回答6:


In addition to the Dexto's Answer I would like to mention one more thing In the JSON file you will get two kind of client id

One Which is having client_type value 1 and Another with the client_type value 3 Make sure you specified the client_id of client_type which has value of 3




回答7:


I had the same problem or similar,

Make sure that in your google-services.json you have:

...    
"client": [
        ...
          "oauth_client": [
            ...
            {
              "client_id": "YOUR WEB CLIENT ID",
              "client_type": 3
            }     
...

For some reason the file downloaded from firebase console doesn't include it.

After adding the entry in the google-services.json file, everything started working as expected.

google-services-plugin documentation




回答8:


Fixed after using this link to create my backend id to Google API.

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

  • 1- Open the Credentials page in the API Console.

  • 2- The Web application type client ID is your backend server's OAuth 2.0 client ID.

After this, you can re-download your json file and android studio will automatically matching your string id.




回答9:


classpath 'com.google.gms:google-services:4.1.0'

has a problem. instead use:

classpath 'com.google.gms:google-services:4.2.0'



回答10:


Try downloading your .json file again after changing the configuration in the Firebase console. Use this newer configuration file, not the old one.




回答11:


After a time searching the "smart" fix without insert directly the client_id, following this answer from FirebaseUI project I just need to add the next line in app/build.gradle:

implementation 'com.firebaseui:firebase-ui-auth:4.3.2'



回答12:


Download your newest google-services.json. List of client_id is present for OAuth 2.0 client IDs in your Google Cloud Credentials.

Then check whether it contains client_id with "client_type" : 3 or not. If not, you need to create a new one:

  1. Open the Credentials page in the API Console.
  2. Click Create credentials -> OAuth cliend ID. Then chose type Web application.
  3. Wait 2-3 minutes, refresh Firebase Console & download your google-services.json again. It should contain client_id with "client_type" : 3 now.

Clean & rebuild your project to apply new API config.


The client_id with "client_type" : 3 is usually inside oauth_client tag, not services or other_platform_oauth_client.

If you fall to this case & cannot build the project, try copy your client_id to oauth_client tag and rebuild again.

"client": [
        ...
        "oauth_client": [
            ...
            {
              "client_id": "YOUR WEB CLIENT ID",
              "client_type": 3
            }
        ]
]     



回答13:


Update your project level build.gradle file with the following code:

buildscript {
repositories {
    google()
    jcenter()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    classpath 'com.google.gms:google-services:4.2.0'
}}
allprojects {
repositories {
    google()
    jcenter()
    maven { url "https://maven.google.com"}  
}}
task clean(type: Delete) {
delete rootProject.buildDir }

More Details:answerdone.com




回答14:


I know it is late to answer but hope this will help someone in the future.

To access there is no need to hard code default_web_client_id in app.

To access default_web_client_id in Android App from google-services.json, we have to add SHA1 key under FireBase project Settings.

Go to Firebase Console > Open Project > Select App > Add Fingerprint.

After this copy generated google-services.json to project.

After this you will see the difference in json file as below:

Before :

"oauth_client": []

After :

"oauth_client": [
    {
      "client_id": "23........4-asdj...........................asda.googleusercontent.com",
      "client_type": 1,
      "android_info": {
        "package_name": "com.abc.xyz",
        "certificate_hash": "asjhdashhs"
      }
    },.....

This will solve your issue.




回答15:


GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)      
 .requestIdToken("1036904004233h4cmmfcistvfjh3phqe1peht56g.apps.googleusercontent.com")
            .requestEmail()
            .build();   

in my experience u must include the Client ID like that in requestIdtoken and u must dont have an instant of default_web_client_id in the string.xml it's look weird but in my case that's the only method work for me.




回答16:


Download again google-services.json file from the firebase project that you connected with your android project and replace it in app/src directory. And then select clean project option which is in build sub menu. This worked for me.



来源:https://stackoverflow.com/questions/37810552/cannot-resolve-symbol-default-web-client-id-in-firebases-android-codelab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!