+ Start a Discussion
raghav.Mraghav.M 

Amazon WebServices integration error

Hi,

 

I have executed the following code which makes a call to Amazon web services and I have receieved the following exception: 

 

<Response><Errors><Error><Code>InvalidParameterValue</Code><Message>Value (2011-8-30'T'6:53:6.495'Z') for parameter Timestamp is invalid. Must be in ISO8601 format.</Message></Error></Errors><RequestID>41aa0620-6f49-48cb-aead-bed311db49d1</RequestID></Response>

 

It will be great if someone helps to fix this problem and also posts AWS S3 REST sample code. 

 

Please replace 'AWS Secret Key' and 'AWS KEY' values before executing this code.

 

public with sharing class S3Rest {

public static void testAlexaWSForAmazon() {

       // The date format is yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 

       DateTime d = System.now();

       String timestamp = ''+ d.year() + '-' +d.month() + '-' +d.day() + '\'T\'' +d.hour() + ':' +d.minute() + ':' +d.second() + '.' +d.millisecond() + '\'Z\'';

       String timeFormat = d.formatGmt(timestamp);

       String urlEncodedTimestamp = EncodingUtil.urlEncode(timestamp, 'UTF-8');

       String action = 'UrlInfo';

       String inputStr = action + timeFormat;

       String algorithmName = 'HMacSHA1';

       Blob mac = Crypto.generateMac(algorithmName, Blob.valueOf(inputStr),Blob.valueOf('AWS Secrect key'));

       String macUrl = EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac), 'UTF-8');

       String urlToTest = 'amazon.com';

       String version = '2005-07-11';

       String endpoint = 'http://awis.amazonaws.com/';

       String accessKey = 'AWS KEY';

       HttpRequest req = new HttpRequest();

       req.setEndpoint(endpoint +'?AWSAccessKeyId=' + accessKey +'&Action=' +

                                 action +'&ResponseGroup=Rank&Version=' +

                                  version +'&Timestamp=' + urlEncodedTimestamp +

                                  '&Url=' + urlToTest +'&Signature=' + macUrl); 

        req.setMethod('GET');

        Http http = new Http();

       try {

             HttpResponse res =   http.send(req);

             System.debug('STATUS:'+res.getStatus());

             System.debug('STATUS_CODE:'+res.getStatusCode());

             System.debug('BODY: '+res.getBody());

        } catch(System.CalloutException e) {

              System.debug('ERROR: '+ e);

         }

   }

}

 

Thanks,

Rag

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

The first one looks more promissing, as it was parsed as a dateTime, and looks to be in the correct format, howver judging by the error, and by the time show, it appears as if you sending a local (pacific?) time but saying its GMT. You'll either need to convert to a gmt date/time first, or use the correct timezone offset for your local timezone.

All Answers

SuperfellSuperfell

You have quote's around the T and Z in your timestamp string, which is not correct, they should be removed.

raghav.Mraghav.M

Hi SimonF,

 

I have taken this example from Salesforce.com documents. 

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

Also I have tried the following date formats and non of them have worked. 

 

1. <Response><Errors><Error><Code>RequestExpired</Code><Message>Request has expired. Timestamp date is 2011-08-30T09:36:21.87Z</Message></Error></Errors><RequestID>09d8c57a-0f2f-56e4-5f1d-3e05b64d9b5d</RequestID></Response>

 

2. <Response><Errors><Error><Code>InvalidParameterValue</Code><Message>Value (2011-8-30T9:41:58.298Z) for parameter Timestamp is invalid. Must be in ISO8601 format.</Message></Error></Errors><RequestID>7d975b04-e433-58bd-58e8-fa614a5f926f</RequestID></Response>

 

Thanks,

Raghavendra

SuperfellSuperfell

The first one looks more promissing, as it was parsed as a dateTime, and looks to be in the correct format, howver judging by the error, and by the time show, it appears as if you sending a local (pacific?) time but saying its GMT. You'll either need to convert to a gmt date/time first, or use the correct timezone offset for your local timezone.

This was selected as the best answer
raghav.Mraghav.M

Hi SimonF,

 

 You are correct. I need to put the timestamp in GMT. Thank for your help. 

 

I am able  to fix the error.  

Problem code:

      DateTime d = System.now();     

String timestamp = ''+ d.year() + '-0' +   

   d.month() + '-' +      d.day() + '\'T\'0' +      d.hour() + ':' +   

  d.minute() + ':' +      d.second() + '.' +   

  d.millisecond() + '\'Z\'';
 String timeFormat = d.formatGmt(timestamp); //Failed to convert to GMT time

 

Correct Code: 

 DateTime d = System.now();   String timestamp = d.formatGmt('yyyy-MM-dd')+'T'+ d.formatGmt('HH:mm:ss')+'.'+ d.formatGMT('SSS')+'Z';

 

 

Thanks,

Raghavendra

 

raghav.Mraghav.M

Hi SimonF,

 

I am unable to make HTTP request to Amazon S3. I am finding it little hard time to consturct HTTP request for Amazon S3. 

Cloud you provide some sample code.

 

 

Thanks,

Raghavendra

AmzonIntegrator9AmzonIntegrator9
Hi Simon , do you have Amazon Dynamo DB Rest samples to Put Item to table,if so please post them.

Thanks.
RajusRajus

Hi simon/Raghava,

 

I am also facing this type of issue in my code.I am getting the status code 403 Forbidden error.

 

Following is my request.

string dateString = Datetime.now().formatGmt('EEE, dd MMM yyyy HH:mm:ss Z');
string stringToSign = 'GET \n'+'\n'+'\n '+dateString;
Blob mac = Crypto.generateMac('hmacSHA1',Blob.valueOf(stringToSign), Blob.valueOf('xxxxxxxx'));
stringToSign = EncodingUtil.base64Encode(mac);
httpRequest con = new HttpRequest();
con.setHeader('Authorization','AWS xxxxx:'+stringToSign);
con.setEndPoint('https://s3.amazonaws.com');
con.setHeader('Host','xxxxx.s3.amazonaws.com');
con.setHeader('Date',dateString);
con.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(con);

 

Can any one please help me out to solve this issue.

 

Thanks,

Rajesh.