├── Java ├── ApproveAssignmentsSample.java ├── CreateHITSample.java ├── GetAccountBalanceSample.java └── my_question.xml ├── Javascript ├── RetrieveAndApproveResults.js ├── SubmitTask.js ├── config.json └── my_question.xml ├── LICENSE ├── Python ├── CreateHitSample.py ├── RetrieveAndApproveHitSample.py └── my_question.xml ├── README.md └── Ruby ├── mturk_sample_part_one_create_hit.rb └── mturk_sample_part_two_approve_and_delete_hit.rb /Java/ApproveAssignmentsSample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance 5 | * with the License. A copy of the License is located at 6 | * 7 | * http://aws.amazon.com/apache2.0/ 8 | * 9 | * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 10 | * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions 11 | * and limitations under the License. 12 | */ 13 | import java.io.IOException; 14 | import java.util.Collections; 15 | import java.util.List; 16 | 17 | import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; 18 | import com.amazonaws.services.mturk.AmazonMTurk; 19 | import com.amazonaws.services.mturk.AmazonMTurkClientBuilder; 20 | import com.amazonaws.services.mturk.model.ApproveAssignmentRequest; 21 | import com.amazonaws.services.mturk.model.Assignment; 22 | import com.amazonaws.services.mturk.model.AssignmentStatus; 23 | import com.amazonaws.services.mturk.model.GetHITRequest; 24 | import com.amazonaws.services.mturk.model.GetHITResult; 25 | import com.amazonaws.services.mturk.model.ListAssignmentsForHITRequest; 26 | import com.amazonaws.services.mturk.model.ListAssignmentsForHITResult; 27 | 28 | /* 29 | * Before connecting to MTurk, set up your AWS account and IAM settings as described here: 30 | * https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 31 | * 32 | * Configure your AWS credentials as described here: 33 | * http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html 34 | * 35 | */ 36 | 37 | public class ApproveAssignmentsSample { 38 | 39 | // TODO Change this to your HIT ID - see CreateHITSample.java for generating a HIT 40 | private static final String HIT_ID_TO_APPROVE = "HIT_ID_FROM_HIT_CREATION"; 41 | 42 | private static final String SANDBOX_ENDPOINT = "mturk-requester-sandbox.us-east-1.amazonaws.com"; 43 | private static final String SIGNING_REGION = "us-east-1"; 44 | 45 | public static void main(final String[] argv) throws IOException { 46 | final ApproveAssignmentsSample sandboxApp = new ApproveAssignmentsSample(getSandboxClient()); 47 | sandboxApp.approveAssignment(HIT_ID_TO_APPROVE); 48 | } 49 | 50 | private final AmazonMTurk client; 51 | 52 | private ApproveAssignmentsSample(final AmazonMTurk client) { 53 | this.client = client; 54 | } 55 | 56 | /* 57 | Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence Tasks (HITs) without paying any money. 58 | Make sure to sign up for a Sanbox account at https://requestersandbox.mturk.com/ with the same credentials as your main MTurk account. 59 | */ 60 | private static AmazonMTurk getSandboxClient() { 61 | AmazonMTurkClientBuilder builder = AmazonMTurkClientBuilder.standard(); 62 | builder.setEndpointConfiguration(new EndpointConfiguration(SANDBOX_ENDPOINT, SIGNING_REGION)); 63 | return builder.build(); 64 | } 65 | 66 | private void approveAssignment(final String hitId) { 67 | GetHITRequest getHITRequest = new GetHITRequest(); 68 | getHITRequest.setHITId(hitId); 69 | GetHITResult getHITResult = client.getHIT(getHITRequest); 70 | System.out.println("HIT " + hitId + " status: " + getHITResult.getHIT().getHITStatus()); 71 | 72 | ListAssignmentsForHITRequest listHITRequest = new ListAssignmentsForHITRequest(); 73 | listHITRequest.setHITId(hitId); 74 | listHITRequest.setAssignmentStatuses(Collections.singletonList(AssignmentStatus.Submitted.name())); 75 | 76 | // Get a maximum of 10 completed assignments for this HIT 77 | listHITRequest.setMaxResults(10); 78 | ListAssignmentsForHITResult listHITResult = client.listAssignmentsForHIT(listHITRequest); 79 | List assignmentList = listHITResult.getAssignments(); 80 | System.out.println("The number of submitted assignments is " + assignmentList.size()); 81 | 82 | // Iterate through all the assignments received 83 | for (Assignment asn : assignmentList) { 84 | System.out.println("The worker with ID " + asn.getWorkerId() + " submitted assignment " 85 | + asn.getAssignmentId() + " and gave the answer " + asn.getAnswer()); 86 | 87 | // Approve the assignment 88 | ApproveAssignmentRequest approveRequest = new ApproveAssignmentRequest(); 89 | approveRequest.setAssignmentId(asn.getAssignmentId()); 90 | approveRequest.setRequesterFeedback("Good work, thank you!"); 91 | approveRequest.setOverrideRejection(false); 92 | client.approveAssignment(approveRequest); 93 | System.out.println("Assignment has been approved: " + asn.getAssignmentId()); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Java/CreateHITSample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance 5 | * with the License. A copy of the License is located at 6 | * 7 | * http://aws.amazon.com/apache2.0/ 8 | * 9 | * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 10 | * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions 11 | * and limitations under the License. 12 | */ 13 | import java.io.IOException; 14 | import java.nio.file.Files; 15 | import java.nio.file.Paths; 16 | import java.util.ArrayList; 17 | import java.util.Collections; 18 | import java.util.List; 19 | 20 | import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; 21 | import com.amazonaws.services.mturk.AmazonMTurk; 22 | import com.amazonaws.services.mturk.AmazonMTurkClientBuilder; 23 | import com.amazonaws.services.mturk.model.Comparator; 24 | import com.amazonaws.services.mturk.model.CreateHITRequest; 25 | import com.amazonaws.services.mturk.model.CreateHITResult; 26 | import com.amazonaws.services.mturk.model.Locale; 27 | import com.amazonaws.services.mturk.model.QualificationRequirement; 28 | 29 | /* 30 | * Before connecting to MTurk, set up your AWS account and IAM settings as described here: 31 | * https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 32 | * 33 | * Configure your AWS credentials as described here: 34 | * http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html 35 | * 36 | */ 37 | 38 | public class CreateHITSample { 39 | 40 | private static final String QUESTION_XML_FILE_NAME = "my_question.xml"; 41 | 42 | private static final String SANDBOX_ENDPOINT = "mturk-requester-sandbox.us-east-1.amazonaws.com"; 43 | private static final String PROD_ENDPOINT = "https://mturk-requester.us-east-1.amazonaws.com"; 44 | private static final String SIGNING_REGION = "us-east-1"; 45 | 46 | public static void main(final String[] argv) throws IOException { 47 | /* 48 | Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence Tasks (HITs) without paying any money. 49 | Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as your main MTurk account 50 | 51 | Switch to getProdClient() in production. 52 | Uncomment line 60, 61, & 66 below to create your HIT in production. 53 | 54 | */ 55 | 56 | final CreateHITSample sandboxApp = new CreateHITSample(getSandboxClient()); 57 | final HITInfo hitInfo = sandboxApp.createHIT(QUESTION_XML_FILE_NAME); 58 | 59 | // final CreateHITSample prodApp = new CreateHITSample(getProdClient()); 60 | // final HITInfo hitInfo = prodApp.createHIT(QUESTION_XML_FILE_NAME); 61 | 62 | System.out.println("Your HIT has been created. You can see it at this link:"); 63 | 64 | System.out.println("https://workersandbox.mturk.com/mturk/preview?groupId=" + hitInfo.getHITTypeId()); 65 | // System.out.println("https://www.mturk.com/mturk/preview?groupId=" + hitInfo.getHITTypeId()); 66 | 67 | System.out.println("Your HIT ID is: " + hitInfo.getHITId()); 68 | } 69 | 70 | private final AmazonMTurk client; 71 | 72 | private CreateHITSample(final AmazonMTurk client) { 73 | this.client = client; 74 | } 75 | 76 | private static AmazonMTurk getSandboxClient() { 77 | AmazonMTurkClientBuilder builder = AmazonMTurkClientBuilder.standard(); 78 | builder.setEndpointConfiguration(new EndpointConfiguration(SANDBOX_ENDPOINT, SIGNING_REGION)); 79 | return builder.build(); 80 | } 81 | 82 | private static AmazonMTurk getProdClient() { 83 | AmazonMTurkClientBuilder builder = AmazonMTurkClientBuilder.standard(); 84 | builder.setEndpointConfiguration(new EndpointConfiguration(PROD_ENDPOINT, SIGNING_REGION)); 85 | return builder.build(); 86 | } 87 | 88 | private static final class HITInfo { 89 | private final String hitId; 90 | private final String hitTypeId; 91 | 92 | private HITInfo(final String hitId, final String hitTypeId) { 93 | this.hitId = hitId; 94 | this.hitTypeId = hitTypeId; 95 | } 96 | 97 | private String getHITId() { 98 | return this.hitId; 99 | } 100 | 101 | private String getHITTypeId() { 102 | return this.hitTypeId; 103 | } 104 | } 105 | 106 | private HITInfo createHIT(final String questionXmlFile) throws IOException { 107 | 108 | // QualificationRequirement: Locale IN (US, CA) 109 | QualificationRequirement localeRequirement = new QualificationRequirement(); 110 | localeRequirement.setQualificationTypeId("00000000000000000071"); 111 | localeRequirement.setComparator(Comparator.In); 112 | List localeValues = new ArrayList<>(); 113 | localeValues.add(new Locale().withCountry("US")); 114 | localeValues.add(new Locale().withCountry("CA")); 115 | localeRequirement.setLocaleValues(localeValues); 116 | localeRequirement.setRequiredToPreview(true); 117 | 118 | // Read the question XML into a String 119 | String questionSample = new String(Files.readAllBytes(Paths.get(questionXmlFile))); 120 | 121 | CreateHITRequest request = new CreateHITRequest(); 122 | request.setMaxAssignments(10); 123 | request.setLifetimeInSeconds(600L); 124 | request.setAssignmentDurationInSeconds(600L); 125 | // Reward is a USD dollar amount - USD$0.20 in the example below 126 | request.setReward("0.20"); 127 | request.setTitle("Answer a simple question"); 128 | request.setKeywords("question, answer, research"); 129 | request.setDescription("Answer a simple question"); 130 | request.setQuestion(questionSample); 131 | request.setQualificationRequirements(Collections.singletonList(localeRequirement)); 132 | 133 | CreateHITResult result = client.createHIT(request); 134 | return new HITInfo(result.getHIT().getHITId(), result.getHIT().getHITTypeId()); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /Java/GetAccountBalanceSample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance 5 | * with the License. A copy of the License is located at 6 | * 7 | * http://aws.amazon.com/apache2.0/ 8 | * 9 | * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 10 | * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions 11 | * and limitations under the License. 12 | */ 13 | import java.io.IOException; 14 | import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; 15 | import com.amazonaws.services.mturk.AmazonMTurk; 16 | import com.amazonaws.services.mturk.AmazonMTurkClientBuilder; 17 | import com.amazonaws.services.mturk.model.GetAccountBalanceRequest; 18 | import com.amazonaws.services.mturk.model.GetAccountBalanceResult; 19 | 20 | /* 21 | * Before connecting to MTurk, set up your AWS account and IAM settings as described here: 22 | * https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 23 | * 24 | * Configure your AWS credentials as described here: 25 | * http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html 26 | * 27 | */ 28 | 29 | public class GetAccountBalanceSample { 30 | 31 | private static final String SANDBOX_ENDPOINT = "mturk-requester-sandbox.us-east-1.amazonaws.com"; 32 | private static final String PRODUCTION_ENDPOINT = "mturk-requester.us-east-1.amazonaws.com"; 33 | private static final String SIGNING_REGION = "us-east-1"; 34 | 35 | public static void main(final String[] argv) throws IOException { 36 | /* 37 | Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence Tasks (HITs) without paying any money. 38 | Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as your main MTurk account. 39 | */ 40 | final GetAccountBalanceSample sandboxApp = new GetAccountBalanceSample(getSandboxClient()); 41 | final String sandboxBalance = sandboxApp.getAccountBalance(); 42 | 43 | // In Sandbox this will always return $10,000 44 | System.out.println("SANDBOX - Your account balance is " + sandboxBalance); 45 | 46 | // Connect to the live marketplace and get your real account balance 47 | final GetAccountBalanceSample productionApp = new GetAccountBalanceSample(getProductionClient()); 48 | final String productionBalance = productionApp.getAccountBalance(); 49 | System.out.println("PRODUCTION - Your account balance is " + productionBalance); 50 | } 51 | 52 | private final AmazonMTurk client; 53 | 54 | private GetAccountBalanceSample(final AmazonMTurk client) { 55 | this.client = client; 56 | } 57 | 58 | private static AmazonMTurk getProductionClient() { 59 | AmazonMTurkClientBuilder builder = AmazonMTurkClientBuilder.standard(); 60 | builder.setEndpointConfiguration(new EndpointConfiguration(PRODUCTION_ENDPOINT, SIGNING_REGION)); 61 | return builder.build(); 62 | } 63 | 64 | private static AmazonMTurk getSandboxClient() { 65 | AmazonMTurkClientBuilder builder = AmazonMTurkClientBuilder.standard(); 66 | builder.setEndpointConfiguration(new EndpointConfiguration(SANDBOX_ENDPOINT, SIGNING_REGION)); 67 | return builder.build(); 68 | } 69 | 70 | private String getAccountBalance() { 71 | GetAccountBalanceRequest getBalanceRequest = new GetAccountBalanceRequest(); 72 | GetAccountBalanceResult result = client.getAccountBalance(getBalanceRequest); 73 | return result.getAvailableBalance(); 74 | } 75 | } -------------------------------------------------------------------------------- /Java/my_question.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 |

