安装步骤 安装 Java 开发环境 目前,CCPJava SDK 支持 J2SE 6.0 及以上的 Java 运行环境,您可以从 Java 官方网站 下载并按说明安装 Java 开发环境。
安装 CCP Java SDK 安装完 Java 开发环境后,您需要安装CCP SDK,将下面的依赖加入 pom.xml 。
CCPPath
com.aliyun ccp-baseclient Use the version shown in the maven badge com.aliyun ccp-client Use the version shown in the maven badge OSSPath
com.aliyun ccp-baseclient Use the version shown in the maven badge com.aliyun ccp-oss-client Use the version shown in the maven badge 注意:请关注 Git Hub提供的最新版本,查看相关功能并选择使用。
初始化Client 使用前提是您已经开通CCP服务, 并且在控制台创建了域实例。参见快速搭建云盘服务。之后您可以按需初始化CCPClient 或者 OSSClient。
AK & SK 初始化客户端 AK, SK 的获取详见 密钥管理页面。选择一对用于 SDK 的访问密钥对。如果没有,请创建一对新访问密钥,且保证它处于启用状态。有关如何创建访问密钥,参见 创建访问密钥。
import com.aliyun.ccp.ccpclient.Client; import com.aliyun.ccp.ccpclient.models.*; public class Demo { private static Client client; private static RuntimeOptions runtime; public static void createClient() throws IllegalAccessException { Config config = new Config(); config.domainId = "your domainId"; config.protocol = "https"; config.accessKeyId = System.getenv("accessKeyId"); config.accessKeySecret = System.getenv("accessKeySecret"); client = new Client(config); runtime = new RuntimeOptions(); } } AccessToken & RefreshToken 初始化客户端 clientId, clientSecret 的获取详见应用接入指南。
import com.aliyun.ccp.ccpclient.Client; import com.aliyun.ccp.ccpclient.models.*; public class Demo { private static Client client; private static RuntimeOptions runtime; public static void createClient() throws IllegalAccessException { Config config = new Config(); config.domainId = "your domainId"; config.protocol = "https"; config.clientId = System.getenv("clientId"); config.clientSecret = System.getenv("clientSecret"); config.accessToken = System.getenv("accessToken"); config.refreshToken = System.getenv("refreshToken"); config.expireTime = System.getenv("expireTime"); client = new Client(config); runtime = new RuntimeOptions(); } } 注意:AK & SK 模式 和 Access Token & Refresh Token 模式 同时只能存在一种
构造请求 Account 相关 API 获取图片验证码 以下代码用于获取图片验证码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 // 获取图片验证码 public static void getCaptcha() throws Exception { try { GetCaptchaRequest getCaptchaRequest = new GetCaptchaRequest(); getCaptchaRequest.appId = appId; Captcha captcha = client.getCaptcha(getCaptchaRequest, runtime); // 打印返回结果 System.out.println(new Gson().toJson(captcha)); } catch (TeaException e) { System.out.println(e.getMessage()); System.out.println(e.getCode()); System.out.println(e.getData()); } } 返回结果 { "captcha":"iVBORw0KGgoAAAANSUhEUgAAAFAAAAAaCAIAAACvsEzwAAABFUlEQVR42u3YwQ7CIAwGYOKDaLya7OBL+BDePRoT4/vPJSSEjFJ+2oEIW3oy0PHRDnVmHuwyO3gH93J9Xm85+PA4+REOmKYLGbGE5/vVBn/f2/O4hMZs9FrSjIMdtVEwKSSLTG7Bb8HZLc30MALmB7QLlmkHAiPa3sDIsE7AYHkbAtvvaw0YHNlQhRezZeee0nh5BWC/EkVamjHHtqAoWGbOe4ZtqZFfWrmHcxhIhYuD3Z2S2gpgmVn450G2u8i5mOzn1aFlV7LKwCQ0FRaqTOWcsSPazvUzZIPJbStaahCMT48lNIjHD0GpkQzKftkMzK9e8/yHecLPwZ0ihZuBK7yFKTq9uXdamp7/M7Df22JPcsxw4C/GgzEzOWpc1QAAAABJRU5ErkJggg==", "captchaFormat":"png", "captchaId":"f644d0b2a7d21a3caddb17377c90c28e8DPXYUIfvs3" } 获取短信验证码 以下代码用于获取短信验证码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void getMNSCode() throws Exception { try { MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest(); sendSmsCodeRequest.appId = appId; sendSmsCodeRequest.phoneNumber = "13225"; sendSmsCodeRequest.type = "login"; MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime); System.out.println(new Gson().toJson(response)); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 返回结果 { "requestId":"07A5175C-E124-42EC-9304-2294DC2CEFAC", "smsCodeId":"b40bba70b37d74****************6j3IW7HP" } 验证手机号是否注册 以下代码用于验证手机号是否存在,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void checkMobileIsExist() throws Exception { try { MobileCheckExistRequest checkExistRequest = new MobileCheckExistRequest(); checkExistRequest.appId = appId; checkExistRequest.phoneNumber = "1325"; MobileCheckExistResponse checkExistResponse = client.checkExist(checkExistRequest, runtime); System.out.println(new Gson().toJson(checkExistResponse)); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 返回结果 { "requestId":"4DB78761-20AE-4724-8960-ADD225F35A0F", "isExist":true, "phoneNumber":"1325" } 手机号注册 以下代码用于手机号注册,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void register() throws Exception { try { // get register mns Code MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest(); sendSmsCodeRequest.appId = appId; sendSmsCodeRequest.phoneNumber = "1325"; sendSmsCodeRequest.type = "change_password"; MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime); String changePasswordCode = response.smsCodeId; // mobile register MobileRegisterRequest registerRequest = new MobileRegisterRequest(); registerRequest.appId = appId; registerRequest.phoneNumber = "132225"; registerRequest.smsCode = "123456"; registerRequest.smsCodeId = changePasswordCode; AccountAccessTokenResponse tokenResponse = client.register(registerRequest, runtime); System.out.println(new Gson().toJson(tokenResponse)); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 返回结果 { "access_token": "eyJhbGciOiJSUzI1NiI", "refresh_token": "fvw1FLZ***tGjXxJl", "expires_in": 7200, "token_type": "Bearer", "user_id": "6c23c98d3*3f9f5211", "user_name": "132****225", "avatar": "", "nick_name": "1329225", "default_drive_id": "", "role": "admin", "expire_time": "2019-10-30T11:44:24Z", "state": "", "exist_link": [], "need_link": false, "user_data": {} } 手机号短信登录 以下代码用于短信登录,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void mnsLogin() throws Exception { try { // get login mns Code MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest(); sendSmsCodeRequest.appId = appId; sendSmsCodeRequest.phoneNumber = "1325"; sendSmsCodeRequest.type = "change_password"; MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime); String changePasswordCode = response.smsCodeId; // mns login MobileLoginRequest mobileLoginRequest = new MobileLoginRequest(); mobileLoginRequest.appId = appId; mobileLoginRequest.phoneNumber = "13297039225"; mobileLoginRequest.smsCode = "123456"; mobileLoginRequest.smsCodeId = changePasswordCode; AccountAccessTokenResponse tokenResponse = client.login(mobileLoginRequest, runtime); System.out.println(new Gson().toJson(tokenResponse)); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 返回结果参见手机号注册的返回结果 设置登录密码 以下代码用于设置登录密码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void setPassWord() throws Exception { try { DefaultSetPasswordRequest setPasswordRequest = new DefaultSetPasswordRequest(); setPasswordRequest.appId = appId; setPasswordRequest.newPassword = "1234567"; setPasswordRequest.state = "aKsdfGoeasdksFasds"; client.setPassword(setPasswordRequest, runtime); // 此结果不返回body } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 密码登录 以下代码用于密码登录,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 // 密码登录 public static void passwordLogin() throws Exception { try { MobileLoginRequest mobileLoginRequest = new MobileLoginRequest(); mobileLoginRequest.appId = appId; mobileLoginRequest.phoneNumber = "13225"; mobileLoginRequest.password = ""; AccountAccessTokenResponse tokenResponse = client.login(mobileLoginRequest, runtime); // 打印返回结果 System.out.println(new Gson().tojson(tokenResponse)); } catch (TeaException e) { System.out.println(e.getMessage()); System.out.println(e.getCode()); System.out.println(e.getData()); } } 返回结果参见手机号注册的返回结果 修改登录密码 以下代码用于修改登录密码,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void changePassWord() throws Exception { try { // get change passowrd mns Code MobileSendSmsCodeRequest sendSmsCodeRequest = new MobileSendSmsCodeRequest(); sendSmsCodeRequest.appId = appId; sendSmsCodeRequest.phoneNumber = "1325"; sendSmsCodeRequest.type = "change_password"; MobileSendSmsCodeResponse response = client.mobileSendSmsCode(sendSmsCodeRequest, runtime); String changePasswordCode = response.smsCodeId; // chang password DefaultChangePasswordRequest changePasswordRequest = new DefaultChangePasswordRequest(); changePasswordRequest.appId = appId; changePasswordRequest.phoneNumber = "13***25"; changePasswordRequest.newPassword = "1234567"; changePasswordRequest.smsCode = "13124353"; changePasswordRequest.smsCodeId = changePasswordCode; client.changePassword(changePasswordRequest, runtime); //此接口不返回body } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 通过刷新令牌获取访问令牌 以下代码用于刷新token,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void getTokenByRefreshToken() throws Exception { try { TokenRequest tokenRequest = new TokenRequest(); tokenRequest.appId = appId; tokenRequest.grantType = "refresh_token"; tokenRequest.refreshToken = "Dln****Tcpz"; AccountAccessTokenResponse tokenResponse = client.token(tokenRequest, runtime); System.out.println(new Gson().toJson(tokenResponse)); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); } } 返回结果参见手机号注册的返回结果 通过账号获取访问令牌 以下代码用于获取用户认证方式,此处的示例,目的是展示关键参数 ,其他参数请查看官方文档。 public void getAccessTokenByLinkInfo() throws Exception { try { // 此接口需要ak,sk调用, 请使用ak, sk初始化客户端 GetAccessTokenByLinkInfoRequest getAccessTokenByLinkInfoRequest = new GetAccessTokenByLinkInfoRequest(); getAccessTokenByLinkInfoRequest.identity = "13297039225"; getAccessTokenByLinkInfoRequest.type = "mobile"; AccountAccessTokenResponse tokenResponse = client.getAccessTokenByLinkInfo(getAccessTokenByLinkInfoRequest, runtime); System.out.println(new Gson().toJson(tokenResponse)); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getMessage()); System.out.println(e.getData()); } } 返回结果参见手机号注册的返回结果
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。