eleme.openapi.demo.OAuthClientDemo Maven / Gradle / Ivy
The newest version!
package eleme.openapi.demo;
import eleme.openapi.sdk.api.entity.product.OCategory;
import eleme.openapi.sdk.api.exception.ServiceException;
import eleme.openapi.sdk.api.service.ProductService;
import eleme.openapi.sdk.config.Config;
import eleme.openapi.sdk.config.Constants;
import eleme.openapi.sdk.efs.EfsMediaClient;
import eleme.openapi.sdk.oauth.IOAuthClient;
import eleme.openapi.sdk.oauth.OAuthClient;
import eleme.openapi.sdk.oauth.impl.DefaultIOAuthClient;
import eleme.openapi.sdk.oauth.request.ClientTokenRequest;
import eleme.openapi.sdk.oauth.response.OpenId;
import eleme.openapi.sdk.oauth.response.Token;
import eleme.openapi.sdk.utils.JacksonUtils;
import eleme.openapi.sdk.utils.PropertiesUtils;
import eleme.openapi.sdk.utils.StringUtils;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class OAuthClientDemo {
// 设置是否沙箱环境
private static final boolean isSandbox = true;
// 设置APPKEY
private static final String key = "app key";
// 设置APPSECRET
private static final String secret = "app secret";
// 初始化OAuthClient
private static OAuthClient client = null;
private static Map tokenMap = new HashMap();
private static Config config = null;
static {
// 初始化全局配置工具
config = new Config(isSandbox, key, secret);
client = new OAuthClient(config);
}
public static void main(String[] args) throws ServiceException {
OAuthClientDemo demo = new OAuthClientDemo();
// demo.clientTokenTest();
// demo.serverOAuthCodeTest();
// demo.serverTokenTest();
// demo.serverRefreshTokenTest();
// demo.serverOpenIdTest();
demo.clientTokenChainSandBoxTest();
}
/**
* 客户端(个人)获取Token
*/
private void clientTokenTest() {
Token token = client.getTokenInClientCredentials();
if (token.isSuccess()) {
setTokenInfo(token);
System.out.println(token);
} else {
System.out.println("error_code: " + token.getError());
System.out.println("error_desc: " + token.getError_description());
}
}
private void clientTokenChainSandBoxTest() {
config = new Config(true, true, "zDD7iCQ4E1", "671576abfeaaadf31d1ce11cd12fbfa89660");
client = new OAuthClient(config);
IOAuthClient ioAuthClient = new DefaultIOAuthClient(config, Constants.TOKEN_URL);
ClientTokenRequest oAuthRequest = new ClientTokenRequest(config);
Map headMap = oAuthRequest.getHeaderMap();
headMap.put("sand_box_chain_id", "92119012");
oAuthRequest.setHeaderMap(headMap);
Token token = ioAuthClient.execute(oAuthRequest);
if (token.isSuccess()) {
setTokenInfo(token);
System.out.println(token);
} else {
System.out.println("error_code: " + token.getError());
System.out.println("error_desc: " + token.getError_description());
}
}
/**
* 服务端(企业)获取授权URL
*/
private void serverOAuthCodeTest() {
String redirect_uri = "https://localhost:8899";
String scope = "all";
String state = "xyz";
String authUrl = client.getAuthUrl(redirect_uri, scope, state);
System.out.println(authUrl);
}
/**
* 授权码(企业)模式获取Token
*/
private void serverTokenTest() {
String autoCode = "XXXXXXXXXXXX";
String redirect_uri = "https://localhost:8899/demo";
Token token = client.getTokenByCode(autoCode, redirect_uri);
if (token.isSuccess()) {
setTokenInfo(token);
System.out.println(token);
} else {
System.out.println("error_code: " + token.getError());
System.out.println("error_desc: " + token.getError_description());
}
}
/**
* 授权码(企业)模式刷新Token
*/
private void serverRefreshTokenTest() {
String refreshTokenStr = "XXXXXXXXXXX";
Token token = client.getTokenByRefreshToken(getToken().getRefreshToken());
if (token.isSuccess()) {
setTokenInfo(token);
System.out.println(token);
} else {
System.out.println("error_code: " + token.getError());
System.out.println("error_desc: " + token.getError_description());
}
}
/**
* 授权码(企业)模式获取OpenId
*/
private void serverOpenIdTest() {
String autoCode = "XXXXXXXXXXXX";
String redirect_uri = "https://localhost:8899/demo";
OpenId openId = client.getOpenIdByCode(autoCode, redirect_uri);
if (openId.isSuccess()) {
System.out.println(openId);
} else {
System.out.println("error_code: " + openId.getError());
System.out.println("error_desc: " + openId.getError_description());
}
}
private void testService() throws ServiceException {
//UserService userService = new UserService(config, getToken());
//long userId = userService.getUser().getUserId();
//System.out.println("userId: " + userId);
ProductService productService = new ProductService(config, getToken());
OCategory category = productService.createCategory(987771L, "蛋炒饭", "一道美味的炒饭");
System.out.println(category.getId());
}
/**
* 已获取Token信息后使用
*
* @return
*/
private static Token getToken() {
String access_token = PropertiesUtils.getPropValueByKey("access_token");
String token_type = PropertiesUtils.getPropValueByKey("token_type");
String expires_in = PropertiesUtils.getPropValueByKey("expires_in");
String refresh_token = PropertiesUtils.getPropValueByKey("refresh_token");
if (access_token.isEmpty()) {
System.out.println("access_token is null");
return null;
}
Token token = new Token();
token.setAccessToken(access_token);
token.setTokenType(token_type);
token.setExpires(Long.valueOf(expires_in));
token.setRefreshToken(refresh_token);
return token;
}
private static void setTokenInfo(Token token) {
if (null != token && token.isSuccess()) {
tokenMap.put("access_token", token.getAccessToken());
tokenMap.put("token_type", token.getTokenType());
tokenMap.put("expires_in", String.valueOf(token.getExpires()));
tokenMap.put("refresh_token", token.getRefreshToken());
PropertiesUtils.setProps(tokenMap);
}
}
/**
* 上传视频demo
*
* @param token 请求接口获取上传token
* @param videoFile 本地视频文件
*/
private static void testVideoUpload(String token, File videoFile) {
try {
EfsMediaClient client = new EfsMediaClient(token);
String videoId = client.uploadVideo(videoFile);
System.out.println(videoId);
} catch (ServiceException e) {
e.printStackTrace();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy