前言
上一篇“企业微信第三方应用(三)基于springboot开发(获取Ticket,auth_code)”我们讲到了如何获取suite_ticket和auth_code两个重要参数。
下面将讲解如何获取企业微信第三方应用的三种token方式
类型 | 描述 | 使用场景 |
---|---|---|
获取服务商凭证provider_access_token | 服务商的corpid,服务商的secret,在服务商管理后台可见,获取服务商凭证provider_access_token | 用于登录授权等 |
第三方应用suite_access_token | suite_id(第三方应用ID,以ww或wx开头应用id)、suite_secret(应用secret)、suite_ticket(企业微信后台推送的ticket)来获取 suite_access_token,第三方应用access_token | 用于获取第三方应用的预授权码等信息 |
授权方(企业)access_token | 企业安装第三方应用后通过授权方corpid,永久授权码permanent_code获取 | 用于获取通讯录信息等 |
获取TOKEN
上面图是获取三种token所需要的参数
1、获取服务商凭证provider_access_token
WeChatThirdTokenController:
package com.wechat.controller;
import com.wechat.service.IWeChatThirdTokenService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 第三方应用操作
*
* @author: wx
* @date: 2022/4/1
*/
@Slf4j
@RestController
@RequestMapping(value = "wechatToken")
public class WeChatThirdTokenController {
@Autowired
private IWeChatThirdTokenService weChatThirdTokenService;
/**
* 获取第三方应用凭证
*/
@ApiOperation(value = "获取第三方应用凭证")
@PostMapping(value = "getSuiteToken")
public void getSuiteToken(){
//获取第三方应用凭证
weChatThirdTokenService.getSuiteToken();
}
/**
* 服务商的token
*/
@ApiOperation(value = "服务商的token")
@PostMapping(value = "getProviderToken")
public void getProviderToken(){
//服务商的token
weChatThirdTokenService.getProviderToken();
}
/**
* 获取企业凭证
*/
@ApiOperation(value = "获取企业凭证")
@PostMapping(value = "getCorpToken")
public void getCorpToken(){
weChatThirdTokenService.getCorpToken();
}
}
折叠
IWeChatThirdTokenService:
package com.wechat.service;
/**
* 获取token
*/
public interface IWeChatThirdTokenService {
/**
* 获取第三方应用凭证
*/
void getSuiteToken();
/**
* 服务商的token
*/
void getProviderToken();
/**
* 获取企业凭证
*/
void getCorpToken();
}
接口实现类WeChatThirdTokenServiceImpl:
package com.wechat.service.impl;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.wechat.common.StringUtils;
import com.wechat.common.WeChatConstants;
import com.wechat.common.WeChatUtils;
import com.wechat.common.cache.CacheData;
import com.wechat.entity.wechat.WeChatProviderAccessToken;
import com.wechat.entity.wechat.WeChatReturn;
import com.wechat.entity.wechat.WeChatSuiteReturn;
import com.wechat.service.IWeChatThirdTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 获取token
*/
@Slf4j
@Service
public class WeChatThirdTokenServiceImpl implements IWeChatThirdTokenService {
/**
* 获取第三方应用凭证
*/
@Override
public void getSuiteToken() {
// 获取第三方应用凭证url
String suiteTokenUrl = WeChatUtils.THIRD_BUS_WECHAT_SUITE_TOKEN;
// 第三方应用access_token
String suiteToken = "";
try {
Map<String,Object> map = new HashMap<>();
//以ww或wx开头应用id
map.put("suite_id", WeChatConstants.SUITE_ID);
//应用secret
map.put("suite_secret", WeChatConstants.SUITE_SECRET);
//企业微信后台推送的ticket
map.put("suite_ticket", CacheData.get(WeChatConstants.SUITE_TICKET));
log.debug("getSuiteToken获取第三方应用凭证url入参:"+ JSONUtil.toJsonStr(map));
String body = HttpRequest.post(suiteTokenUrl).body(JSONUtil.toJsonStr(map), ContentType.JSON.getValue()).execute().body();
log.debug("getSuiteToken获取第三方应用凭证出参:"+JSONUtil.toJsonStr(body));
WeChatSuiteReturn weChat = JSONUtil.toBean(body, WeChatSuiteReturn.class);
log.debug("getSuiteToken获取第三方应用凭证出参转换成bea:"+JSONUtil.toJsonStr(weChat));
if(weChat.getErrcode() == null || weChat.getErrcode() == 0){
suiteToken = weChat.getSuite_access_token();
CacheData.put(WeChatConstants.SUITE_TOKEN, suiteToken);
}
// 打印消息
log.debug("获取suite token成功:"+suiteToken);
} catch (Exception e) {
log.debug("获取suite token失败errcode:"+suiteToken);
throw new RuntimeException();
}
}
/**
* 服务商的token
*/
@Override
public void getProviderToken() {
// 服务商的secret,在服务商管理后台可见
String providerSecret = WeChatConstants.PROVIDER_SECRET;
// 服务商的corpid
String corpId = WeChatConstants.CORP_ID;
// 获取服务商的tokenurl
String providerTokenUrl = WeChatUtils.THIRD_BUS_WECHAT_GET_PROVIDER_TOKEN;
String providerAccessToken = null;
try {
Map<String, Object> map = new HashMap<>();
map.put("corpid", corpId);
map.put("provider_secret", providerSecret);
log.debug("getProviderToken入参:"+ JSONUtils.toJSONString(map));
String body = HttpRequest.post(providerTokenUrl).body(JSONUtil.toJsonStr(map), ContentType.JSON.getValue()).execute().body();
log.debug("getProviderToken出参"+body);
WeChatProviderAccessToken weChat = JSONUtil.toBean(body, WeChatProviderAccessToken.class);
if(weChat.getErrcode() == null || weChat.getErrcode() == 0){
providerAccessToken = weChat.getProvider_access_token();
CacheData.put("PROVIDER_ACCESS_TOKEN",providerAccessToken);
}
// 打印消息
log.debug("获取providerAccessTokenn成功:"+ providerAccessToken);
} catch (Exception e) {
log.error("获取providerAccessToken失败:"+ providerAccessToken);
throw new RuntimeException();
}
}
/**
* 如果企业凭证到期后
* 根据授权方corpid,企业永久码获取获取企业凭证
*/
@Override
public void getCorpToken() {
log.debug("获取企业凭证getCorpToken==========start============");
//永久码
String permanentCode = (String)CacheData.get(WeChatConstants.