wangsixiang
7 months ago
11 changed files with 412 additions and 8 deletions
@ -0,0 +1,95 @@ |
|||
package com.zc.business.controller; |
|||
|
|||
|
|||
import com.alibaba.fastjson.JSONObject; |
|||
import org.apache.http.HttpEntity; |
|||
import org.apache.http.HttpResponse; |
|||
import org.apache.http.client.HttpClient; |
|||
import org.apache.http.client.methods.CloseableHttpResponse; |
|||
import org.apache.http.client.methods.HttpGet; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.impl.client.HttpClients; |
|||
import org.apache.http.util.EntityUtils; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
import java.net.URLEncoder; |
|||
/** |
|||
* @author 王思祥 |
|||
* @ClassName DcWarningPush 微博推送 |
|||
*/ |
|||
|
|||
public class WeiboAuthUtil { |
|||
//1.登录传入重定向的url,用户授权后返回授权的code,2.使用code取得认证权限token 3.调用接口参数
|
|||
//1.登录获取用户的回调code
|
|||
private static final String APP_KEY = "你的App Key"; |
|||
private static final String AUTH_URL = "https://api.weibo.com/oauth2/authorize"; |
|||
private static final String APP_SECRET = "你的App Secret"; |
|||
private static final String REDIRECT_URI = "你的回调URL"; |
|||
//获取授权后的code
|
|||
public String tokenCode(){ |
|||
String url="https://api.weibo.com/oauth2/authorize?client_id="+APP_KEY+"&redirect_uri="+AUTH_URL; |
|||
String token=null; |
|||
try { |
|||
URL urlGet = new URL(url); //创建链接
|
|||
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); |
|||
http.setRequestMethod("GET"); |
|||
http.setDoInput(true); //打开获取返回数据
|
|||
http.connect(); //发送链接
|
|||
InputStream is = http.getInputStream(); //
|
|||
int size = is.available(); |
|||
byte[] jsonBytes = new byte[size]; |
|||
is.read(jsonBytes); |
|||
token = new String(jsonBytes, "UTF-8"); |
|||
System.err.println(token); |
|||
JSONObject jsonObject = JSONObject.parseObject(token); |
|||
is.close(); |
|||
return jsonObject.get("code").toString(); |
|||
}catch (Exception e){ |
|||
e.printStackTrace(); |
|||
} |
|||
return ""; |
|||
} |
|||
//获取toke
|
|||
public String token(String code)throws IOException { |
|||
HttpClient httpClient = HttpClients.createDefault(); |
|||
String tokenUrl = REDIRECT_URI + "?client_id=" + APP_KEY |
|||
+ "&client_secret=" + APP_SECRET |
|||
+ "&grant_type=authorization_code" |
|||
+ "&code=" + code |
|||
+ "&redirect_uri=" + URLEncoder.encode(REDIRECT_URI, "UTF-8"); |
|||
HttpGet httpGet = new HttpGet(tokenUrl); |
|||
HttpResponse response = httpClient.execute(httpGet); |
|||
HttpEntity entity = response.getEntity(); |
|||
if (entity != null) { |
|||
String responseBody = EntityUtils.toString(entity, "UTF-8"); |
|||
org.json.JSONObject jsonObject = new org.json.JSONObject(responseBody); |
|||
return jsonObject.optString("access_token"); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
//执行调用推送api
|
|||
public static void main(String[] args) throws Exception { |
|||
WeiboAuthUtil weiboAuthUtil = new WeiboAuthUtil(); |
|||
String code = weiboAuthUtil.tokenCode(); |
|||
String accessToken = weiboAuthUtil.token(code);//认证后的code放入,获取token
|
|||
// 创建HttpClient实例
|
|||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
|||
// 构建请求URL,这里以获取用户信息为例
|
|||
String url = "https://api.weibo.com/2/users/show.json?access_token=" + accessToken + "&uid=用户UID"; |
|||
// 创建HttpGet请求
|
|||
HttpGet httpGet = new HttpGet(url); |
|||
// 执行请求并获取响应
|
|||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
|||
HttpEntity entity = response.getEntity(); |
|||
if (entity != null) { |
|||
// 读取响应内容
|
|||
String responseString = EntityUtils.toString(entity, "UTF-8"); |
|||
System.out.println(responseString); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,79 @@ |
|||
package com.zc.business.domain; |
|||
|
|||
import org.apache.commons.lang3.builder.ToStringBuilder; |
|||
import org.apache.commons.lang3.builder.ToStringStyle; |
|||
import com.ruoyi.common.annotation.Excel; |
|||
import com.ruoyi.common.core.domain.BaseEntity; |
|||
|
|||
/** |
|||
* 调度信息记录对象 dc_dispatch_resource |
|||
* |
|||
* @author ruoyi |
|||
* @date 2024-04-15 |
|||
*/ |
|||
public class DcDispatchResource extends BaseEntity |
|||
{ |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
/** $column.columnComment */ |
|||
private Long id; |
|||
|
|||
/** 1-人员2-车辆 */ |
|||
@Excel(name = "1-人员 2-车辆") |
|||
private Integer dispatchType; |
|||
|
|||
/** $column.columnComment */ |
|||
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()") |
|||
private Long resourceId; |
|||
|
|||
/** $column.columnComment */ |
|||
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()") |
|||
private Long dispatchId; |
|||
|
|||
public void setId(Long id) |
|||
{ |
|||
this.id = id; |
|||
} |
|||
|
|||
public Long getId() |
|||
{ |
|||
return id; |
|||
} |
|||
public void setDispatchType(Integer dispatchType) |
|||
{ |
|||
this.dispatchType = dispatchType; |
|||
} |
|||
|
|||
public Integer getDispatchType() |
|||
{ |
|||
return dispatchType; |
|||
} |
|||
public void setResourceId(Long resourceId) |
|||
{ |
|||
this.resourceId = resourceId; |
|||
} |
|||
|
|||
public Long getResourceId() |
|||
{ |
|||
return resourceId; |
|||
} |
|||
public void setDispatchId(Long dispatchId) |
|||
{ |
|||
this.dispatchId = dispatchId; |
|||
} |
|||
|
|||
public Long getDispatchId() |
|||
{ |
|||
return dispatchId; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) |
|||
.append("id", getId()) |
|||
.append("dispatchType", getDispatchType()) |
|||
.append("resourceId", getResourceId()) |
|||
.append("dispatchId", getDispatchId()) |
|||
.toString(); |
|||
} |
|||
} |
Loading…
Reference in new issue