Facebook 登录设置
复制一个包含安装步骤和本插件的完整Markdown指南的设置提示。
简介
简介In this guide, you will learn how to setup Facebook Login with Capgo Social Login. You will need the following:
- Facebook开发者帐户
- 您的应用程序的包名/捆绑ID
- 访问终端以生成密钥哈希(Android)
创建Facebook应用
-
遵循教程
-
为您的应用程序添加 Facebook 登录
在您的 Facebook 开发者控制台中,添加 Facebook 登录产品到您的应用程序
-
在发布您的应用程序到公众之前,遵循以下 教程 发布它
重要信息
标题为“重要信息”以下是您需要的整合所需的关键信息的位置:
-
CLIENT_TOKEN:
-
APP_ID:
-
APP_NAME:
Android 配置
Android 设置-
在您的
AndroidManifest.xml确保以下行存在:
<uses-permission android:name="android.permission.INTERNET"/> -
生成您的 Android 密钥哈希
这是 Facebook 需要的关键安全步骤。打开您的终端并运行:
终端窗口 keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64 -A当提示输入密码时,请使用:
android -
将密钥哈希添加到您的Facebook应用
- 转到Facebook开发者平台上的您的应用的控制台
- 导航到设置 > 基本
- 滚动到“Android”部分
- 如果Android尚未添加,请点击“添加平台”,并填写详细信息
- 添加您生成的密钥哈希
- 对于生产环境,添加两种密钥哈希(debug和release)
-
更新您的
AndroidManifest.xml以包含:<application>...<activity android:name="com.facebook.FacebookActivity"android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"android:label="@string/app_name" /><activityandroid:name="com.facebook.CustomTabActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.BROWSABLE" /><data android:scheme="FB[APP_ID]" /></intent-filter></activity></application>
-
前往您的Facebook开发者应用程序的控制台
- 导航到设置 > 基本
- 滚动到页面底部并点击“添加平台”
- 选择iOS并填写所需详细信息
- __CAPGO_KEEP_0__
-
打开您的 Xcode 项目并导航到 Info.plist
-
在 Info.plist 中添加以下条目:
<key>FacebookAppID</key><string>[APP-ID]</string><key>FacebookClientToken</key><string>[CLIENT-TOKEN]</string><key>FacebookDisplayName</key><string>[APP-NAME]</string><key>LSApplicationQueriesSchemes</key><array><string>fbapi</string><string>fb-messenger-share-api</string></array><key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>fb[APP-ID]</string></array></dict></array> -
修改
AppDelegate.swiftimport FBSDKCoreKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch.// Initialize Facebook SDKFBSDKCoreKit.ApplicationDelegate.shared.application(application,didFinishLaunchingWithOptions: launchOptions)return true}func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {// Called when the app was launched with a url. Feel free to add additional processing here,// but if you want the App API to support tracking app url opens, make sure to keep this callif (FBSDKCoreKit.ApplicationDelegate.shared.application(app,open: url,sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,annotation: options[UIApplication.OpenURLOptionsKey.annotation])) {return true;} else {return ApplicationDelegateProxy.shared.application(app, open: url, options: options)}}}
在您的应用中使用 Facebook 登录
在应用中使用 Facebook 登录-
在应用中初始化 Facebook 登录
import { SocialLogin } from '@capgo/capacitor-social-login';// Initialize during app startupawait SocialLogin.initialize({facebook: {appId: 'APP_ID',clientToken: 'CLIENT_TOKEN',}}) -
实现登录功能
async function loginWithFacebook() {try {const result = await SocialLogin.login({provider: 'facebook',options: {permissions: ['email', 'public_profile'],limitedLogin: false // See Limited Login section below for important details}});console.log('Facebook login result:', result);// Handle successful login} catch (error) {console.error('Facebook login error:', error);// Handle error}} -
获取用户资料
登录成功后,您可以获取更多的资料信息:
async function getFacebookProfile() {try {const profileResponse = await SocialLogin.providerSpecificCall({call: 'facebook#getProfile',options: {fields: ['id', 'name', 'email', 'first_name', 'last_name', 'picture']}});console.log('Facebook profile:', profileResponse.profile);return profileResponse.profile;} catch (error) {console.error('Failed to get Facebook profile:', error);return null;}}// Example usage after loginasync function loginAndGetProfile() {const loginResult = await loginWithFacebook();if (loginResult) {const profile = await getFacebookProfile();if (profile) {console.log('User ID:', profile.id);console.log('Name:', profile.name);console.log('Email:', profile.email);console.log('Profile Picture:', profile.picture?.data?.url);}}}令牌类型限制:该
getProfile调用仅在您有令牌时才有效 访问令牌 (标准登录允许跟踪). 如果用户拒绝跟踪或您正在使用有限登录(仅限 JWT 令牌),则此调用将失败。 在这种情况下,请使用初始登录响应中提供的配置文件数据。
⚠️ Critical: 后端令牌处理
标题:⚠️ Critical: 后端令牌处理您的后端必须处理 两个不同的令牌类型 因为 iOS 用户可以根据 App Tracking Transparency 选择接收访问令牌或 JWT 令牌,而 Android 用户总是会接收访问令牌。
各个平台的令牌类型
平台类型| 平台 | 受限登录设置 | 用户ATT选择 | 结果令牌类型 |
|---|---|---|---|
| iOS | true | 任何 | JWT令牌 |
| iOS | false | 允许跟踪 | 访问令牌 |
| iOS | false | 拒绝跟踪 | __CAPGO_KEEP_0__ |
| Android | 任何 | N/A | __CAPGO_KEEP_0__ (始终) |
后端实现
后端实现-
检测令牌类型并处理
async function loginWithFacebook() {try {const loginResult = await SocialLogin.login({provider: 'facebook',options: {permissions: ['email', 'public_profile'],limitedLogin: false // iOS: depends on ATT, Android: ignored}});if (loginResult.accessToken) {// Access token (Android always, iOS when tracking allowed)return handleAccessToken(loginResult.accessToken.token);} else if (loginResult.idToken) {// JWT token (iOS only when tracking denied or limitedLogin: true)return handleJWTToken(loginResult.idToken);}} catch (error) {console.error('Facebook login error:', error);}} -
Firebase 集成示例
import { OAuthProvider, FacebookAuthProvider, signInWithCredential } from 'firebase/auth';async function handleAccessToken(accessToken: string, nonce: string) {// For access tokens, use OAuthProvider (new method)const fbOAuth = new OAuthProvider("facebook.com");const credential = fbOAuth.credential({idToken: accessToken,rawNonce: nonce});try {const userResponse = await signInWithCredential(auth, credential);return userResponse;} catch (error) {console.error('Firebase OAuth error:', error);return false;}}async function handleJWTToken(jwtToken: string) {// For JWT tokens, send to your backend for validationtry {const response = await fetch('/api/auth/facebook-jwt', {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({ jwtToken })});const result = await response.json();return result;} catch (error) {console.error('JWT validation error:', error);return false;}} -
后端 JWT 验证
// Backend: Validate JWT token from Facebookimport jwt from 'jsonwebtoken';import { Request, Response } from 'express';app.post('/api/auth/facebook-jwt', async (req: Request, res: Response) => {const { jwtToken } = req.body;try {// Verify JWT token with Facebook's public key// See: https://developers.facebook.com/docs/facebook-login/limited-login/token/validating/#standard-claimsconst decoded = jwt.verify(jwtToken, getFacebookPublicKey(), {algorithms: ['RS256'],audience: process.env.FACEBOOK_APP_ID,issuer: 'https://www.facebook.com' // From: https://www.facebook.com/.well-known/openid-configuration/?_rdr});// Extract user info from JWTconst userInfo = {id: decoded.sub,email: decoded.email,name: decoded.name,isJWTAuth: true};// Create your app's session/tokenconst sessionToken = createUserSession(userInfo);res.json({success: true,token: sessionToken,user: userInfo});} catch (error) {console.error('JWT validation failed:', error);res.status(401).json({ success: false, error: 'Invalid token' });}}); -
通用后端令牌处理器
// Handle both token types in your backendasync function authenticateFacebookUser(tokenData: any) {if (tokenData.accessToken) {// Handle access token - validate with Facebook Graph APIconst response = await fetch(`https://graph.facebook.com/me?access_token=${tokenData.accessToken}&fields=id,name,email`);const userInfo = await response.json();return {user: userInfo,tokenType: 'access_token',expiresIn: tokenData.expiresIn || 3600};} else if (tokenData.jwtToken) {// Handle JWT token - decode and validate// See: https://developers.facebook.com/docs/facebook-login/limited-login/token/validating/#standard-claimsconst decoded = jwt.verify(tokenData.jwtToken, getFacebookPublicKey());return {user: {id: decoded.sub,name: decoded.name,email: decoded.email},tokenType: 'jwt',expiresIn: decoded.exp - Math.floor(Date.now() / 1000)};} else {throw new Error('No valid token provided');}}
重要考虑因素
标题:重要考虑因素标准登录访问令牌:
- ✅ Android: iOS 限制不适用, 始终可用
- ✅ iOS: 只有当用户明确允许应用跟踪时
- ✅ 可用于访问 Facebook Graph API
- ✅ 可获得更长的过期时间
- ✅ 可获得更多用户数据
- ❌ 在 iOS 上变得越来越少见 因为用户越来越多地拒绝跟踪
JWT Token (仅限 iOS 私有模式):
- ❌ Android: 从未发生 (不受支持)
- ✅ iOS: 当跟踪被拒绝或
limitedLogin: true - ✅ 遵守 iOS 用户隐私偏好
- ❌ 只包含基本用户信息
- ❌ 过期时间更短
- ❌ 无法访问 Facebook Graph API
- ⚠️ 现在是 iOS 用户中最常见的场景
平台特定行为:
- iOS应用: 必须处理两种类型的令牌:访问令牌和 JWT 令牌
- Android应用: 只需处理访问令牌
- 跨平台应用: 必须实现两种令牌处理方法
Secure Context Requirements (Web/Capacitor)
Section titled “Secure Context Requirements (Web/Capacitor)”Crypto API Limitations
Section titled “Crypto API Limitations”The updated Facebook login flow requires the Web Crypto API for nonce generation, which is only available in secure contexts:
// This requires secure context (HTTPS or localhost)async function sha256(message: string) { const msgBuffer = new TextEncoder().encode(message); const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer); // ❌ Fails in insecure context // ...}Development Environment Issues
Section titled “Development Environment Issues”Common Problem: ionic serve with HTTP URLs breaks Facebook authentication
| 环境 | Crypto API 可用 | Facebook 登录已启用 |
|---|---|---|
http://localhost:3000 | ✅ 是 | ✅ 是 |
http://127.0.0.1:3000 | ✅ 是 | ✅ 是 |
http://192.168.1.100:3000 | ❌ 否 | ❌ 否 |
https://any-domain.com | ✅ 是 | ✅ 是 |
Solutions for Capacitor Development
解决方案Capacitor开发-
使用localhost进行web测试
终端窗口 # Instead of ionic serve --host=0.0.0.0ionic serve --host=localhost -
在Ionic中启用HTTPS
终端窗口 ionic serve --ssl -
在实际设备上测试
终端窗口 # Capacitor apps run in secure context on devicesionic cap run iosionic cap run android -
开发环境中的替代nonce生成
async function generateNonce() {if (typeof crypto !== 'undefined' && crypto.subtle) {// Secure context - use crypto.subtlereturn await sha256(Math.random().toString(36).substring(2, 10));} else {// Fallback for development (not secure for production)console.warn('Using fallback nonce - not secure for production');return btoa(Math.random().toString(36).substring(2, 10));}}
Firebase 集成注意事项
Firebase 集成注意事项最近的 Firebase 文档要求使用 JWT 令牌和非法令牌(nonce)进行 Facebook 验证,完全不受登录设置的影响。这一方法适用于两种情况 limitedLogin: true 和 limitedLogin: false:
// Both modes can return JWT tokens depending on user choice const loginResult = await SocialLogin.login({ provider: 'facebook', options: { permissions: ['email', 'public_profile'], limitedLogin: false, // true = always JWT, false = depends on user tracking choice nonce: nonce } });开发限制: 如果您在使用 ionic serve on a network IP (not localhost), Facebook login will fail due to crypto API restrictions. Use localhost or HTTPS for web testing.
故障排除
故障排除常见问题和解决方案
常见问题和解决方案-
Android 上的密钥哈希错误
- 请确认您已在 Facebook 控制台中添加了正确的密钥哈希
- 对于发布版本,请确保已添加了 debug 和 release 密钥哈希
- 生成哈希时,请确认您正在使用正确的 keystore
-
Facebook 登录按钮未显示
- 请确认所有清单条目都是正确的
- 请确认您的 Facebook App ID 和 Client Token 是正确的
- 请确认已正确初始化 SDK
-
常见iOS问题
- 确保所有Info.plist条目都是正确的
- 验证URL方案是否正确配置
- 检查您的包ID是否与Facebook控制台中注册的包ID匹配
测试
测试-
在Facebook开发者控制台中添加测试用户之前进行测试
- 转到角色>测试用户
- 创建测试用户
- 使用这些凭据进行测试
-
测试两种构建方式:debug和release
- 使用debug密钥哈希的debug构建
- 使用发布密钥哈希发布构建
- 在模拟器和物理设备上测试
请记住测试完整的登录流程,包括:
- 成功登录
- 取消登录
- 错误处理
- 注销功能