1.获取code码,调用getCode,会打开网址
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import top.yokey.svapi.utils.Config;
import java.util.HashMap;
import java.util.Map;
public class DyGrant {
static String host = "https://open.douyin.com";
public static void getCode(){
String url = host +"/platform/oauth/connect?client_key="+ Config.getValue("dy_client_ID") +"&response_type=code&scope="+Config.getValue("dy_auth_scope") +"&redirect_uri="+Config.getValue("dy_auth_redirect_url");
BaseHttp.openUrl(url);
}
public static String getAccessToken(){
String url = host +"/oauth/access_token?client_key="+ Config.getValue("dy_client_ID") +"&client_secret="+Config.getValue("dy_client_SERCRET")+"&code="+Config.getValue("dy_auth_code")+"&grant_type=authorization_code";
String code = BaseHttp.get(url);
System.out.println(code);
Map returnMap = (Map) JSONObject.parse(code);
Map data = (Map) returnMap.get("data");
String access_token = (String) data.get("access_token");
String open_id = (String)data.get("open_id");
String refresh_token = (String)data.get("refresh_token");
String refresh_expires_in = data.get("refresh_expires_in") + "";
String scope = data.get("scope") + "";
if(access_token != null){
Config.updateProperties("dy_auth_accessToken", access_token);
Config.updateProperties("dy_auth_openId", open_id);
Config.updateProperties("dy_auth_refreshToken", refresh_token);
Config.updateProperties("dy_refresh_expires_in", refresh_expires_in);
Config.updateProperties("dy_auth_scope", scope);
}
return code;
}
public static String eventUpdate(){
String url = host +"/event/status/update//?open_id="+ Config.getValue("dy_client_ID") +"&access_token="+Config.getValue("dy_auth_accessToken");
Map textMap = new HashMap();
textMap.put("text", "1111");
String params = "content="+ JSONObject.toJSONString(textMap) + "&message_type=text"+"&to_user_id="+"123";
String code = BaseHttp.post(url, params,"");
System.out.println(code);
return code;
}
public static String getClientToken(){
String url = host +"/oauth/client_token/?client_key="+ Config.getValue("dy_client_ID") +"&client_secret="+Config.getValue("dy_client_SERCRET")+"&grant_type=client_credential";
String code = BaseHttp.get(url);
System.out.println(code);
Map returnMap = (Map) JSONObject.parse(code);
Map data = (Map) returnMap.get("data");
String access_token = (String) data.get("access_token");
if(access_token != null){
Config.updateProperties("dy_auth_clientToken", access_token);
}
return code;
}
public static String sendMsg(){
String url = host +"/enterprise/im/message/send?open_id="+ Config.getValue("dy_auth_openId") +"&access_token="+Config.getValue("dy_auth_accessToken");
System.out.println(url);
Map textMap = new HashMap();
textMap.put("text", "12331");
Map paramsMap = new HashMap();
textMap.put("content", JSON.toJSONString(textMap));
textMap.put("message_type", "text");
textMap.put("to_user_id", "a069e0b4-6465-4d53-b810-2ac2a79b6d19");
JSONObject jsonObj=new JSONObject(textMap);
String params = jsonObj.toJSONString();
// "content={\\\"text\\\": \\\"文本内容\\\"}&message_type=text"+"&to_user_id=a069e0b4-6465-4d53-b810-2ac2a79b6d19";
System.out.println(params);
String code = BaseHttp.post(url, params,"application/json; charset=utf-8");
System.out.println(code);
return code;
}
public static String eventStatus(){
String url = host +"/event/status/list?access_token="+Config.getValue("dy_auth_clientToken");
String code = BaseHttp.get(url);
System.out.println(code);
return code;
}
public static String getFanList(){
String url = host +"/fans/list?open_id="+ Config.getValue("dy_auth_openId") +"&access_token="+Config.getValue("dy_auth_accessToken")+"&count=10";
String code = BaseHttp.get(url);
System.out.println(code);
return code;
}
public static void main(String[] args) {
// getCode();
// getAccessToken();
// getClientToken();
// eventStatus();
// sendMsg();
// getFanList();
}
}
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import okhttp3.*;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.BufferedReader;
import java.net.URI;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class BaseHttp {
public static void openUrl(String url) {
Desktop desktop = Desktop.getDesktop();
if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
URI uriConn = new URI(url);
desktop.browse(uriConn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String get(String url) {
try {
//获取签名
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
String string = response.body().string();
return string;
} catch (Exception e) {
ErrorBean errorBean = new ErrorBean();
errorBean.setMsg(e.getMessage());
return new Gson().toJson(errorBean);
}
}
public static String post(String url, String params, String type) {
try {
String requestType = "application/x-www-form-urlencoded; charset=utf-8";
if(type!=null && type!=""){
requestType = type;
}
OkHttpClient okHttpClient = new OkHttpClient();
MediaType mediaType = MediaType.parse(requestType);
RequestBody requestBody = FormBody.create(mediaType, params);
Request request = new Request.Builder()
.post(requestBody)
.url(url)
.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
return Objects.requireNonNull(response.body()).string();
} catch (Exception e) {
ErrorBean errorBean = new ErrorBean();
errorBean.setMsg(e.getMessage());
return new Gson().toJson(errorBean);
}
}
public static Map getRequestParams(HttpServletRequest request) {
BufferedReader bufferReader;
try {
bufferReader = new BufferedReader(request.getReader());
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = bufferReader.readLine()) != null) {
sb.append(line);
}
String decodeUrl = URLDecoder.decode(sb.toString(), "UTF-8");
Map addReptitleData = JSON.parseObject(decodeUrl, Map.class);
System.out.println(addReptitleData);
if (addReptitleData.containsKey("data")) {
String allDataParams = URLDecoder.decode((String) addReptitleData.get("data"), "UTF-8");
Map params = JSON.parseObject(allDataParams, Map.class);
return params;
}
return addReptitleData;
}catch(Exception e) {
return new HashMap();
}
}
}