【抖音|java 抖音开放平台 code token等】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 voidgetCode(){
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 StringgetAccessToken(){
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 = https://www.it610.com/article/(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 StringeventUpdate(){
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 StringgetClientToken(){
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 = https://www.it610.com/article/(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 StringsendMsg(){
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 StringeventStatus(){
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 StringgetFanList(){
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 = https://www.it610.com/article/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();
}
}}
推荐阅读
- 抖音开发|抖音开放平台入门教程之获取抖音授权,根据授权换取token,根据token调用接口示例!
- 抖音|抖音开放平台开发记录
- java|抖音开放平台, 企业号私信订阅服务 java(公开的)
- 面试|秋招已至,抓紧备一波蚂蚁金服、字节跳动、阿里等大厂面试,冲刺金九银十!!
- java|Java开发四年遇瓶颈,决心跳槽入字节,四面后成功斩获45万offer!!
- java|(Java岗面试)耗时1月最新整理了20个技术栈的大厂面试题+解析+面经!
- java|盘它!分布式+框架+微服务+性能优化等,一篇拿下架构大全!
- windows|NSSM - 将任何exe应用封装成windows服务的神器
- Jdbi3|SpringBoot中Jdbi3使用多数据源详解