首页 > 解决方案 > 使用 HTTP REST api 在颤振中使用 FIREBASE STORAGE 时出错

问题描述

将图像文件插入 Firebase Stroage 时出现错误。我正在使用 HTTP rest api 来做同样的事情。

代码如下:

Future<void> upload(Image image2) async {
var accountCredentials = ServiceAccountCredentials.fromJson({
 "private_key_id": " ",
 "private_key": " ",
 "client_email":"< >.iam.gserviceaccount.com",
 "client_id": "",
 "type": "service_account"
});

var scopes = [
 'https://www.googleapis.com/auth/cloud-platform',
];

 var client = Client();
 AccessCredentials credentials =
     await obtainAccessCredentialsViaServiceAccount(accountCredentials, scopes, client);
     String accessToken = credentials.accessToken.data;

var request = Request(
   'POST',
    Uri.parse(
      'https://www.googleapis.com/upload/storage/v1/b/mybucket/o?uploadType=media&name=$image2'),
    );
    request.headers['Authorization'] = "Bearer $accessToken";
    request.headers['Content-Type'] = "image/png";


Response response = await Response.fromStream(await request.send());
print("response is ");
print(response.statusCode);
print("response body");
print(response.body);
client.close();
}

错误如下:

 {
         "error": {
           "code": 403,
           "message": "< >.iam.gserviceaccount.com does not have storage.objects.create access  
                       to the Google Cloud Storage object.",
           "errors": [
             {
               "message": "< >.gserviceaccount.com does not have storage.objects.create access to the
                           Google Cloud Storage object.",
               "domain": "global",
               "reason": "forbidden"
             }
           ]
         }
       }

我已通过 IAM 服务管理授予 storage.objects.create 的权限,但这不起作用

单击此处查看分配权限的屏幕截图

标签: firebaseresthttpflutterfirebase-storage

解决方案


推荐阅读