背景:在企业微信工作台添加应用入口,根据点击人授权获得用户信息
简略记录一下流程:
首先要了解开发过程和开发步骤,可点击进入了解https://work.weixin.qq.com/api/doc#10028
了解之后呢,开始开发:
这里做的是网页授权步骤,因为这是嵌入手机的一个应用
第一步:进入企业微信管理后台页面,扫码进入https://work.weixin.qq.com/wework_admin/frame#apps
填写完毕后应用就创建第一步就完成了
第二步.填写必要数据
红色是需要填写的工作台的填写详见企业微信说明
写完了这些,应用端的基本就搞定了
后台的代码部分大体如下
@RequestMapping(value = "/weixinLogin",method = {RequestMethod.POST,RequestMethod.GET})
@ApiOperation(value="用户第三方登录", notes="企业微信授权登录")
public ModelAndView thridPartloginWeixin( HttpServletResponse response, HttpServletRequest request) throws Exception{
String code = request.getParameter("code");
String state =request.getParameter("state");
logger.error("*********************1***********************");
logger.error("企业微信测试—程1:code="+code+"state="+state);
logger.error("************************************************");
//获取公司凭证
String access_token = HttpUtil.doGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=公司编码&corpsecret=应用的密码","access_token");
logger.error("********************2*************************");
logger.error("企业微信测试—程2:access_token="+access_token);
logger.error("************************************************");
//获取用户凭证
String user_ticket = HttpUtil.doGet("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token="+access_token+"&code="+code,"user_ticket");
logger.error("********************3**************************");
logger.error("企业微信测试—程3:user_ticket="+user_ticket);
logger.error("************************************************");
//获取用户信息 用post请求
JSONObject obj = new JSONObject();
obj.put("user_ticket",user_ticket);
String emial = HttpUtil.doPost("https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token="+access_token,obj.toString());
logger.error("********************4**************************");
logger.error("企业微信测试—程4:最后="+emial);
logger.error("************************************************");
return new ModelAndView( new RedirectView("www.baidu.com"));
}
事情到这基本上解决了,根据自己的需求完善就好了
不好意思一直没来更新,我把后面的贴上
import net.sf.json.JSONObject;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
/**
* get请求
*
* @return
* @throws IOException
* @throws ParseException
*/
public static String doGet(String url,String pam) throws ParseException, IOException {
// try {
HttpClient client = new DefaultHttpClient();
// 发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/** 请求发送成功,并得到响应 **/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/** 读取服务器返回过来的json字符串数据 **/
String strResult = EntityUtils.toString(response.getEntity());
if (strResult != null && strResult.contains(pam)) {
JSONObject object = JSONObject.fromObject(strResult);
return object.get(pam).toString();
} else {
logger.error("获取企业微信凭证");
throw new ParseException("获取企业微信凭证:" + strResult);
}
}
// } catch (IOException e) {
// logger.error("获取微信小程序token错误:" + e.getMessage(), e);
// e.printStackTrace();
// }
return null;
}
/**
* post请求(用于请求json格式的参数)
*
* @param url
* @param params
* @return
*/
public static String doPost(String url, String params) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);// 创建httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
} else {
logger.error("请求返回:" + state + "(" + url + ")");
}
} catch (Exception e) {
logger.error("获取企业微信凭证:" + e.getMessage(), e);
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}