This is a test question

13 |

14 |

15 | 16 | 17 | 18 | ]]> 19 |
20 | 450 21 |
22 | -------------------------------------------------------------------------------- /Javascript/RetrieveAndApproveResults.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance 5 | * with the License. A copy of the License is located at 6 | * 7 | * http://aws.amazon.com/apache2.0/ 8 | * 9 | * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 10 | * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions 11 | * and limitations under the License. 12 | */ 13 | 14 | var util = require('util'); 15 | var AWS = require('aws-sdk'); 16 | 17 | /** 18 | * Before connecting to MTurk, set up your AWS account and IAM settings as described here: 19 | * https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 20 | * 21 | * Follow AWS best practices for setting credentials from here: 22 | * http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html 23 | */ 24 | AWS.config.loadFromPath('./config.json'); 25 | 26 | /** 27 | * Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence Tasks (HITs) without paying any 28 | * money. Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as 29 | * your main MTurk account. 30 | */ 31 | 32 | // Add in the HITId below. See SubmitTask.js for generating a HIT 33 | var myHITId = 'YOUR_HIT_ID'; 34 | 35 | var endpoint = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'; 36 | // Uncomment this line to use in production 37 | // endpoint = 'https://mturk-requester.us-east-1.amazonaws.com'; 38 | 39 | // Connect to sandbox 40 | var mturk = new AWS.MTurk({ endpoint: endpoint }); 41 | 42 | /** 43 | * To keep this example simple, we are assuming that there are fewer 44 | * than 10 results and there is no need to iterate through pages of results 45 | */ 46 | 47 | mturk.listAssignmentsForHIT({HITId: myHITId}, function (err, assignmentsForHIT) { 48 | if (err) { 49 | console.log(err.message); 50 | } else { 51 | console.log('Completed Assignments found: ' + assignmentsForHIT.NumResults); 52 | for (var i = 0; i < assignmentsForHIT.NumResults; i++) { 53 | console.log('Answer from Worker with ID - ' + assignmentsForHIT.Assignments[i].WorkerId + ': ', assignmentsForHIT.Assignments[i].Answer); 54 | 55 | // Approve the work so the Worker is paid with and optional feedback message 56 | mturk.approveAssignment({ 57 | AssignmentId: assignmentsForHIT.Assignments[i].AssignmentId, 58 | RequesterFeedback: 'Thanks for the great work!', 59 | }, function (err) { 60 | if (err) { 61 | console.log(err, err.stack); 62 | } 63 | }); 64 | } 65 | } 66 | }); 67 | -------------------------------------------------------------------------------- /Javascript/SubmitTask.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance 5 | * with the License. A copy of the License is located at 6 | * 7 | * http://aws.amazon.com/apache2.0/ 8 | * 9 | * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 10 | * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions 11 | * and limitations under the License. 12 | */ 13 | 14 | var util = require('util'); 15 | var AWS = require('aws-sdk'); 16 | 17 | /** 18 | * Before connecting to MTurk, set up your AWS account and IAM settings as described here: 19 | * https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 20 | * 21 | * Follow AWS best practices for setting credentials from here: 22 | * http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html 23 | */ 24 | AWS.config.loadFromPath('./config.json'); 25 | 26 | fs = require('fs'); 27 | 28 | /** 29 | * Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence Tasks (HITs) without paying any 30 | * money. Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as 31 | * your main MTurk account. 32 | */ 33 | 34 | var endpoint = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'; 35 | // Uncomment this line to use in production 36 | // endpoint = 'https://mturk-requester.us-east-1.amazonaws.com'; 37 | 38 | // Connect to sandbox 39 | var mturk = new AWS.MTurk({ endpoint: endpoint }); 40 | 41 | // Test your ability to connect to MTurk by checking your account balance 42 | mturk.getAccountBalance(function (err, data) { 43 | if (err) { 44 | console.log(err.message); 45 | } else { 46 | // Sandbox balance check will always return $10,000 47 | console.log('I have ' + data.AvailableBalance + ' in my account.'); 48 | } 49 | }); 50 | 51 | /* 52 | Publish a new HIT to the Sandbox marketplace start by reading in the HTML markup specifying your task from a seperate file (my_question.xml). To learn more about the HTML question type, see here: http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkAPI/ApiReference_HTMLQuestionArticle.html 53 | */ 54 | 55 | fs.readFile('my_question.xml', 'utf8', function (err, myQuestion) { 56 | if (err) { 57 | return console.log(err); 58 | } 59 | 60 | // Construct the HIT object below 61 | var myHIT = { 62 | Title: 'This is a new test question', 63 | Description: 'Another description', 64 | MaxAssignments: 1, 65 | LifetimeInSeconds: 3600, 66 | AssignmentDurationInSeconds: 600, 67 | Reward: '0.20', 68 | Question: myQuestion, 69 | 70 | // Add a qualification requirement that the Worker must be either in Canada or the US 71 | QualificationRequirements: [ 72 | { 73 | QualificationTypeId: '00000000000000000071', 74 | Comparator: 'In', 75 | LocaleValues: [ 76 | { Country: 'US' }, 77 | { Country: 'CA' }, 78 | ], 79 | }, 80 | ], 81 | }; 82 | 83 | // Publish the object created above 84 | mturk.createHIT(myHIT, function (err, data) { 85 | if (err) { 86 | console.log(err.message); 87 | } else { 88 | console.log(data); 89 | // Save the HITId printed by data.HIT.HITId and use it in the RetrieveAndApproveResults.js code sample 90 | console.log('HIT has been successfully published here: https://workersandbox.mturk.com/mturk/preview?groupId=' + data.HIT.HITTypeId + ' with this HITId: ' + data.HIT.HITId); 91 | } 92 | }) 93 | }); 94 | -------------------------------------------------------------------------------- /Javascript/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "accessKeyId": "YOUR_ACCESS_ID", 3 | "secretAccessKey": "YOUR_SECRET_ACCESS_KEY", 4 | "region": "us-east-1" 5 | } -------------------------------------------------------------------------------- /Javascript/my_question.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 |

