I'm getting this error when I try to upload an image.
"error": {
"code": 401,
"message": "Invalid Credentials",
"errors": [
{
"message": "Invalid Credentials",
"domain": "global",
"reason": "authError",
"locationType": "header",
"location": "Authorization"
}
]
}
}
I tried passing fileBytes
to the data
but it doesn't work. Firebase Storage doesn't work for flutter desktop that's why I'm using the rest api.
Future<void> uploadProfilePicture({
required String fileName,
required String filePath,
required String email,
}) async {
return Chain.capture(() async {
final url =
"https://storage.googleapis.com/upload/storage/v1/b/${Constants.projectID}.appspot.com/o?uploadType=media&name=$email/profilePicture/$fileName";
// final fileBytes = await File(filePath).readAsBytes();
final token = await FirebaseAuth.instance.tokenProvider.idToken;
try {
final response = await _dio.post<Map<String, dynamic>>(
url,
data: filePath,
options: Options(
headers: {
"Content-Type": "image/jpeg",
"Authorization": "Bearer $token,",
},
),
);
logger.i(response.data);
} on DioException catch (e, stackTrace) {
final terseStacktrace = Chain.forTrace(stackTrace).terse;
logger
..e("Failed to upload profile picture", [e, terseStacktrace])
..e("Error ${e.error} Message ${e.message} Response ${e.response}");
} catch (e, stackTrace) {
final terseStacktrace = Chain.forTrace(stackTrace).terse;
logger.wtf("Failed to upload profile picture", [e, terseStacktrace]);
rethrow;
}
});
}
The above is based on this:
curl -X POST --data-binary @[OBJECT_LOCATION] \
-H "Authorization: Bearer [OAUTH2_TOKEN]" \
-H "Content-Type: [OBJECT_CONTENT_TYPE]" \
"https://storage.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=[OBJECT_NAME]"
These are my Storage rules
service firebase.storage {
match /b/{bucket}/o {
allow read, write: if true
}
}