企业微信 js-sdk java 小结

该博客介绍企业微信第三方应用开发。给出开发文档地址,说明在服务端用Java获取access_token,可得到用户ID及手机号等隐私信息。还介绍集成企业微信js - sdk,包括初始化参数、前端引入脚本,初始化成功后可调用企业微信地图、相机等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、这是开发文档的地址 https://work.weixin.qq.com/api/doc#90001/90143/91201在这里插入图片描述二、获取 access_token 只要是第三方的应用想在企业微信中获取用户的信息,或是调用企业微信APP的功能都需要这个:
只需要在服务端(java)

		try {
		    String accessUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=自己的&corpsecret=自己的";
			RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(3000).setConnectTimeout(3000).setSocketTimeout(3000).build();
			HttpClient httpClient = HttpClients.createDefault();
			HttpGet httpget = new HttpGet(accessUrl);
			httpget.setConfig(config);
			HttpResponse httpResponse = httpClient.execute(httpget);
			String result = EntityUtils.toString(httpResponse.getEntity());
			JSONObject jsonObject = new JSONObject(result);
			String accessToken = String.valueOf(jsonObject.get("access_token"));
			System.out.println("这就是要获取的token=====>"+accessToken);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

三、得到用户的ID(企业微信id,基本就是用户的微信id)

String GET_USERINFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE"; 
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(3000).setConnectTimeout(3000).setSocketTimeout(3000).build();
HttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(get_userInfo_url);
    httpget.setConfig(config);
    try {
    	HttpResponse httpResponse = httpClient.execute(httpget);
    	String userInfo = EntityUtils.toString(httpResponse.getEntity());
    	JSONObject jsonObject = new JSONObject(userInfo);
    	Object userId = jsonObject.get("UserId");
    	System.out.println("这就是用户的微信id ===============>"+userId );
   	} catch (Exception e) {
		e.printStackTrace();
	}

四、根据上面的token与userid可以获取用户的手机号等隐私信息

 try{
    	String urlNameString = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
        HttpGet request = new HttpGet(urlNameString);
    	HttpResponse response2 = httpClient.execute(request);
    	String result = EntityUtils.toString(response2.getEntity());
    	System.out.println("这就是用户的隐私数据 ===============>"+result );
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	}

五、集成企业微信的 js-sdk
文档地址 https://work.weixin.qq.com/api/doc#90001/90144/90547
在这里插入图片描述
(1)初始化参数

    Map<String, String> map = new HashMap<String, String>();
	String ticket_url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token="+accessToken;
	HttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(ticket_url);
  	RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(3000).setConnectTimeout(3000).setSocketTimeout(3000).build();
  	httpget.setConfig(config);
  	try {
		HttpResponse httpResponse = httpClient.execute(httpget);
		String result = EntityUtils.toString(httpResponse.getEntity());
    	JSONObject jsonObject = new JSONObject(result);
    	String ticket = String.valueOf(jsonObject.get("ticket"));
    	String noncestr = UUIDUtils.getUUID().replace("-", "").substring(0, 16);
    	String timestamp = String.valueOf(System.currentTimeMillis()/1000);    	
    	String str = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url;MessageDigest digest = MessageDigest.getInstance("SHA-1");
    	digest.update(str.getBytes());
    	byte messageDigest[] = digest.digest();
    	StringBuffer hexString = new StringBuffer();
    	for (int i = 0; i < messageDigest.length; i++) {
    		String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
    		if (shaHex.length() < 2) {
    			hexString.append(0);
    		}
    		hexString.append(shaHex);
    	}
    	map.put("timestamp", timestamp);
    	map.put("noncestr", noncestr);
    	map.put("signature", hexString.toString());
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} 

(2)前端页面需要拿到上面返回的参数

在页面中引入script:http://res.wx.qq.com/open/js/jweixin-1.2.0.js

wx.config({
	beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
	debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
	appId: '自己的', // 必填,企业微信的corpID
	timestamp: data.timestamp, // 必填,生成签名的时间戳
	nonceStr: data.noncestr, // 必填,生成签名的随机串
	signature: data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
	jsApiList: ['openLocation', 'getLocation'] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
});

wx.ready(function(){
	console.log("初始化成功......");
});
wx.error(function(res){
	console.log("初始化失败......");
});

(3)初始化也成功之后调用企业微信中的地图

wx.getLocation({
	type : 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
	success : function(res) {
		//使用微信内置地图查看位置接口
		wx.openLocation({
			latitude : res.latitude, // 纬度,浮点数,范围为90 ~ -90
			longitude : res.longitude, // 经度,浮点数,范围为180 ~ -180。
			name : '我的位置', // 位置名
			address : '329创业者社区', // 地址详情说明
			scale : 28, // 地图缩放级别,整形值,范围从1~28。默认为最大
			infoUrl : 'http://www.gongjuji.net' // 在查看位置界面底部显示的超链接,可点击跳转(测试好像不可用)
		});
	},
	cancel : function(res) {}
});

类似的可以调用企业微信的相机等等…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值