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

Loading…
Cancel
Save