集成 Google 登录
将 Google 登录集成到应用中,让用户能够使用 Google 账号无缝完成身份验证
集成 Google 登录 是 CoddyKit 上的免费 Firebase Auth & Realtime Database Apps 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Firebase Auth & Realtime Database Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to Google Sign-In
Google Sign-In is a secure way to let users log into your app using their Google accounts. It's fast, convenient, and users trust it.
This lesson will guide you through integrating Google Sign-In with Firebase Authentication, making your app more accessible.
Why Google Sign-In?
Using Google Sign-In offers several benefits:
- Ease of Use: Users don't need to create new passwords.
- Security: Leverages Google's robust authentication system.
- Trust: Users are familiar with the Google sign-in flow.
- Developer Simplicity: Firebase handles much of the complexity.
Integration Steps Overview
Integrating Google Sign-In with Firebase involves a few key steps:
- Enable Google as a provider in Firebase.
- Configure your app's Google Sign-In client.
- Handle the user's sign-in flow.
- Exchange the Google credential for a Firebase credential.
- Sign in the user to Firebase.
Enable Google in Firebase
First, you need to tell Firebase that you'll be using Google Sign-In.
Go to your Firebase Console, select your project, then navigate to Authentication > Sign-in method. Find 'Google' and enable it. Make sure to save your changes!
Web Client ID for Auth
Even for mobile apps, Firebase often uses a Web Client ID to securely authenticate with Google's backend services. This ID ensures that the Google token received by your app can be verified by Firebase.
After enabling Google Sign-In in the Firebase Console, Firebase automatically creates and links a Web Client ID for you. You'll typically find this in your project's `google-services.json` (Android) or `GoogleService-Info.plist` (iOS) file.
Configure Google Sign-In Client
In your application, you need to configure the Google Sign-In client. This setup includes requesting the user's ID token, which Firebase will use for authentication.
Here's a conceptual example of how you'd set up these options:
class GoogleSignInOptions {
boolean requestIdToken;
String webClientId;
GoogleSignInOptions(String clientId) {
this.webClientId = clientId;
}
GoogleSignInOptions requestIdToken(boolean value) {
this.requestIdToken = value;
return this;
}
String build() {
return "Options(ID_Token:" + requestIdToken + ", ClientID:" + webClientId + ")";
}
}
public class Main {
public static void main(String[] args) {
String WEB_CLIENT_ID = "YOUR_WEB_CLIENT_ID.apps.googleusercontent.com";
GoogleSignInOptions gso = new GoogleSignInOptions(WEB_CLIENT_ID)
.requestIdToken(true); // Request ID token for Firebase Auth
System.out.println("Google Sign-In options configured:");
System.out.println(gso.build());
}
}Initiate Sign-In Flow
Once the Google Sign-In client is configured, you'll typically have a "Sign in with Google" button. When the user taps this button, you'll launch the Google Sign-In intent to prompt the user to choose their Google account.
After the user selects an account, Google returns an authentication result, which includes the ID token we requested earlier.
Authenticate with Firebase
With the Google ID token in hand, the next step is to create a Firebase credential using this token and then sign in to Firebase Authentication.
Firebase handles the verification of the Google token and creates a new user or links to an existing one.
class GoogleAuthProvider {
static String getCredential(String idToken) {
return "GoogleCredential(" + idToken.substring(0, 10) + "...)";
}
}
class FirebaseAuth {
void signInWithCredential(String credential) {
System.out.println("Firebase Auth: Signing in with " + credential);
// Simulate success
System.out.println("User signed in successfully!");
}
}
public class Main {
public static void main(String[] args) {
// Imagine this ID token comes from Google Sign-In result
String googleIdToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
// 1. Create a Firebase credential from the Google ID token
String credential = GoogleAuthProvider.getCredential(googleIdToken);
// 2. Sign in to Firebase with the credential
FirebaseAuth auth = new FirebaseAuth();
auth.signInWithCredential(credential);
}
}Check Your Understanding
What is the primary piece of information obtained from Google Sign-In that Firebase uses to authenticate a user?
Google Sign-In Summary
You've learned how to integrate Google Sign-In with Firebase Authentication! This powerful combination provides a secure and user-friendly way for users to access your application.
Key takeaways:
- Enable Google as a provider in Firebase.
- Configure your app's Google Sign-In client to request an ID token.
- Use the obtained ID token to create a Firebase credential.
- Sign in to Firebase Auth with that credential.
Next, we'll explore other social login options like Facebook and GitHub!
常见问题解答
「集成 Google 登录」课时是免费的吗?
是的 — 「集成 Google 登录」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Firebase Auth & Realtime Database Apps 课程的其余内容,请升级到 CoddyKit PRO。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。
「集成 Google 登录」这节课中我会学到什么?
将 Google 登录集成到应用中,让用户能够使用 Google 账号无缝完成身份验证 你通过在浏览器中直接运行的动手代码来练习 Firebase Auth & Realtime Database Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Firebase Auth & Realtime Database Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Firebase Auth & Realtime Database Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「集成 Google 登录」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Firebase Auth & Realtime Database Apps 课中编写并运行代码吗?
能。每节 Firebase Auth & Realtime Database Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 集成 Google 登录
- Facebook 与 GitHub 身份验证
- 匿名与自定义身份验证
- 集成 Apple 登录