This is a test question

13 |

14 |

15 | 16 | 17 | 18 | ]]> 19 |
20 | 450 21 |
22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Python/CreateHitSample.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Amazon.com, Inc. or its affiliates 2 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import sys 16 | import boto3 17 | 18 | # Before connecting to MTurk, set up your AWS account and IAM settings as 19 | # described here: 20 | # https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 21 | # 22 | # Follow AWS best practices for setting up credentials here: 23 | # http://boto3.readthedocs.io/en/latest/guide/configuration.html 24 | 25 | # Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence 26 | # Tasks (HITs) without paying any money. Sign up for a Sandbox account at 27 | # https://requestersandbox.mturk.com/ with the same credentials as your main 28 | # MTurk account. 29 | 30 | # By default, HITs are created in the free-to-use Sandbox 31 | create_hits_in_live = False 32 | 33 | environments = { 34 | "live": { 35 | "endpoint": "https://mturk-requester.us-east-1.amazonaws.com", 36 | "preview": "https://www.mturk.com/mturk/preview", 37 | "manage": "https://requester.mturk.com/mturk/manageHITs", 38 | "reward": "0.00" 39 | }, 40 | "sandbox": { 41 | "endpoint": "https://mturk-requester-sandbox.us-east-1.amazonaws.com", 42 | "preview": "https://workersandbox.mturk.com/mturk/preview", 43 | "manage": "https://requestersandbox.mturk.com/mturk/manageHITs", 44 | "reward": "0.11" 45 | }, 46 | } 47 | mturk_environment = environments["live"] if create_hits_in_live else environments["sandbox"] 48 | 49 | # use profile if one was passed as an arg, otherwise 50 | profile_name = sys.argv[1] if len(sys.argv) >= 2 else None 51 | session = boto3.Session(profile_name=profile_name) 52 | client = session.client( 53 | service_name='mturk', 54 | region_name='us-east-1', 55 | endpoint_url=mturk_environment['endpoint'], 56 | ) 57 | 58 | # Test that you can connect to the API by checking your account balance 59 | user_balance = client.get_account_balance() 60 | 61 | # In Sandbox this always returns $10,000. In live, it will be your acutal balance. 62 | print "Your account balance is {}".format(user_balance['AvailableBalance']) 63 | 64 | # The question we ask the workers is contained in this file. 65 | question_sample = open("my_question.xml", "r").read() 66 | 67 | # Example of using qualification to restrict responses to Workers who have had 68 | # at least 80% of their assignments approved. See: 69 | # http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkAPI/ApiReference_QualificationRequirementDataStructureArticle.html#ApiReference_QualificationType-IDs 70 | worker_requirements = [{ 71 | 'QualificationTypeId': '000000000000000000L0', 72 | 'Comparator': 'GreaterThanOrEqualTo', 73 | 'IntegerValues': [80], 74 | 'RequiredToPreview': True, 75 | }] 76 | 77 | # Create the HIT 78 | response = client.create_hit( 79 | MaxAssignments=3, 80 | LifetimeInSeconds=600, 81 | AssignmentDurationInSeconds=600, 82 | Reward=mturk_environment['reward'], 83 | Title='Answer a simple question', 84 | Keywords='question, answer, research', 85 | Description='Answer a simple question. Created from mturk-code-samples.', 86 | Question=question_sample, 87 | QualificationRequirements=worker_requirements, 88 | ) 89 | 90 | # The response included several fields that will be helpful later 91 | hit_type_id = response['HIT']['HITTypeId'] 92 | hit_id = response['HIT']['HITId'] 93 | print "\nCreated HIT: {}".format(hit_id) 94 | 95 | print "\nYou can work the HIT here:" 96 | print mturk_environment['preview'] + "?groupId={}".format(hit_type_id) 97 | 98 | print "\nAnd see results here:" 99 | print mturk_environment['manage'] 100 | -------------------------------------------------------------------------------- /Python/RetrieveAndApproveHitSample.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Amazon.com, Inc. or its affiliates 2 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import sys 16 | import boto3 17 | from xml.dom.minidom import parseString 18 | 19 | # Before connecting to MTurk, set up your AWS account and IAM settings as described here: 20 | # https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 21 | # 22 | # Follow AWS best practices for setting up credentials here: 23 | # http://boto3.readthedocs.io/en/latest/guide/configuration.html 24 | 25 | # Use the Amazon Mechanical Turk Sandbox to publish test Human Intelligence Tasks (HITs) without paying any money. 26 | # Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as your main MTurk account. 27 | 28 | # This HIT id should be the HIT you just created - see the CreateHITSample.py file for generating a HIT 29 | if(len(sys.argv) < 2): 30 | print("You must pass a HIT id as the first argument") 31 | sys.exit(-1) 32 | 33 | hit_id = sys.argv[1] 34 | 35 | # By default, we use the free-to-use Sandbox 36 | create_hits_in_live = False 37 | 38 | environments = { 39 | "live": { 40 | "endpoint": "https://mturk-requester.us-east-1.amazonaws.com", 41 | "preview": "https://www.mturk.com/mturk/preview", 42 | "manage": "https://requester.mturk.com/mturk/manageHITs", 43 | "reward": "0.00" 44 | }, 45 | "sandbox": { 46 | "endpoint": "https://mturk-requester-sandbox.us-east-1.amazonaws.com", 47 | "preview": "https://workersandbox.mturk.com/mturk/preview", 48 | "manage": "https://requestersandbox.mturk.com/mturk/manageHITs", 49 | "reward": "0.11" 50 | }, 51 | } 52 | mturk_environment = environments["live"] if create_hits_in_live else environments["sandbox"] 53 | 54 | # use profile if one was passed as an arg, otherwise 55 | profile_name = sys.argv[2] if len(sys.argv) >= 3 else None 56 | session = boto3.Session(profile_name=profile_name) 57 | client = session.client( 58 | service_name='mturk', 59 | region_name='us-east-1', 60 | endpoint_url=mturk_environment['endpoint'], 61 | ) 62 | 63 | hit = client.get_hit(HITId=hit_id) 64 | print 'Hit {} status: {}'.format(hit_id, hit['HIT']['HITStatus']) 65 | response = client.list_assignments_for_hit( 66 | HITId=hit_id, 67 | AssignmentStatuses=['Submitted', 'Approved'], 68 | MaxResults=10, 69 | ) 70 | 71 | assignments = response['Assignments'] 72 | print 'The number of submitted assignments is {}'.format(len(assignments)) 73 | for assignment in assignments: 74 | worker_id = assignment['WorkerId'] 75 | assignment_id = assignment['AssignmentId'] 76 | answer_xml = parseString(assignment['Answer']) 77 | 78 | # the answer is an xml document. we pull out the value of the first 79 | # //QuestionFormAnswers/Answer/FreeText 80 | answer = answer_xml.getElementsByTagName('FreeText')[0] 81 | # See https://stackoverflow.com/questions/317413 82 | only_answer = " ".join(t.nodeValue for t in answer.childNodes if t.nodeType == t.TEXT_NODE) 83 | 84 | print 'The Worker with ID {} submitted assignment {} and gave the answer "{}"'.format(worker_id, assignment_id, only_answer) 85 | 86 | # Approve the Assignment (if it hasn't already been approved) 87 | if assignment['AssignmentStatus'] == 'Submitted': 88 | print 'Approving Assignment {}'.format(assignment_id) 89 | client.approve_assignment( 90 | AssignmentId=assignment_id, 91 | RequesterFeedback='good', 92 | OverrideRejection=False, 93 | ) 94 | -------------------------------------------------------------------------------- /Python/my_question.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 |

