Rav*_*mar 5 android firebase flutter firebase-storage firebase-app-check
Firebase 存储不断引发身份验证问题。即使我能够成功从 firebase auth 实例获取用户信息
通过使用
Miscellaneous.logMessage(
Tag, "currentUser ${FirebaseAuth.instance.currentUser}");
Run Code Online (Sandbox Code Playgroud)
应用程序检查当前应用程序中的设置
- 在 yaml 文件中添加依赖项
firebase_core: ^1.13.1
firebase_analytics: ^8.3.4
firebase_auth: ^3.3.11
cloud_firestore: ^2.5.4
firebase_crashlytics: ^2.5.3
firebase_messaging: ^10.0.9
firebase_database: ^7.2.2
firebase_storage: ^10.2.9
firebase_app_check: ^0.0.6+7
Run Code Online (Sandbox Code Playgroud)
-并在main.dart中添加了这段代码
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
);
}
Run Code Online (Sandbox Code Playgroud)
- Android 中的设置
摇篮
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: ['*mock*.jar'])
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.multidex:multidex:2.0.1'
implementation("androidx.browser:browser:1.4.0")
implementation platform('com.google.firebase:firebase-bom:28.2.1')
implementation 'com.google.android.material:material:1.5.0'
implementation 'com.google.android.gms:play-services-auth:20.1.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:18.0.1'
implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta04'
implementation 'com.google.firebase:firebase-appcheck-safetynet:16.0.0-beta04'
androidTestImplementation 'com.google.firebase:firebase-appcheck-debug-testing:16.0.0-beta04'
implementation 'androidx.work:work-runtime-ktx:2.7.1'
implementation 'com.google.android.play:core:1.10.3'
implementation 'org.jetbrains:annotations:20.1.0'
}
Run Code Online (Sandbox Code Playgroud)
主要活动
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseApp.initializeApp(this);
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance());
}
Run Code Online (Sandbox Code Playgroud)
错误
StorageUtil(15655):获取应用程序检查令牌时出错;使用占位符标记代替。错误:com.google.firebase.FirebaseException:从 API 返回错误。代码:403 正文:应用程序认证失败。[+3151 毫秒]
W/StorageUtil(15655):获取应用程序检查令牌时出错;使用占位符标记代替。错误:com.google.firebase.FirebaseException:尝试次数过多。
存储上传代码
firebaseStorageUtils.sendImageTOFirebase(imageFile, false).then((value) {
Miscellaneous.logMessage(Tag, "sendImageTOFirebase URL $value");
if (value == null) {
Miscellaneous.showMessage(context, "Server Error!!,Retry Later");
return;
}
setState(() {
Miscellaneous.logMessage(Tag, "Download URL $value");
profileUrl = value;
Miscellaneous.logMessage(Tag, imageFile.path);
});
});
Future<String?> sendImageTOFirebase(File imageSend, bool imgPost) async {
var len = await imageSend.length();
var fileSend;
Miscellaneous.logMessage(Tag, len);
if (len <= Constants.maxImageSize) {
fileSend = await compressFile(imageSend, 70);
} else {
var quality;
if (len >= Constants.maxImageSizeLarge) {
quality = 30;
} else {
quality = 50;
}
fileSend = await compressFile(imageSend, quality);
}
Miscellaneous.showLoader(context, this);
Miscellaneous.logMessage(Tag, fileSend.path);
if (imgPost) {
fileName = "${Miscellaneous.uniqueName()}.png";
drivePath = 'post/$userUUID/$fileName';
} else {
drivePath = 'user_profile/$userUUID/defaultProfile.png';
}
Miscellaneous.logMessage(Tag, "drivePath$drivePath");
try {
FirebaseAuth auth = FirebaseAuth.instance;
if (auth.currentUser != null) {
Miscellaneous.logMessage(Tag, auth.currentUser.toString());
}
// Create your custom metadata.
firebase_storage.SettableMetadata metadata =
firebase_storage.SettableMetadata(
cacheControl: 'max-age=60',
customMetadata: <String, String>{
'userId': userUUID,
},
);
await Firebase.initializeApp();
await firebase_storage.FirebaseStorage.instance
.ref(drivePath)
.putFile(fileSend, metadata);
String downloadUrlImage = await firebase_storage.FirebaseStorage.instance
.ref(drivePath)
.getDownloadURL();
this.downloadUrl = downloadUrlImage;
Miscellaneous.logMessage(Tag, "downloadURL $downloadUrlImage");
if (imgPost) {
Miscellaneous.dismissLoader(context);
return downloadUrlImage;
} else {
bool uploadToFirebaseImage = await uploadToFirebase(auth);
Miscellaneous.dismissLoader(context);
if (uploadToFirebaseImage) {
return downloadUrlImage;
} else {
return null;
}
}
} on firebase_core.FirebaseException catch (e) {
// e.g, e.code == 'canceled'
Miscellaneous.logMessage(Tag, "error $e");
Miscellaneous.dismissLoader(context);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
Firebase 规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
//if the file is less than 200kB
match /user_profile {
allow read, write:
if request.auth != null && request.resource.size <= 200 * 1024;
}
match /user_profile/{userId}/defaultProfile.png {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId;
}
match /post/{userId}/{fileName} {
allow read;
allow write: if request.auth.uid == userId && request.resource.size <= 200 * 1024;
allow delete: if request.auth.uid == userId;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 flutter 中使用 App Check 的正确方法是什么?
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |