內容協作平臺如何使用Golang SDK?

阿裏雲問答 2022-01-07 07:32:53 阅读数:892

使用 golang sdk

內容協作平臺如何使用Golang SDK?




采納答案1:

安裝步驟 安裝 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()); } } 返回結果參見手機號注册的返回結果


版权声明:本文为[阿裏雲問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201070732527363.html