我收到带有此错误页面的 403 响应。我确信我的 API 令牌是正确的,因为我的其他功能运行得很好。
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>Access denied.</Message>
<Details>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Details>
<StringToSign>GET 1722666597 /</StringToSign>
</Error>
我根据 API 文档检查了我的标头。看起来不错。我的其他请求的其他标头,它们工作得很好。除了这个。
这是API文档
curl --请求 PUT
--url 'https://storage.googleapis.com/backend-fishes-storage-prod/hpleg1o4wez3e7mvy1sivhep9dw6?GoogleAccessId=services-storage-client%40ecstatic-baton-230905.iam.gserviceaccount.com&Expires=1656619087&Signature=aMeN%2FVW4I6LDg LRUEW0jzJqeA89gRpjn0TBcGMEnal3RU1iGqe0uOraHainV5qUw6tRPEBRBJ6rMeu9x0AmX6OC3Q8cCCmBUMs4k1jCQOnnCNgkttpU7ov% 2FeZ9WpPr47rTQSd5np7jCG3EWVEBhNeP25%2BTx5JlKQQ8UErP%2Bc46Lr%2Bj28wgLw%2BeeVjO4sVjEnLx3djoZD0Htei5XR0YQKVr%2FGDbS4iBOmjPsD5g4txKM6071zft %2BFK6U7I%2FfWjef2w4Nx%2BVvdATkNRpVEzbkAv1lBWMhfrOLy5koJfGepFk0BGQbTXNokhFCQYxzQuJBeC1xubKHzZSQLCVnTXt4EXA%3D%3D'
--header '内容处置:内联;文件名=“fishpic.jpg”;文件名*=UTF-8''''''fishpic.jpg'
--header 'Content-Md5: EA5w4bPQDfzBgEbes8ZmuQ=='
--header '内容类型:'
--data-binary @fishpic.jpg
这是我的标题
case .putPhoto(let model, _):
return [
"Content-Disposition" : model.directUpload.headers.contentDisposition,
"Content-Md5" : model.directUpload.headers.contentMD5,
"Content-Type" : ""
]
还有我的httpBody
switch self {
case .putPhoto(_, let image):
request.httpBody = image
}
有人可以帮助我吗?
好的,我发现问题了。当我发送请求时,网址正在更改。
func request() -> URLRequest {
var url: URL
switch self {
case .putPhoto(model: let model, _):
url = URL(string: model.directUpload.url)!
default:
guard var components = URLComponents(string: baseUrl) else {
fatalError("URL Error")
}
components.path = path
url = components.url!
}
现在,我正在检查 httpMethod 是否等于 .putPhoto 然后直接发送 url。这解决了我的问题。