SMSSDK驗證碼服務端校驗介面
阿新 • • 發佈:2018-12-06
一、前言
有些時候開發者需要驗證服務託管自己的伺服器(開發者伺服器擁有校驗驗證碼的功能),然後可以更好處理自己的業務邏輯。因此,Mob平臺提供了此介面實現上述需求,供開發者使用。
此介面支援
SMSSDK2.0.0以上版本(包括2.0.0)。
二、摘要
本介面屬於SMSSDK簡訊平臺的開放驗證服務,不提供簡訊傳送服務,主要是承擔驗證APP 使用SMSSDK傳送的簡訊驗證碼,使用該介面來驗證驗證碼是否正常。
簡單梳理了一下流程,在沒有簡訊服務端驗證介面的時候,我的伺服器需要知道使用者是否驗證成功是這樣走的
引入了服務端驗證介面我們可以這樣走,本介面開放了第五步動作
三、介面使用
第一步:開通服務端驗證開關
免費簡訊驗證碼SDK/應用管理/簡訊設定 , 把服務端驗證介面開關開啟
第二步:請求介面驗證簡訊
請求地址為:https://webapi.sms.mob.com/sms/verify
請求方式: POST
請求引數
返回結果
{status:200}
測試指令碼
curl -d 'appkey=xxxx&phone=132****8362&zone=86&code=xxxx' 'https://webapi.sms.mob.com/sms/verify'
四、樣例程式碼
phper 請看
<?php // 配置項 $api = '介面地址(例:https://webapi.sms.mob.com); $appkey = '您的appkey'; // 傳送驗證碼 $response = postRequest( $api . '/sms/verify', array( 'appkey' => $appkey, 'phone' => '152xxxx4345', 'zone' => '86', 'code' => '1234', ) ); /** * 發起一個post請求到指定介面 * * @param string $api 請求的介面 * @param array $params post引數 * @param int $timeout 超時時間 * @return string 請求結果 */ function postRequest( $api, array $params = array(), $timeout = 30 ) { $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $api ); // 以返回的形式接收資訊 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // 設定為POST方式 curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) ); // 不驗證https證書 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 ); curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8', 'Accept: application/json', ) ); // 傳送資料 $response = curl_exec( $ch ); // 不要忘記釋放資源 curl_close( $ch ); return $response; }
五、C#開發者請看
public static void ConnectSSL()
{
WebRequest request = WebRequest.Create("https://webapi.sms.mob.com/sms/verify");
request.Proxy = null;
request.Credentials = CredentialCache.DefaultCredentials;
//allows for validation of SSL certificates
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
byte[] bs = Encoding.UTF8.GetBytes("appkey=xxxxxxxx&phone=xxxxxxxxxx&zone=xx&code=xxxx");
request.Method = "Post";
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
}
//for testing purpose only, accept any dodgy certificate...
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
六、JAVA 開發者請看
樣例程式: https://github.com/tian-github/Mob_SmsSpi
public static void main(String[] args) throws Exception {
String result = requestData("https://webapi.sms.mob.com/sms/verify",
"appkey=xxxx&phone=xxx&zone=xx&&code=xx");
System.out.println(result);
}
/**
* 發起https 請求
* @param address
* @param m
* @return
*/
public static String requestData(String address ,String params){
HttpURLConnection conn = null;
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
//ip host verify
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return urlHostName.equals(session.getPeerHost());
}
};
//set ip host verify
HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
URL url = new URL(address);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");// POST
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
// set params ;post params
if (params!=null) {
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(params.getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
}
conn.connect();
//get result
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
String result = parsRtn(conn.getInputStream());
return result;
} else {
System.out.println(conn.getResponseCode() + " "+ conn.getResponseMessage());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.disconnect();
}
return null;
}
七、python 開發者請看
感謝@jimidata 開發者提供,謝謝支援
樣例程式: https://github.com/freeznet/py_mob_sms
#!/usr/bin/env python
# encoding: utf-8
import requests
__author__ = 'rui'
class MobSMS:
def __init__(self, appkey):
self.appkey = appkey
self.verify_url = 'https://webapi.sms.mob.com/sms/verify'
def verify_sms_code(self, zone, phone, code, debug=False):
if debug:
return 200
data = {'appkey': self.appkey, 'phone': phone, 'zone': zone, 'code': code}
req = requests.post(self.verify_url, data=data, verify=False)
if req.status_code == 200:
j = req.json()
return j.get('status', 500)
return 500
if __name__ == '__main__':
mobsms = MobSMS('your_mob_sms_key_goes_here')
print mobsms.verify_sms_code(86, 13900000000, '1234')