Popular blog tags

HttpClient获取Aqara Developer Platform oAuth2.0中的code

Published

Quick Start

Register and login developer account in Aqara developer platform

Create a Project  with developer account in Aqara developer platform

Aqara home app account authorization

 

https://developer.aqara.com/

API Usage Guide

https://opendoc.aqara.cn/en/docs/developmanual/apiIntroduction/APIUsageGuide.html

 

China server API Domain

open-cn.aqara.com

https://opendoc.aqara.com/en/docs/developmanual/apiIntroduction/APIUsageGuide.html

 

 


 

获取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="";
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、将请求参数(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;
 
  • 1

三、这里我将账号密码用Map封装传递参数

代码如下(示例):

 HashMap<String,String> param=new HashMap<String, String>();
 param.put("account",account);
 param.put("password",password);
 
  • 1
  • 2
  • 3

四、调用post请求

代码如下(示例):

String result=doPost(url,param);
System.out.print("code=====>"+result+"<=======");
 
  • 1
  • 2

五、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;
    }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

六、运行结果

在这里插入图片描述


七、附完整代码

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;
    }

}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

https://blog.csdn.net/weixin_44830737/article/details/108630687