This is a test question

13 |

14 |

15 | 16 | 17 | 18 | ]]> 19 |
20 | 450 21 |
22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Amazon Mechanical Turk Requester API Code Samples 2 | In 2017 [Amazon Mechanical Turk](https://www.mturk.com) (MTurk) launched support for [AWS Software Development Kits](https://aws.amazon.com/tools/). Requesters can now programmatically access MTurk with nine new SDKs. 3 | 4 | As part of this launch, MTurk also released a [new version](http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkAPI/Welcome.html) of the Requester API (version: ‘2017–01–17’). This version significantly updates naming conventions used in the API and adopts the latest AWS authentication and authorization standard of [Signature Version 4](http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html). The API uses REST and no longer requires developers to also be familiar with SOAP. These changes make the MTurk API consistent with other AWS APIs, simplifying the on-boarding process for both new and existing AWS developers. You can explore the full API reference documentation [here](http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkAPI/Welcome.html). 5 | 6 | This repo contains code samples to help you get started with the AWS SDKs and the updated API. 7 | 8 | ## Get Started 9 | 10 | 1. Set up your AWS account and your MTurk Requester and [Developer Sandbox](https://requestersandbox.mturk.com/) accounts as described [here](http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMechanicalTurkGettingStartedGuide/SetUp.html). 11 | 12 | 2. Download and set up the SDK of your choice: [Python/Boto3](https://aws.amazon.com/sdk-for-python/), Javascript ([NodeJS](https://aws.amazon.com/sdk-for-node-js/) or [Browser](https://aws.amazon.com/sdk-for-browser/)), [Java](https://aws.amazon.com/sdk-for-java/), [.NET](https://aws.amazon.com/sdk-for-net/), [Go](https://aws.amazon.com/sdk-for-go/), [Ruby](https://aws.amazon.com/sdk-for-ruby/), [PHP](https://aws.amazon.com/sdk-for-php/) or [C++](https://aws.amazon.com/sdk-for-cpp/). 13 | 14 | 3. Configure the AWS SDK to use the ‘us-east-1’ region. This is the region in which the MTurk API is available. 15 | 16 | 4. Connect to the MTurk Developer Sandbox and check your account balance (the Sandbox should always return a balance of $10,000). To connect to the MTurk Developer Sandbox, set the API endpoint in your SDK to https://mturk-requester-sandbox.us-east-1.amazonaws.com. You can find examples [here](https://requester.mturk.com/developer) in various languages. 17 | 18 | 5. Explore the code samples available in this repo to see how to use the updated API and the SDKs to submit tasks and get back results from MTurk. 19 | 20 | 6. Use the [MTurk API reference](http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkAPI/Welcome.html) to explore all the operations available. 21 | -------------------------------------------------------------------------------- /Ruby/mturk_sample_part_one_create_hit.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # Copyright 2017 Amazon.com, Inc. or its affiliates 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | require 'aws-sdk-core' 18 | 19 | # Before connecting to MTurk, set up your AWS account and IAM settings as described here: 20 | # https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 21 | # 22 | # Follow AWS best practices for setting up credentials here: 23 | # http://boto3.readthedocs.io/en/latest/guide/configuration.html 24 | 25 | # NOTE: Example config module contains secret keys 26 | require './mturk_config' 27 | include MTurkConfig 28 | 29 | # Set up AWS credentials. 30 | credentials = Aws::Credentials.new( 31 | MTurkConfig.account_key, 32 | MTurkConfig.secret_key 33 | ) 34 | 35 | # Use the following endpoints: 36 | # mturk: create tasks in the live marketplace 37 | # sandbox: use the sandbox to publish test Human Intelligence Tasks (HITs) without paying any money. 38 | # Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as your main MTurk account. 39 | endpoints = { 40 | mturk: 'https://mturk-requester.us-east-1.amazonaws.com', 41 | sandbox: 'https://mturk-requester-sandbox.us-east-1.amazonaws.com/' 42 | } 43 | 44 | # Instantiate a new client for the Amazon Mechanical Turk. 45 | mturk = Aws::MTurk::Client.new( 46 | endpoint: endpoints[:sandbox], 47 | region: 'us-east-1', 48 | credentials: credentials 49 | ) 50 | 51 | # Let's check your account balance 52 | # NOTE: In Sandbox, you should always get back $10,000 53 | puts balance = mturk.get_account_balance.available_balance 54 | puts "You have $#{balance}!" 55 | puts 56 | 57 | # Now let's create a hit! 58 | 59 | # Step 1) Get your favorite HTML question 60 | my_html_question = p %{ 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 |
70 | 71 |

This is a test question

72 |

73 |

74 |
75 | 76 | 77 | ]]> 78 |
79 | 450 80 |
81 | }.gsub(/\s+/, " ").strip 82 | 83 | # Step 2) Get your favorite qualifications! 84 | my_qualifications = [ 85 | # Let's exclude workers who live in California 86 | Aws::MTurk::Types::QualificationRequirement.new( 87 | qualification_type_id: '00000000000000000071', 88 | comparator: 'NotEqualTo', 89 | locale_values: [ Aws::MTurk::Types::Locale.new( country: 'US', subdivision: 'CA' ) ] 90 | ) 91 | ] 92 | 93 | # Step 3) Create your hit! 94 | puts result = mturk.create_hit( 95 | lifetime_in_seconds: 60 * 60 * 4, 96 | assignment_duration_in_seconds: 120, 97 | max_assignments: 1, 98 | reward: '0.25', 99 | title: 'Your Thoughts', 100 | description: 'Tell me what you think', 101 | question: my_html_question, 102 | qualification_requirements: my_qualifications 103 | ) 104 | 105 | puts 106 | puts "Once your HIT has an assignment ready to review, run the following script to approve/delte HIT:" 107 | puts "'ruby mturk_sample_part_two_approve_and_delete_hit.rb #{result.hit.hit_id}'" 108 | -------------------------------------------------------------------------------- /Ruby/mturk_sample_part_two_approve_and_delete_hit.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # Copyright 2017 Amazon.com, Inc. or its affiliates 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | require 'aws-sdk-core' 18 | 19 | # Before connecting to MTurk, set up your AWS account and IAM settings as described here: 20 | # https://blog.mturk.com/how-to-use-iam-to-control-api-access-to-your-mturk-account-76fe2c2e66e2 21 | # 22 | # Follow AWS best practices for setting up credentials here: 23 | # http://docs.aws.amazon.com/sdk-for-ruby/v1/developer-guide/set-up-creds.html 24 | 25 | # NOTE: Example config module contains secret keys 26 | require './mturk_config' 27 | include MTurkConfig 28 | 29 | # Set up AWS credentials. 30 | credentials = Aws::Credentials.new( 31 | MTurkConfig.account_key, 32 | MTurkConfig.secret_key 33 | ) 34 | 35 | # Use the following endpoints: 36 | # mturk: create tasks in the live marketplace 37 | # sandbox: use the sandbox to publish test Human Intelligence Tasks (HITs) without paying any money. 38 | # Sign up for a Sandbox account at https://requestersandbox.mturk.com/ with the same credentials as your main MTurk account. 39 | endpoints = { 40 | mturk: 'https://mturk-requester.us-east-1.amazonaws.com', 41 | sandbox: 'https://mturk-requester-sandbox.us-east-1.amazonaws.com/' 42 | } 43 | 44 | # Instantiate a new client for the Amazon Mechanical Turk. 45 | @mturk = Aws::MTurk::Client.new( 46 | endpoint: endpoints[:sandbox], 47 | region: 'us-east-1', 48 | credentials: credentials 49 | ) 50 | 51 | # This example uses the Sandbox, so no money is involved. 52 | def approve_all_submitted_assignments(hit_id) 53 | puts "Approving submitted assignments for hit id: #{hit_id}" 54 | # NOTE: If you have more than 100 results, you'll need to page through using 55 | # a unique pagination token 56 | @mturk.list_assignments_for_hit(hit_id: hit_id).assignments.each do |assignment| 57 | puts "Assignment answer submitted:" 58 | puts "#{assignment.answer}" 59 | puts 60 | puts "Approving assignment ... " 61 | @mturk.approve_assignment( 62 | assignment_id: assignment.assignment_id, 63 | requester_feedback: 'Thanks for the great work!' 64 | ) 65 | puts "Approved!" 66 | puts 67 | end 68 | end 69 | 70 | def delete_my_hit(hit_id) 71 | puts "Deleting hit with id: #{hit_id}" 72 | @mturk.delete_hit(hit_id: hit_id) 73 | rescue Aws::MTurk::Errors::RequestError => e 74 | puts "DID NOT DELETE: This hit still has unsubmitted assigments." 75 | end 76 | 77 | # Run script and pass in a HITId as a command line argument - see mturk_sample_part_one_create_hit.rb for generating a HIT 78 | hit_id = ARGV[0] 79 | while hit_id.nil? || hit_id.length.zero? 80 | puts "Please enter a hit id: " 81 | hit_id = gets.chomp! 82 | end 83 | 84 | approve_all_submitted_assignments(hit_id) 85 | delete_my_hit(hit_id) 86 | --------------------------------------------------------------------------------