Browse Source

微博推送

develop
wangsixiang 8 months ago
parent
commit
639c465ddf
  1. 33
      zc-business/src/main/java/com/zc/business/controller/WeiboAuthUtil.java

33
zc-business/src/main/java/com/zc/business/controller/WeiboAuthUtil.java

@ -15,6 +15,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
/** /**
* @author 王思祥 * @author 王思祥
* @ClassName DcWarningPush 微博推送 * @ClassName DcWarningPush 微博推送
@ -23,29 +24,29 @@ import java.net.URLEncoder;
public class WeiboAuthUtil { public class WeiboAuthUtil {
//1.登录传入重定向的url,用户授权后返回授权的code,2.使用code取得认证权限token 3.调用接口参数 //1.登录传入重定向的url,用户授权后返回授权的code,2.使用code取得认证权限token 3.调用接口参数
//1.登录获取用户的回调code //1.登录获取用户的回调code
private static final String APP_KEY = "你的App Key"; private static final String APP_KEY = "1894516689";
private static final String AUTH_URL = "https://api.weibo.com/oauth2/authorize"; private static final String APP_SECRET = "4e89660243b70328fb74ae10f9ed98e5";
private static final String APP_SECRET = "你的App Secret"; private static final String REDIRECT_URI = "https://api.weibo.com/oauth2/default.html";//授权回调地址
private static final String REDIRECT_URI = "你的回调URL";
//获取授权后的code //获取授权后的code
public String tokenCode(){ public String tokenCode(){
String url="https://api.weibo.com/oauth2/authorize?client_id="+APP_KEY+"&redirect_uri="+AUTH_URL; String url="https://api.weibo.com/oauth2/authorize?client_id="+APP_KEY+"&redirect_uri="+REDIRECT_URI+"&response_type=code";
String token=null; String accessToken=null;
com.alibaba.fastjson.JSONObject jsonObj = null;
try { try {
URL urlGet = new URL(url); //创建链接 URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); http.setRequestMethod("GET"); // 必须是get方式请求
http.setDoInput(true); //打开获取返回数据 http.setDoInput(true);
http.connect(); //发送链接 http.connect();
InputStream is = http.getInputStream(); // InputStream is = http.getInputStream();
int size = is.available(); int size = is.available();
byte[] jsonBytes = new byte[size]; byte[] jsonBytes = new byte[size];
is.read(jsonBytes); is.read(jsonBytes);
token = new String(jsonBytes, "UTF-8"); accessToken = new String(jsonBytes, "UTF-8");
System.err.println(token); System.err.println(accessToken);
JSONObject jsonObject = JSONObject.parseObject(token); jsonObj = com.alibaba.fastjson.JSONObject.parseObject(accessToken);
is.close(); is.close();
return jsonObject.get("code").toString(); return jsonObj.get("code").toString();
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
@ -74,7 +75,7 @@ public class WeiboAuthUtil {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
WeiboAuthUtil weiboAuthUtil = new WeiboAuthUtil(); WeiboAuthUtil weiboAuthUtil = new WeiboAuthUtil();
String code = weiboAuthUtil.tokenCode(); String code = weiboAuthUtil.tokenCode();
String accessToken = weiboAuthUtil.token(code);//认证后的code放入,获取token String accessToken = weiboAuthUtil.token("1223e0dbc177fe5a20ba1cc2f3c444d5");//认证后的code放入,获取token
// 创建HttpClient实例 // 创建HttpClient实例
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// 构建请求URL,这里以获取用户信息为例 // 构建请求URL,这里以获取用户信息为例

Loading…
Cancel
Save