+ Start a Discussion
Yogesh_Rankawat.ax397Yogesh_Rankawat.ax397 

How to create HIT using REST of Amazon Mechanical Turk Webservice

Hi

I am using following Apex class in Salesforce to create HIT using REST of Amazon Mechanical Turk Webservice :

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

Code:
public class DemoHIT {
  public DemoHIT(){
    
  }
 
  public string signature( string op, Datetime now, string securityKey) {
     String formattednow = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';           
     String canonical = 'AWSMechanicalTurkRequester'+op+formattednow;
     Blob bsig = Crypto.generateMac('HmacSHA1', Blob.valueOf(canonical), Blob.valueOf(securityKey));          
     return EncodingUtil.base64Encode(bsig);
  }
        
   
  public PageReference CreateHITFromREST(){
   try{
     
      string AWSAccessKeyId = '0946QXXMCPG7HW9FJTG2';
      string SecretAccessKey  = 'R/nrFePFroQL4uzgzLi7iZOKRSzCN1Sg7coZRUpl';
      string op = 'GetAccountBalance';
      DateTime Timestamp = system.now();
      String Signature = signature(op, Timestamp, SecretAccessKey);
      
      String formattednow = Timestamp.formatGmt('yyyy-MM-dd')+'T'+Timestamp.formatGmt('HH:mm:ss')+'.'+Timestamp.formatGMT('SSS')+'Z';           
        
      string ques = '<Question><QuestionIdentifier>my_question_id</QuestionIdentifier><DisplayName>My Question</DisplayName><IsRequired>true</IsRequired><QuestionContent><Title>The Next Move</Title></QuestionContent><AnswerSpecification><FreeTextAnswer><Constraints><IsNumeric minValue="100" maxValue="999"/><Length minLength="3" maxLength="3"/></Constraints></FreeTextAnswer></AnswerSpecification></Question>';
      ques = EncodingUtil.urlEncode(ques,'UTF-8');   
      Signature =  EncodingUtil.urlEncode(Signature,'UTF-8');    
      string url = 'http://mechanicalturk.sandbox.amazonaws.com/onca/soap—Service=AWSMechanicalTurkRequester&AWSAccessKeyId=' + AWSAccessKeyId + '&Version=2008-08-02&Operation=CreateHIT&Signature=' + Signature + '&Timestamp=' + formattednow + '&Title=Location%20and%20Photograph%20Identification&Description=Select%20the%20image%20that%20best%20represents&Reward.1.Amount=5&Reward.1.CurrencyCode=USD&Question=' + ques + '&AssignmentDurationInSeconds=30&LifetimeInSeconds=604800&Keywords=location,%20photograph,%20image,%20identification,%20opinion' ;     
       


      Http h = new Http();
      HttpRequest req = new HttpRequest();
      req.setEndpoint(url);
      req.setMethod('GET');

      HttpResponse res = h.send(req);
      system.debug('-------------body---');
      System.debug(res.getBody());
      system.debug('----------------');

     }
   catch(exception ex){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING,ex.getMessage()));
   }
   return null;
  }
    
}

 
---------------------------------------------------------------------------------------------------
But it gives following response in system log as "AWS.BadClaimsSupplied" :

<CreateHITResponse><OperationRequest><RequestId>e643f098-4b44-4121-aab7-988b12d23668</RequestId><Errors><Error><Code>AWS.BadClaimsSupplied</Code><Message>The specified claims are invalid.</Message></Error></Errors></OperationRequest></CreateHITResponse>


Can any body help me to fix this problem?

Thanks



Message Edited by Yogesh_Rankawat on 01-16-2009 04:21 PM
aalbertaalbert
That error message is coming from the Amazon Web Service itself.
I did a quick Google Search and foudn this link on the AWS website: http://developer.amazonwebservices.com/connect/thread.jspa?threadID=24728&tstart=0
Hopefully it helps.