简体   繁体   中英

Working with multiple Firebase projects in an Android app for Google sign-in

My android app is using the GCM project which is registered with application keystore SHA1. I am using the another firebase project in my app to FCM which is using the different SHA1. Now i want to integrate the Google Sign-in in my app which required the SHA1 to register for my app for google sign-in. I don't want to remove my SHA1 from GCM project for this. How can i import the another firebase project such that it don't require my SHA1 key again.

  1. Google Sign-In component to work on Android, it requires a SHA-1 (the fingerprint of the key used to sign the APK) and package name (eg com.foo.bar) to be registered for the app . This combination allows Google Sign-In to know which Google API project is being used by a particular app. A given pair of SHA1 and Package Name is globally unique within Google (and Firebase projects), so if you try to add the same pair SHA-1 and package name to an upgraded Firebase project, you get an error that the OAuth2 client already exists (in the Google API project) see Image

  2. Warning: If you see this, don't delete your existing client ID for apps in production ! The right choice is to create a new app with the same your package name in the Firebase console for the upgraded project, but not to include a SHA1 . Now implement Google Sign In with Firebase Auth as normal. At one point you will have to configure your Google Sign Options object:

  3. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();

  4. The default_web_client_id string here is used to set the audience field of the ID token. The value comes from the google-services.json file, which is from the Firebase project rather than the Google project. You'll need to replace it with a client ID from the Google project. You can use any Web client ID, or create a new one: see image

  5. Next, back in the Firebase project, whitelist the client ID you just set for the GoogleSignInOptions in the Auth > Sign In Providers > Google section of the Firebase console. See Image

  6. Be sure to re-download your google-services.json and add it to your Android app. At this point, your Firebase project will accept Google ID tokens generated by your Google project - so your Android app will happily sign in to Google using the Google project, then authenticate with your Firebase project using the Google ID token following the normal approach . You'll be able to make authenticated calls to Google APIs associated with the Google API project, and authenticated calls to Firebase APIs using the Firebase project.

  7. Please find the document source for your reference

I ran into this issue as well. I discovered that there is actually a Google Developer Console associated with the Firebase account, which is hidden. My solution is head to Firebase Project Settings > Service accounts > Click the link under the Other service accounts: Firebase Console Service Accounts .

This will open up the Google Cloud Platform, you may close this page and get to your Google Developer Console, you will see a project with the exact name of your project name in your firebase shown.

You can also see OAuth 2.0 Client IDs are auto generated by Google Service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM