2

I am trying to get blob from Microsoft Azure Blob Storage. But like everytime I am receiving same error.

System.HttpResponse[Status=Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature., StatusCode=403]

Here is my code .

public class AzurePOC {

    public void azurePOCMethod(){

        string storageKey = 'XJpVjJ41BslaysUMNP2NF8yJEQ0sAX6eD3aLW7XaSntp6J8zMAafVGe9U+g6NViBdLvW6vrjFahsE3uxuXsIGQ==';

        Datetime dt = Datetime.now();
        string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT';
        //string stringToSign = 'GET\n\napplication/xml\n\n\n' +'/testcontainer/2016';
        string stringToSign = 'GET\n\n\n\n0\n\n\n\n\n\n\n\nx-ms-date:'+formattedDate+'\nx-ms-version:2009-09-19\n/manishstore/testcontainer\ncomp:list\ninclude:metadata,snapshots,uncommittedblobs\nrestype:container';

        // Sign the request
        Blob temp = EncodingUtil.base64Decode(storageKey);
        Blob hmac = Crypto.generateMac('HMacSHA256', Blob.valueOf(stringToSign), temp);
        string signature = EncodingUtil.base64Encode(hmac);
       // signature = 'wRRRYV%2FKGwXhmZcY7cnFcUvwbopa%2BUD1Di2NLDjfK7Q%3D';
       EncodingUtil.urlEncode(stringToSign, 'UTF-8');

       // Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

        // This ends up being the exact same as the console app
        system.debug('SIGNATURE==>SharedKey myqueue:' + signature);
        system.debug('formattedDate:'+formattedDate);
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
       req.setHeader('content-type', 'application/xml');
        req.setHeader('x-ms-date', formattedDate);
        string authHeader = ':SharedKey manishstore:' + signature;
        req.setHeader('Authorization', authHeader);
        req.setHeader('x-ms-version', '2009-09-19');
        //req.setHeader('Content-Length', '0');

        req.setEndpoint('https://manishstore.blob.core.windows.net?restype=service&comp=properties');

       // req.setBody('<QueueMessage><MessageText>' + EncodingUtil.base64Encode(Blob.valueOf('This is a test from salesforce')) + '</MessageText></QueueMessage>');

        system.debug(req);

        Http http = new Http();

        try
        {
            HTTPResponse res = http.send(req);

            system.debug(res.toString());
            system.debug(res.getStatus());
            system.debug(res.getStatusCode());

        }
        catch (system.CalloutException ce)
        {
            system.debug(ce);
        }
}

}

Any kind of help on this will be helpful. Thanks in advance

1
  • Are you able to authorize via postman or curl for example? – kurunve Aug 23 '16 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.