本机号码一键登录
1、背景
用户名密码方式的登录与注册繁琐,耗时长,用户体验差。
号码认证服务
https://www.aliyun.com/product/dypns?spm=5176.19720258.J_3207526240.79.e93976f4did2vi
2、原理
3、服务器
(1)前提条件
这里用阿里云作为第三方整合运营商的厂商
必须配置对应的accessKey ID和accessKey Secret
(2)流程
服务器需要拿到app从运营商获取到的认证token,请求运营商获取到手机号,并通过手机号进行注册登录,并返回登录相关信息。
流程:本机号码一键登录在服务端的业务流程图
本机号码一键登录在服务端的业务流程图:
(3)代码示例
- 添加依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dypnsapi20170525</artifactId>
<version>1.0.6</version>
</dependency>
- 获取本机号码代码示例
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dypnsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dypnsapi.aliyuncs.com";
return new com.aliyun.dypnsapi20170525.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.dypnsapi20170525.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
com.aliyun.dypnsapi20170525.models.GetMobileRequest getMobileRequest = new com.aliyun.dypnsapi20170525.models.GetMobileRequest();
//app端请求的认证token(accessToken)
getMobileRequest.setAccessToken("accessToken");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
client.getMobileWithOptions(getMobileRequest, runtime);
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
- 响应示例
{
"Code": "OK",
"Message": "请求成功",
"RequestId": "8906582E-6722",
"GetMobileResultDTO": {
"Mobile": "13900001234"
}
}