Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API key not valid #188

Open
behhak opened this issue Nov 13, 2021 · 12 comments
Open

API key not valid #188

behhak opened this issue Nov 13, 2021 · 12 comments
Labels

Comments

@behhak
Copy link

behhak commented Nov 13, 2021

I'm learning firebase through Firebase Android Codelab - Build Friendly Chat
I've done all coding according to instruction till the end of Enable Authentication section.
But when trying to test the app I encounter with the following message:
2021-11-13 09:42:17.083 4741-4741/? E/AuthUI: A sign-in error occurred. com.google.firebase.FirebaseException: An internal error has occurred. [ API key not valid. Please pass a valid API key. ] at com.google.android.gms.internal.firebase-auth-api.zzto.zza(com.google.firebase:firebase-auth@@21.0.1:4) at com.google.android.gms.internal.firebase-auth-api.zzuw.zza(com.google.firebase:firebase-auth@@21.0.1:9) at com.google.android.gms.internal.firebase-auth-api.zzux.zzl(com.google.firebase:firebase-auth@@21.0.1:1) at com.google.android.gms.internal.firebase-auth-api.zzuu.zzk(com.google.firebase:firebase-auth@@21.0.1:25) at com.google.android.gms.internal.firebase-auth-api.zztl.zzh(com.google.firebase:firebase-auth@@21.0.1:1) at com.google.android.gms.internal.firebase-auth-api.zzoj.zza(com.google.firebase:firebase-auth@@21.0.1:2) at com.google.android.gms.internal.firebase-auth-api.zzvb.zza(com.google.firebase:firebase-auth@@21.0.1:23) at com.google.android.gms.internal.firebase-auth-api.zzul.zza(com.google.firebase:firebase-auth@@21.0.1:4) at com.google.android.gms.internal.firebase-auth-api.zzpt.zzq(com.google.firebase:firebase-auth@@21.0.1:4) at com.google.android.gms.internal.firebase-auth-api.zztp.zzl(com.google.firebase:firebase-auth@@21.0.1:5) at com.google.android.gms.internal.firebase-auth-api.zzql.zzd(com.google.firebase:firebase-auth@@21.0.1:3) at com.google.android.gms.internal.firebase-auth-api.zzqk.accept(Unknown Source:6) at com.google.android.gms.common.api.internal.zacj.doExecute(com.google.android.gms:play-services-base@@17.5.0:2) at com.google.android.gms.common.api.internal.zah.zaa(com.google.android.gms:play-services-base@@17.5.0:9) at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zac(com.google.android.gms:play-services-base@@17.5.0:193) at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zab(com.google.android.gms:play-services-base@@17.5.0:158) at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zaa(com.google.android.gms:play-services-base@@17.5.0:126) at com.google.android.gms.common.api.internal.GoogleApiManager.handleMessage(com.google.android.gms:play-services-base@@17.5.0:164) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:214) at android.os.HandlerThread.run(HandlerThread.java:67)
It seems the the API key given in mock-google-services.json has expired.
What's the solution for this issue?

@ghost
Copy link

ghost commented Nov 17, 2021

The issue is something else I used my own Firebase account but getting same issue

@ghost
Copy link

ghost commented Nov 17, 2021

Found the solution, the issue is occurring because
if (BuildConfig.DEBUG) { FirebaseDatabase.getInstance().useEmulator("10.0.2.2", 9000) FirebaseAuth.getInstance().useEmulator("10.0.2.2", 9099); FirebaseStorage.getInstance().useEmulator("10.0.2.2", 9199) }
here BuildConfig.DEBUG is giving false when app build, that why the emulator is not connection with app, make sure BuildConfig is imported from Build generated file

@behhak
Copy link
Author

behhak commented Nov 18, 2021

"make sure BuildConfig is imported from Build generated file"
How?

@ghost
Copy link

ghost commented Nov 18, 2021

"make sure BuildConfig is imported from Build generated file" How?

before pasting the above code on onCreate method go to Build -> Rebuild project it will create an Java (generated) folder which contains the BuildConfig file use that BuildConfig

@jquinterom
Copy link

The issue is something else I used my own Firebase account but getting same issue

Me too. I can't found the API KEY

@DevinRC
Copy link

