1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 物联网萤石云获取登录的accessToken工具类

物联网萤石云获取登录的accessToken工具类

时间:2024-05-05 07:57:53

相关推荐

物联网萤石云获取登录的accessToken工具类

刚刚接触物联网,萤石云平台,编写萤石云获取登录的accessToken工具类

我接手写公司物联网的模块,入坑不少,主要不知道有哪些东西要写。

总结流程:

1.先边设备调试好(摄像头),在手机上看到画面

1.1获取获取appKey

萤石云开放平台appKey申请可在开放平台官网注册登录/view/app/app_edit.html(官网),填写正确的信息后开放平台会发放到您的账号下。appKey和appSecret是开放平台应用的秘钥,请不要请泄露,如若泄露可将appSecret重置即可。

1.2手机上下载萤石云视频app

1.3如果设备是二手的(别人使用过),还需要修改设备的IP地址

1.4电脑下载设备网络搜索

1.4.1 百度网盘下载:/s/1HNC8l2Lvh5_4-OMO_8LWPQ

提取码:6j4u

1.4.2加QQ群:829336169 下载:SADPToo V3.0.2.4.rar压缩包(我习惯工作上使用的软件都会上传到这个群,下次下载就直接下载速度快)

1.5安装设备网络搜索,直接解压,直接运行,下一步就行

1.6打开设备网络搜索

修改设备的IP,(查询自己电脑的IP:打开cmd命令:ipconfig,如果不会我以前有写过这文章)

1.7,登录萤石云视频app,添加设备扫一扫就可以添加成功了

1.8,自己可以看到画面

2.现在开始写Java代码了(开心 高兴)

2.1萤石云官方文档:/doc/zh/book/index/user.html

2.1获取登录的accessToken

package com.smartfarm.base.monitor.core.util;import java.io.IOException;import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;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.HttpClientBuilder;import org.apache.http.util.EntityUtils;/*** 获取登录的accessToken* @author lyq**/public class GetTokenUtil {public static void main(String[] args) {//我自己的密匙String AppKey="7fec057876c445f9b919ed9bb8bfd821";String Secret="afa8b6fdced65f379c13e371c9967721";Token t=getSnapUrl(AppKey, Secret);System.out.println(t.getData().getAccessToken());System.out.println(t.getData().getExpireTime());}public static Token getSnapUrl(String AppKey,String Secret){// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)CloseableHttpClient httpClient = HttpClientBuilder.create().build();// 封装参数StringBuffer params = new StringBuffer();params.append("appKey="+ AppKey);params.append("&");params.append("appSecret="+Secret);// 创建Post请求HttpPost httpPost = new HttpPost("/api/lapp/token/get" + "?" + params);// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)httpPost.setHeader("Content-Type", "application/json;charset=utf8");// 响应模型CloseableHttpResponse response = null;try {// 由客户端执行(发送)Post请求response = httpClient.execute(httpPost);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();if (responseEntity != null) {//返回数据String responseString = EntityUtils.toString(responseEntity);//生成实体类(responseString就可以看到数据的)JSONObject jsonObject = JSONObject.fromObject(responseString);Token t = (Token) JSONObject.toBean(jsonObject, Token.class);return t;}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}return null;}}

2.3获取获取设备状态信息

package com.smartfarm.base.monitor.core.util;import java.io.IOException;import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;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.HttpClientBuilder;import org.apache.http.util.EntityUtils;/*** 获取设备状态信息* @author lyq**/public class SnapUtil {public static void main(String[] args) {//我自己的密匙String AppKey="7fec057876c445f9b919ed9bb8bfd821";String Secret="afa8b6fdced65f379c13e371c9967721";String accessToken="at.60haqycfbqoizem15csgx1q21lmn85q6-4jmaids8bm-130mqh2-iamhp5emw";//获取登录的accessToken//String accessToken=GetTokenUtil.getSnapUrl(AppKey,Secret).getData().getAccessToken();int channel=1;String deviceSerial="C16499527";System.out.println(stateInformation(accessToken, deviceSerial,channel));}/** 获取设备状态信息* accessTokenString授权过程获取的access_tokenY* deviceSerialString设备序列号,存在英文字母的设备序列号,字母需为大写Y* channelint通道号,默认为1N* @param accessToken* @param deviceSerial* @return*/public static String stateInformation(String accessToken,String deviceSerial,int channel){// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)CloseableHttpClient httpClient = HttpClientBuilder.create().build();// 封装参数StringBuffer params = new StringBuffer();params.append("accessToken="+ accessToken);params.append("&");params.append("deviceSerial="+deviceSerial);params.append("&");params.append("channel="+channel);// 创建Post请求HttpPost httpPost = new HttpPost("/api/lapp/device/status/get" + "?" + params);// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)httpPost.setHeader("Content-Type", "application/json;charset=utf8");// 响应模型CloseableHttpResponse response = null;try {// 由客户端执行(发送)Post请求response = httpClient.execute(httpPost);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();if (responseEntity != null) {String responseString = EntityUtils.toString(responseEntity);JSONObject jsonObject = JSONObject.fromObject(responseString);Picture p = (Picture) JSONObject.toBean(jsonObject, Picture.class);return p.getCode();}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}return null;}}

现在后台就可以操控设备了

如果有帮助就关注一下,没有帮助的就不强求,

滴水之恩,当涌泉相报,那是你没有绝望,

跳槽工作了3个星期了,星期一到星期五加班到7点,回到家9点

星期六星期日,不是自己就是学新的东西,

能不裸辞,就尽量不要裸辞了,裸辞之后,可能还没有上一家好,

我在以前的公司6点准时走 哈哈哈

加油干!!! 死刚Java

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。