开发者社区> 问答> 正文

SignatureDoesNotMatch:自己用JAVA写的请求头老是验证失败

我按照OSS API文档里面的说法,写了以下这段JAVA代码,总是遇到SignatureDoesNotMatch错误。求助!

                HttpClient client = new DefaultHttpClient();
                String uri = "http://storage.aliyun.com";
        HttpGet httpget = new HttpGet(uri);
        String time =  DateUtil.formatRfc822Date(new Date());


        String req = "GET\n"   time   "\n"   "/";
        SecretKeySpec signingKey = new SecretKeySpec(ACCESS_KEY.getBytes("utf-8"),
                "HmacSHA1");
        Mac mac;
        mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(req.getBytes("utf-8"));
        String auth = "OSS "   ACCESS_ID   ":"
                  new String(Base64.encodeBase64(rawHmac));
        httpget.addHeader("Authorization", auth);
        httpget.addHeader("Date", time);
        HttpResponse response = client.execute(httpget);
        HttpEntity en = response.getEntity();
        byte[] buffer = new byte[(int) en.getContentLength()];
        en.getContent().read(buffer);
        String html = new String(buffer, "utf-8");
        System.out.println(html);


错误如下:

<?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 key and signing method.</Message>
  <StringToSignBytes>47 45 54 0A 0A 0A 53 75 6E 2C 20 30 35 20 41 75 67 20 32 30 31 32 20 30 32 3A 32 38 3A 34 31 20 47 4D 54 0A 2F </StringToSignBytes>
  <SignatureProvided>9g /ufOa/QYlU3EHlfHWvc/q6sg=</SignatureProvided>
  <StringToSign>GET




Sun, 05 Aug 2012 02:28:41 GMT
/</StringToSign>
  <OSSAccessKeyId>aafet0g8vi9gh8fzl9vt3smj</OSSAccessKeyId>
  <RequestId>501DDA5A28EE62A4511B2CDC</RequestId>
  <HostId>storage.aliyun.com</HostId>
</Error>

展开
收起
zjyuan86 2012-08-05 10:39:19 14409 0
5 条回答
写回答
取消 提交回答
  • 技术问题,友情帮顶。
    2012-08-07 14:45:40
    赞同 展开评论 打赏
  • 阿里云容器服务管控调度系统开发
    ReSignatureDoesNotMatch:自己用JAVA写的请求头老是验证失败
      <StringToSign>GET




    Sun, 05 Aug 2012 02:28:41 GMT
    /</StringToSign>   你GET后边空行太多了吧
    2012-08-06 15:15:33
    赞同 展开评论 打赏
  • 楼主,您的问题我们已经反馈给相关人员了,有消息了会尽快回复您!
    2012-08-06 10:57:37
    赞同 展开评论 打赏
  • ReSignatureDoesNotMatch:自己用JAVA写的请求头老是验证失败
    可不可以开放java sdk的源代码?
    2012-08-05 11:05:48
    赞同 展开评论 打赏
  • http://storage.aliyun.com/aliyun_portal_storage/oss_api/aliyun_java_sdk_20120705.zip
    看下SDK吧,我不懂JAVA。

    -------------------------

    回 2楼(zjyuan86) 的帖子
    额,原来JAVA SDK并不是真正的SDK啊。。。
    2012-08-05 10:47:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载