package org.jeecg.modules.ysjpt.getdata;
import java.io.IOException;
import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
@Slf4j
public class Test {
public static String account="";
public static String password= "";
public static String appId="";
public static String appKey="";
public static String state="OwO";
public static String redirect_uri= "ssw";
public static String auth_base_url= "https://aiot-oauth2.aqara.cn/";
public static String authorize_url= "authorize";
public static String token_url= "access_token";
public static String grant_type="refresh_token";
public static String refresh_token= "";
public static String api_query_base_url= "https://aiot-open-3rd.aqara.cn/3rd/v1.0/";
public static String api_query_pos_url= "open/position/query";
public static String api_query_dev_url= "open/device/query";
public static String api_query_resource_history_url= "open/resource/history/query";
public static String api_query_resource_url="open/resource/query";
public static String response_type="code";
public static void main(String[] args) {
//1、client_id,response_type,redirect_uri拼接url后
String url=auth_base_url+authorize_url+"?client_id="+appId+"&response_type="+response_type+"&redirect_uri="+redirect_uri;
//2、account和password放在body表单(x-www-form-urlencoded)中
HashMap<String,String> param=new HashMap<String, String>();
param.put("account",account);
param.put("password",password);
String result=doPost(url,param);
System.out.print("code=====>"+result+"<=======");
}
/**
* post请求
* @param url
* @param param
* @return String
*/
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
// 从从定向地址中截取code
if (response.getStatusLine().getStatusCode() == 302) {
Header header = response.getFirstHeader("Location");
String location = header.getValue();
resultString = location.substring(location.indexOf("=") + 1, location.length());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
HttpClient获取OAuth2.0中的code
共1个文件
java:1个
1星 需积分: 50 19 下载量 4 浏览量
2020-09-20
21:18:32
上传
评论 1
收藏 2KB ZIP 举报
温馨提示
通过httpclient post去获取,response返回码是302,返回的code放在header的Location中。 请求的时候client_id,response_type,redirect_uri,state拼接在url后面,account和password放在body表单(x-www-form-urlencoded)中
资源推荐
资源详情
资源评论
收起资源包目录
获取Oauto2授权码code.zip (1个子文件)
获取Oauto2授权码code
GetOauth2Code.java 4KB
共 1 条
- 1
资源评论
- kou|zhao2022-04-27所有oauth2.0都能用?
44830737
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功