{"__v":17,"_id":"56d866f4a957fb1500765a40","category":{"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","_id":"56e92ef71996862200fd7f42","pages":[],"__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-03-16T10:01:27.194Z","from_sync":false,"order":3,"slug":"java-quickstarts","title":"Java Quickstarts"},"parentDoc":null,"project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","version":{"__v":20,"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-03-03T16:31:48.383Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip\"\n}\n[/block]\n##Prerequisites\nYou'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing.\n\n##Registration\nWe're going to create a simple console application that sends a verification code via SMS when the user enters their phone number.\n\nWe're going to make use of the following libraries\n* [Unirest](http://unirest.io/java.html) to make calling REST APIs easy.\n* [Jackson](http://wiki.fasterxml.com/JacksonHome) for JSON deserialisation\n\n1. Create a new java console project\n2. Add the following libraries to the project\n  * unirest-java-1.4.7.jar\n  * jackson-annotations-2.7.2.jar\n  * jackson-core-2.7.2.jar\n  * jackson-databind-2.7.2.jar\n3. Add a new class, Verify.java\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport java.io.IOException;\\nimport java.util.Scanner;\\nimport java.util.UUID;\\n\\nimport com.fasterxml.jackson.databind.ObjectMapper;\\nimport com.mashape.unirest.http.HttpResponse;\\nimport com.mashape.unirest.http.Unirest;\\nimport com.mashape.unirest.http.exceptions.UnirestException;\\n\\npublic class Verify {\\n\\tprivate static String userName = \\\":::at:::username@\\\";\\n\\tprivate static String password = \\\"@Password@\\\";\\n\\tprivate static UUID requestId;\\n\\tprivate static ObjectMapper objectMapper = new ObjectMapper();\\n  \\n  private static boolean SendCode(String phoneNumber) throws UnirestException, IOException {\\n\\t\\tHttpResponse<String> response = \\n\\t\\t\\t\\tUnirest.post(\\\"https://services.dynmark.com/webapi/verify\\\")\\n\\t\\t\\t\\t.header(\\\"content-type\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.header(\\\"accept\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.basicAuth(userName, password)\\n\\t\\t\\t\\t.body(\\\"{\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"'from': 'Example',\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"'to': '\\\" + phoneNumber + \\\"',\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"}\\\")\\n\\t\\t\\t\\t.asString();\\n    \\n    return true;\\n  }\\n\\n\\tpublic static void main(String[] args) throws UnirestException, IOException {\\n\\t\\tScanner scanIn = new Scanner(System.in);\\n\\t\\tSystem.out.println(\\\"Enter your phone number: \\\");\\n\\t\\tSendCode(scanIn.nextLine()))\\n\\n\\t\\tscanIn.close();         \\n\\t}\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nLets look at the details:\n1. You'll need your Dynmark username and password to authenticate calls.\n2. We use the [Unirest](http://unirest.io/java.html) library to make REST calls\n3. We define a request containing the minimum details to send a verification code\n\nUsers often make mistakes entering their details. Let's add some code to parse the response from the API and handle validation failures:\n\nAdd a new file *ValidateResponse.java*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnoreProperties;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\n\\n@JsonIgnoreProperties(ignoreUnknown = true)\\npublic class ValidateResponse {\\n\\t\\n\\t@JsonProperty(\\\"status\\\")\\n\\tprivate String status;\\n\\t\\n\\tpublic String getStatus() {\\n\\t\\treturn status;\\n\\t}\\n\\tpublic void setStatus(String status) {\\n\\t\\tthis.status = status;\\n\\t}\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nAdd a new file, *ApiValidationFailure.java*. Note that you can reuse the one from the [Send SMS Messages](doc:qs-java-send-sms-messages) quickstart.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\npublic class ApiValidationFailure {\\n\\tprivate String property;\\n\\tprivate String failureCode;\\n\\tprivate String details;\\n\\n\\tpublic String getProperty() {\\n\\t\\treturn property;\\n\\t}\\n\\tpublic void setProperty(String property) {\\n\\t\\tthis.property = property;\\n\\t}\\n\\tpublic String getFailureCode() {\\n\\t\\treturn failureCode;\\n\\t}\\n\\tpublic void setFailureCode(String failureCode) {\\n\\t\\tthis.failureCode = failureCode;\\n\\t}\\n\\tpublic String getDetails() {\\n\\t\\treturn details;\\n\\t}\\n\\tpublic void setDetails(String details) {\\n\\t\\tthis.details = details;\\n\\t}\\n}\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n2. Now we change the SendCode method to parse the response and store the returned requestId when we get an HTTP 201 OK. This shows that the API call was in a valid format.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\tif (response.getStatus() == 201)\\n\\t\\t{\\n\\t\\t\\tSendCodeResponse body = objectMapper.readValue(response.getBody(), SendCodeResponse.class);\\n\\t\\t\\trequestId = body.getRequestId();\\n\\t\\t\\treturn true;\\n\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n3. We should also check for HTTP 401 Unauthorised to ensure we're using the right credentials, and also for HTTP 400 Bad Request to ensure we've submitted a request that passes basic JSON validation.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\telse if (response.getStatus() == 401)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Your username and password are incorrect\\\");\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\telse if (response.getStatus() == 400)\\n\\t\\t{\\n\\t\\t\\tApiValidationFailure[] failures =\\n        objectMapper.readValue(response.getRawBody(), ApiValidationFailure[].class);\\n\\n\\t\\t\\tSystem.out.println(\\\"Sorry, we couldn't send you a code.\\\");\\n\\t\\t\\tfor (ApiValidationFailure failure : failures)\\n\\t\\t\\t{\\n\\t\\t\\t\\tif (failure.getFailureCode().equals(\\\"ToInvalid\\\"))\\n\\t\\t\\t\\t{\\n\\t\\t\\t\\t\\tSystem.out.println(\\\"Your phone number doesn't look like a valid number.\\\");\\n\\t\\t\\t\\t}\\n\\t\\t\\t}\\n\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\telse\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Non success response \\\" + response.getStatus());\\n\\t\\t\\treturn false;\\n\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow that we can send a verification code to a phone, we need to be able to verify the code that's received. We'll add another method to call [Validate](doc:rest-verify-validate), passing the requestId returned from SendCode:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tprivate static boolean ValidateCode(String code) throws UnirestException, IOException {\\n\\t\\tHttpResponse<String> response = \\n\\t\\t\\t\\tUnirest.put(\\\"https://services.dynmark.com/webapi/verify/\\\" + requestId.toString() + \\\"/validate\\\")\\n\\t\\t\\t\\t.header(\\\"content-type\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.header(\\\"accept\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.basicAuth(userName, password)\\n\\t\\t\\t\\t.body(\\\"{\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"'code': '\\\" + code + \\\"'\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"}\\\")\\n\\t\\t\\t\\t.asString();\\n\\n\\t\\tif (response.getStatus() == 200)\\n\\t\\t{\\n\\t\\t\\tValidateResponse body = \\n        objectMapper.readValue(response.getBody(), ValidateResponse.class);\\n\\n\\t\\t\\tif (body.getStatus().equals(\\\"CodeVerified\\\"))\\n\\t\\t\\t{\\n\\t\\t\\t\\tSystem.out.println(\\\"Your code was correct and is verified.\\\");\\n\\t\\t\\t\\treturn true;\\n\\t\\t\\t}\\n\\n\\t\\t\\tSystem.out.println(\\\"Your code was incorrect. Status: \\\" + body.getStatus());\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\tif (response.getStatus() == 204)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Request id not recognised\\\");\\n\\t\\t\\treturn true;\\n\\t\\t}\\n\\t\\telse if (response.getStatus() == 401)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Your username and password are incorrect\\\");\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\telse if (response.getStatus() == 400)\\n\\t\\t{\\n\\t\\t\\tApiValidationFailure[] failures =\\n        objectMapper.readValue(response.getRawBody(), ApiValidationFailure[].class);\\n\\n\\t\\t\\tSystem.out.println(\\\"Sorry, we couldn't send you a code.\\\");\\n\\t\\t\\tfor (ApiValidationFailure failure : failures)\\n\\t\\t\\t{\\n\\t\\t\\t\\tif (failure.getFailureCode().equals(\\\"ToInvalid\\\"))\\n\\t\\t\\t\\t{\\n\\t\\t\\t\\t\\tSystem.out.println(\\\"Your phone number doesn't look like a valid number.\\\");\\n\\t\\t\\t\\t}\\n\\t\\t\\t}\\n\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\telse\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Non success response \\\" + response.getStatus());\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"An example of how Verify can be used as part of an authentication process to check the owner of a mobile number","slug":"qs-java-verify","type":"basic","title":"Use Verify to check mobile number ownership"}

Use Verify to check mobile number ownership

An example of how Verify can be used as part of an authentication process to check the owner of a mobile number

[block:callout] { "type": "info", "body": "Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip" } [/block] ##Prerequisites You'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing. ##Registration We're going to create a simple console application that sends a verification code via SMS when the user enters their phone number. We're going to make use of the following libraries * [Unirest](http://unirest.io/java.html) to make calling REST APIs easy. * [Jackson](http://wiki.fasterxml.com/JacksonHome) for JSON deserialisation 1. Create a new java console project 2. Add the following libraries to the project * unirest-java-1.4.7.jar * jackson-annotations-2.7.2.jar * jackson-core-2.7.2.jar * jackson-databind-2.7.2.jar 3. Add a new class, Verify.java [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.io.IOException;\nimport java.util.Scanner;\nimport java.util.UUID;\n\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport com.mashape.unirest.http.HttpResponse;\nimport com.mashape.unirest.http.Unirest;\nimport com.mashape.unirest.http.exceptions.UnirestException;\n\npublic class Verify {\n\tprivate static String userName = \"@username@\";\n\tprivate static String password = \"@Password@\";\n\tprivate static UUID requestId;\n\tprivate static ObjectMapper objectMapper = new ObjectMapper();\n \n private static boolean SendCode(String phoneNumber) throws UnirestException, IOException {\n\t\tHttpResponse<String> response = \n\t\t\t\tUnirest.post(\"https://services.dynmark.com/webapi/verify\")\n\t\t\t\t.header(\"content-type\", \"application/json\")\n\t\t\t\t.header(\"accept\", \"application/json\")\n\t\t\t\t.basicAuth(userName, password)\n\t\t\t\t.body(\"{\"\n\t\t\t\t\t\t+ \"'from': 'Example',\"\n\t\t\t\t\t\t+ \"'to': '\" + phoneNumber + \"',\"\n\t\t\t\t\t\t+ \"}\")\n\t\t\t\t.asString();\n \n return true;\n }\n\n\tpublic static void main(String[] args) throws UnirestException, IOException {\n\t\tScanner scanIn = new Scanner(System.in);\n\t\tSystem.out.println(\"Enter your phone number: \");\n\t\tSendCode(scanIn.nextLine()))\n\n\t\tscanIn.close(); \n\t}\n}", "language": "java" } ] } [/block] Lets look at the details: 1. You'll need your Dynmark username and password to authenticate calls. 2. We use the [Unirest](http://unirest.io/java.html) library to make REST calls 3. We define a request containing the minimum details to send a verification code Users often make mistakes entering their details. Let's add some code to parse the response from the API and handle validation failures: Add a new file *ValidateResponse.java* [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport com.fasterxml.jackson.annotation.JsonIgnoreProperties;\nimport com.fasterxml.jackson.annotation.JsonProperty;\n\n@JsonIgnoreProperties(ignoreUnknown = true)\npublic class ValidateResponse {\n\t\n\t@JsonProperty(\"status\")\n\tprivate String status;\n\t\n\tpublic String getStatus() {\n\t\treturn status;\n\t}\n\tpublic void setStatus(String status) {\n\t\tthis.status = status;\n\t}\n}", "language": "java" } ] } [/block] Add a new file, *ApiValidationFailure.java*. Note that you can reuse the one from the [Send SMS Messages](doc:qs-java-send-sms-messages) quickstart. [block:code] { "codes": [ { "code": "package com.dynmark;\n\npublic class ApiValidationFailure {\n\tprivate String property;\n\tprivate String failureCode;\n\tprivate String details;\n\n\tpublic String getProperty() {\n\t\treturn property;\n\t}\n\tpublic void setProperty(String property) {\n\t\tthis.property = property;\n\t}\n\tpublic String getFailureCode() {\n\t\treturn failureCode;\n\t}\n\tpublic void setFailureCode(String failureCode) {\n\t\tthis.failureCode = failureCode;\n\t}\n\tpublic String getDetails() {\n\t\treturn details;\n\t}\n\tpublic void setDetails(String details) {\n\t\tthis.details = details;\n\t}\n}\n", "language": "java" } ] } [/block] 2. Now we change the SendCode method to parse the response and store the returned requestId when we get an HTTP 201 OK. This shows that the API call was in a valid format. [block:code] { "codes": [ { "code": "\t\tif (response.getStatus() == 201)\n\t\t{\n\t\t\tSendCodeResponse body = objectMapper.readValue(response.getBody(), SendCodeResponse.class);\n\t\t\trequestId = body.getRequestId();\n\t\t\treturn true;\n\t\t}", "language": "csharp" } ] } [/block] 3. We should also check for HTTP 401 Unauthorised to ensure we're using the right credentials, and also for HTTP 400 Bad Request to ensure we've submitted a request that passes basic JSON validation. [block:code] { "codes": [ { "code": "\t\telse if (response.getStatus() == 401)\n\t\t{\n\t\t\tSystem.out.println(\"Your username and password are incorrect\");\n\t\t\treturn false;\n\t\t}\n\t\telse if (response.getStatus() == 400)\n\t\t{\n\t\t\tApiValidationFailure[] failures =\n objectMapper.readValue(response.getRawBody(), ApiValidationFailure[].class);\n\n\t\t\tSystem.out.println(\"Sorry, we couldn't send you a code.\");\n\t\t\tfor (ApiValidationFailure failure : failures)\n\t\t\t{\n\t\t\t\tif (failure.getFailureCode().equals(\"ToInvalid\"))\n\t\t\t\t{\n\t\t\t\t\tSystem.out.println(\"Your phone number doesn't look like a valid number.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn false;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tSystem.out.println(\"Non success response \" + response.getStatus());\n\t\t\treturn false;\n\t\t}", "language": "csharp" } ] } [/block] Now that we can send a verification code to a phone, we need to be able to verify the code that's received. We'll add another method to call [Validate](doc:rest-verify-validate), passing the requestId returned from SendCode: [block:code] { "codes": [ { "code": "\tprivate static boolean ValidateCode(String code) throws UnirestException, IOException {\n\t\tHttpResponse<String> response = \n\t\t\t\tUnirest.put(\"https://services.dynmark.com/webapi/verify/\" + requestId.toString() + \"/validate\")\n\t\t\t\t.header(\"content-type\", \"application/json\")\n\t\t\t\t.header(\"accept\", \"application/json\")\n\t\t\t\t.basicAuth(userName, password)\n\t\t\t\t.body(\"{\"\n\t\t\t\t\t\t+ \"'code': '\" + code + \"'\"\n\t\t\t\t\t\t+ \"}\")\n\t\t\t\t.asString();\n\n\t\tif (response.getStatus() == 200)\n\t\t{\n\t\t\tValidateResponse body = \n objectMapper.readValue(response.getBody(), ValidateResponse.class);\n\n\t\t\tif (body.getStatus().equals(\"CodeVerified\"))\n\t\t\t{\n\t\t\t\tSystem.out.println(\"Your code was correct and is verified.\");\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tSystem.out.println(\"Your code was incorrect. Status: \" + body.getStatus());\n\t\t\treturn false;\n\t\t}\n\t\tif (response.getStatus() == 204)\n\t\t{\n\t\t\tSystem.out.println(\"Request id not recognised\");\n\t\t\treturn true;\n\t\t}\n\t\telse if (response.getStatus() == 401)\n\t\t{\n\t\t\tSystem.out.println(\"Your username and password are incorrect\");\n\t\t\treturn false;\n\t\t}\n\t\telse if (response.getStatus() == 400)\n\t\t{\n\t\t\tApiValidationFailure[] failures =\n objectMapper.readValue(response.getRawBody(), ApiValidationFailure[].class);\n\n\t\t\tSystem.out.println(\"Sorry, we couldn't send you a code.\");\n\t\t\tfor (ApiValidationFailure failure : failures)\n\t\t\t{\n\t\t\t\tif (failure.getFailureCode().equals(\"ToInvalid\"))\n\t\t\t\t{\n\t\t\t\t\tSystem.out.println(\"Your phone number doesn't look like a valid number.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn false;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tSystem.out.println(\"Non success response \" + response.getStatus());\n\t\t\treturn false;\n\t\t}\n\t}", "language": "java" } ] } [/block]