文章目录
获取Oauth2授权码code
官网的API文档是通过浏览器将用户重定向到AIOT OAuth 2.0服务。使用Aqara账号和密码成功登录后,会返回用户的授权码(code),也就是说把url复制到浏览器打开,然后输入账号密码,才会返回code。 网上也搜了很多文章,看到Oauth2开发社区中有大佬说可以通过httpclient post去获取,response返回码是302,返回的code放在header的Location中。 请求的时候client_id,response_type,redirect_uri,state拼接在url后面,account和password放在body表单(x-www-form-urlencoded)中,我也是根据这个思路通过代码来实现了,这里分享给大家,解决代码获取code,其他的接口调用根据API进行传参调用即可
一、准备账号密码appId等参数
代码如下(示例):没有的自己到官网注册获取
public static String account="";
public static String password= "";
public static String auth_base_url= "https://aiot-oauth2.aqara.cn/";
public static String authorize_url= "authorize";
public static String response_type="code";
public static String redirect_uri= "ssw";//回调地址可以随便填
public static String appId="";
二、将请求参数(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;
三、这里我将账号密码用Map封装传递参数
代码如下(示例):
HashMap<String,String> param=new HashMap<String, String>();
param.put("account",account);
param.put("password",password);
四、调用post请求
代码如下(示例):
String result=doPost(url,param);
System.out.print("code=====>"+result+"<=======");
五、post请求方法
代码如下(示例):
/**
* 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;
}
六、运行结果
七、附完整代码
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;
}
}