首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Postman工作,但不在Flutter中工作:使用GCS预签名URL的API调用

通过Postman工作,但不在Flutter中工作:使用GCS预签名URL的API调用
EN

Stack Overflow用户
提问于 2021-08-03 08:13:00
回答 2查看 203关注 0票数 1

我正在尝试使用预先签名的url将视频文件上传到GCS。我已经设法通过谷歌创建了url,但现在我在使用它时遇到了问题。

在邮递员中上传作品,得到200响应。

postman bodypostman params

从邮递员复制的代码结果为403禁止(SignatureDoesNotMatch):

代码语言:javascript
复制
Future<http.StreamedResponse> uploadVideo(
      {required String uploadURL, required String filePath}) async {
    var headers = {'Content-Type': 'application/octet-stream'};
    var request = http.MultipartRequest('PUT', Uri.parse(uploadURL));
    request.files.add(await http.MultipartFile.fromPath('file', filePath));
    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
      print(await response.stream.bytesToString());
    } else {
      print(response.reasonPhrase);
    }
    return response;
  }

这是我从Google得到的错误:

代码语言:javascript
复制
<?xml version='1.0' encoding='UTF-8'?><Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message><StringToSign>GOOG4-RSA-SHA256
20210803T082850Z
20210803/auto/storage/goog4_request
6d513846a3db49f949b0d2eea8f04b90f918b3b94588c3ed55ed3620b7d7e1f6</StringToSign><CanonicalRequest>PUT
/phonedo-interviews/app-test/007/2.mp4
X-Goog-Algorithm=GOOG4-RSA-SHA256&amp;X-Goog-Credential=interviews%40interviews-317011.iam.gserviceaccount.com%2F20210803%2Fauto%2Fstorage%2Fgoog4_request&amp;X-Goog-Date=20210803T082850Z&amp;X-Goog-Expires=900&amp;X-Goog-SignedHeaders=content-type%3Bhost
content-type:multipart/form-data; boundary=dart-http-boundary-6w1yq6BQN3EkGBrhHZnwidOXZsBecsgSwTT3nBjB9vQCToHt0cg
host:storage.googleapis.com

content-type;host
UNSIGNED-PAYLOAD</CanonicalRequest></Error>

注意:我需要Content-Typeapplication/octet-stream,所以我在Postman的自动标头中禁用了该标头,并手动添加了Content-Type。当我没有这样做的时候,我也得到了403。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-08 13:29:59

解决方案是以二进制格式发送文件。

以下是工作代码:

代码语言:javascript
复制
  Future<http.Response> uploadVideo(
      {required String uploadURL, required String filePath}) async {
    var response = await http.put(
      Uri.parse(uploadURL),
      headers: {'content-type': 'application/octet-stream'},
      body: File(filePath).readAsBytesSync(),
    );
票数 1
EN

Stack Overflow用户

发布于 2021-08-03 08:31:01

在您的邮递员标头中,一个Token被提供给GCS (第一行)。考虑到您需要授权,Postman可能会将此令牌保存在应用程序方面的某个位置。

在这段flutter代码中,您给出的报头不包括Auth令牌,因此您将收到一个403错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68632515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档