DevinRC commented Jan 2, 2022

"make sure BuildConfig is imported from Build generated file" How?

before pasting the above code on onCreate method go to Build -> Rebuild project it will create an Java (generated) folder which contains the BuildConfig file use that BuildConfig

Can you please elaborate further? I bypassed the if (BuildConfig.DEBUG) condition by setting it to True, but I still get an exception that the API key is invalid.
I believe the issue is lying with the key truly being expired/invalid, and not with the application logic.

@ghost
Copy link

ghost commented Jan 2, 2022

"make sure BuildConfig is imported from Build generated file" How?

before pasting the above code on onCreate method go to Build -> Rebuild project it will create an Java (generated) folder which contains the BuildConfig file use that BuildConfig

Can you please elaborate further? I bypassed the if (BuildConfig.DEBUG) condition by setting it to True, but I still get an exception that the API key is invalid. I believe the issue is lying with the key truly being expired/invalid, and not with the application logic.

Actually the key is truly invalid, but local emulator does not require any valid api key, so for time being use all the code inside if (BuildConfig.DEBUG) without if

@DevinRC
Copy link

DevinRC commented Jan 2, 2022

SignInActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // This codelab uses View Binding
    // See: https://developer.android.com/topic/libraries/view-binding
    binding = ActivitySignInBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Initialize FirebaseAuth
    auth = Firebase.auth    <-- Wrong code
}

Turns out I was having a code segment in the wrong place. Removing it fixed the issue.
There is nothing wrong with the API key at the time of writing.
Try comparing your code with the provided solution in the \codelab-friendlychat-android\build-android folder

@nishatoma
Copy link

nishatoma commented Jan 13, 2022

Was anyone able to resolve this issue?

Update: I was using the wrong BuildConfig class, I deleted some BuildConfig Import, and it ended up using the local BuildConfig class from the project, which is the correct file.

@hebadi
Copy link

hebadi commented Jun 23, 2022

Hello its June 23 and I'm still having this same issue today. "E/AuthUI: A sign-in error occurred."

@DevinRC

Turns out I was having a code segment in the wrong place.

this was definitely not my issue, but just to be certain I copied the whole SignInActivity and MainActivity from the completed version and pasted it onto my version. The difference I noticed was the code lab said to put some code into the onCreate and the completed one put it instead it onStart. Not sure how much of a difference that made but for my issue, it persisted nonetheless after doing this direct copy paste.

@nishatoma

I was using the wrong BuildConfig class

This was also not my issue, I control click the class and see its from the appropriate "build" folder that was generated.

So, I'm stuck. How do i move forward? i would love some guidance here

June 24th update: I removed the google-services.json file and replaced it with my own after making a real project on firebase, thinking maybe something was up with my firebase emulator. But no, this still didn't work and gave me the same error. It said there was a network connection issue so i thought maybe i should try the app on a physical device instead of emulator to ensure wifi connection is there, but nope that did nothing too. So still stuck here. Before doing this codelab, i completed this one: https://developer.android.com/codelabs/advanced-android-kotlin-training-login#0
and everything went smoothly there so not sure whats up.

@matt-hires
Copy link

"make sure BuildConfig is imported from Build generated file" How?

before pasting the above code on onCreate method go to Build -> Rebuild project it will create an Java (generated) folder which contains the BuildConfig file use that BuildConfig

Can you please elaborate further? I bypassed the if (BuildConfig.DEBUG) condition by setting it to True, but I still get an exception that the API key is invalid. I believe the issue is lying with the key truly being expired/invalid, and not with the application logic.

Actually the key is truly invalid, but local emulator does not require any valid api key, so for time being use all the code inside if (BuildConfig.DEBUG) without if

if anyone else runs into this, just add buildConfig = true to the buildFeatures in app/build.gradle.kts:

    buildFeatures {
        viewBinding = true
        buildConfig = true
    }

After that rebuild project via ide or run gradle clean build.

@skille7
Copy link

skille7 commented Oct 4, 2024

"make sure BuildConfig is imported from Build generated file" How?

before pasting the above code on onCreate method go to Build -> Rebuild project it will create an Java (generated) folder which contains the BuildConfig file use that BuildConfig

fake you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants