locked
Create file Rest API throws 403 however GET works fine RRS feed

  • Question

  • Hi, I am trying to create file using rest api. but I keep on getting 403 . With the same way I am able to get file but in case of PUT I keep on getting

    Response message : Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
    Response code : 403

    I am creating string to sign like this in java 

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #4e9072} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #3933ff} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} span.s1 {color: #000000} span.s2 {color: #7e504f} span.s3 {color: #3933ff} span.s4 {color: #4e9072} span.Apple-tab-span {white-space:pre} </style>

    String stringToSign = "PUT\n" + "\n" // content encoding

    + "\n" // content language

    + "0" // content length

    + "\n" // content md5

    + "\n" // content type

    + "\n" // date

    + "\n" // if modified since

    + "\n" // if match

    + "\n" // if none match

    + "\n" // if unmodified since

    + "\n" // range

    + "x-ms-content-length:1024\n"

    + "x-ms-date:" + date + "\nx-ms-type: file\n"

    + "x-ms-version:2015-02-21\n" // headers

    + "/" + account  + request.getURL().getPath(); // resources

    and setting headers

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px} span.s1 {color: #7e504f} span.s2 {color: #3933ff} span.Apple-tab-span {white-space:pre} </style>

    request.setRequestMethod("PUT");

    request.setRequestProperty("Content-Length", "0");

    request.setRequestProperty("x-ms-content-length", "1024");

    request.setRequestProperty("x-ms-date", date);

    request.setRequestProperty("x-ms-type", "file");

    request.setRequestProperty("x-ms-version", "2015-02-21");

    request.setRequestProperty("Authorization", auth);

    Also creating auth as 

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #931a68} span.s1 {color: #931a68} span.s2 {color: #7e504f} span.s3 {text-decoration: underline} span.s4 {color: #3933ff} span.s5 {color: #0326cc} span.s6 {text-decoration: underline ; color: #3933ff} span.s7 {text-decoration: underline ; color: #0326cc} span.s8 {text-decoration: underline ; color: #7e504f} span.s9 {color: #000000} span.Apple-tab-span {white-space:pre} </style>

    private static String getAuthenticationString(String stringToSign) throws Exception {

    Mac mac = Mac.getInstance("HmacSHA256");

    mac.init(new SecretKeySpec(Base64.decode(key), "HmacSHA256"));

    String authKey = new String(Base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8"))));

    String auth = "SharedKey " + account + ":" + authKey;

    return auth;

    }

    Please help me resolving this error. 

    Monday, November 5, 2018 7:28 PM

All replies

  • Hello,

    Please make sure that the time settings are correct (timezone) or if the system clock isn't turned off on your local computer. 

    https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc738995(v=ws.10)

    If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click Here

    Friday, November 9, 2018 9:57 AM