Last active
January 21, 2016 07:27
-
-
Save SendOTP/c911b711e303d5d69c41 to your computer and use it in GitHub Desktop.
SendOTP : Java server side sample code for Custom Security Architecture.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.jersey.api.client.Client; | |
import com.sun.jersey.api.client.ClientResponse; | |
import com.sun.jersey.api.client.WebResource; | |
import org.json.JSONObject; | |
import javax.ws.rs.core.MediaType; | |
import java.util.HashMap; | |
/* | |
* This code is based on jersey-client library. | |
* For gradle based project use compile 'com.sun.jersey:jersey-client:1.18.4' | |
* You can also download the jar and add it to you project. | |
* */ | |
public class SendOTP { | |
//Base URL | |
public static String baseUrl = "http://sendotp.msg91.com/api"; | |
// Your application key | |
public static String applicationKey = "Your application key here"; | |
/* | |
* This function is used to send OTP message on mobile number | |
* */ | |
public static void generateOTP(String countryCode, String mobileNumber){ | |
try { | |
Client client = Client.create(); | |
String Url = baseUrl+"/generateOTP"; | |
WebResource webResource = client.resource(Url); | |
HashMap<String, String> requestBodyMap = new HashMap<>(); | |
requestBodyMap.put("countryCode",countryCode); | |
requestBodyMap.put("mobileNumber",mobileNumber); | |
JSONObject requestBodyJsonObject = new JSONObject(requestBodyMap); | |
String input = requestBodyJsonObject.toString(); | |
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON) | |
.header("application-Key", applicationKey) | |
.post(ClientResponse.class, input); | |
String output = response.getEntity(String.class); | |
System.out.println(output); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/* | |
* This function is used to verify mobile number and will give refresh token in response. | |
* */ | |
public static String verifyOTP(String countryCode, String mobileNumber, String oneTimePassword){ | |
try { | |
Client client = Client.create(); | |
String Url = baseUrl+"/verifyOTP"; | |
WebResource webResource = client.resource(Url); | |
HashMap<String, String> requestBodyMap = new HashMap<>(); | |
requestBodyMap.put("countryCode",countryCode); | |
requestBodyMap.put("mobileNumber",mobileNumber); | |
requestBodyMap.put("oneTimePassword",oneTimePassword); | |
JSONObject requestBodyJsonObject = new JSONObject(requestBodyMap); | |
String input = requestBodyJsonObject.toString(); | |
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON) | |
.header("application-Key", applicationKey) | |
.post(ClientResponse.class, input); | |
String output = response.getEntity(String.class); | |
System.out.println(output); | |
JSONObject jsonObject = new JSONObject(output); | |
return jsonObject.get("refreshToken").toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment