新增OauthApi授权接口

This commit is contained in:
jinyu 2015-03-06 22:10:14 +08:00
parent d8d294fea9
commit fa06dca406
12 changed files with 188 additions and 232 deletions

View File

@ -172,4 +172,8 @@
* 2015-02-05
+ release 1.2
+ release 1.2
* 2015-03-06
+ 新增oauth授权接口

View File

@ -170,4 +170,8 @@ weixin4j-mp
* 2015-01-31
+ **weixin4j-mp-api**: 新增数据分析接口
+ **weixin4j-mp-api**: 新增数据分析接口
* 2015-03-06
+ **weixin4j-mp-api**: 新增oauth授权接口

View File

@ -33,20 +33,23 @@ weixin4j-mp-api
* DataApi `数据统计API`
* OauthApi `oauth授权API`
如何使用
--------
1.API工程可以单独打包到其他项目中使用,需新增或拷贝`weixin.properties`文件到项目的`classpath`
weixin.properties说明
| 属性名 | 说明 |
| :---------- | :-------------- |
| account | 微信公众号信息 `json格式` |
| token_path | 使用FileTokenHolder时token保存的物理路径 |
| qr_path | 调用二维码接口时保存二维码图片的物理路径 |
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
| bill_path | 调用下载对账单接口保存excel文件的物理路径 |
| ca_file | 调用某些接口(支付相关)强制需要auth的ca授权文件 |
| 属性名 | 说明 |
| :---------- | :-------------- |
| account | 微信公众号信息 `json格式` |
| token_path | 使用FileTokenHolder时token保存的物理路径 |
| qr_path | 调用二维码接口时保存二维码图片的物理路径 |
| media_path | 调用媒体接口时保存媒体文件的物理路径 |
| bill_path | 调用下载对账单接口保存excel文件的物理路径 |
| ca_file | 调用某些接口(支付相关)强制需要auth的ca授权文件 |
| redirect_uri | 调用OauthApi接口时需要填写的重定向路径 |
示例(properties中换行用右斜杆\\)
@ -66,6 +69,8 @@ weixin.properties说明
# ca证书存放的完整路径 (V2版本后缀为*.pfx,V3版本后缀为*.p12)
ca_file=/tmp/weixin/xxxxx.p12
#classpath路径下:ca_file=classpath:xxxxx.p12
redirect_uri=http://xxx
2.实例化一个`WeixinProxy`对象,调用API,需要强调的是如果只传入appid,appsecret两个参数将无法调用支付相关接口
@ -149,4 +154,8 @@ weixin.properties说明
* 2015-01-31
+ 新增数据分析接口
+ 新增数据分析接口
* 2015-03-06
+ 新增oauth授权接口

View File

@ -25,7 +25,6 @@ import com.foxinmy.weixin4j.mp.model.CustomRecord;
import com.foxinmy.weixin4j.mp.model.Following;
import com.foxinmy.weixin4j.mp.model.Group;
import com.foxinmy.weixin4j.mp.model.KfAccount;
import com.foxinmy.weixin4j.mp.model.OauthToken;
import com.foxinmy.weixin4j.mp.model.QRParameter;
import com.foxinmy.weixin4j.mp.model.SemQuery;
import com.foxinmy.weixin4j.mp.model.SemResult;
@ -525,40 +524,6 @@ public class WeixinProxy {
return massApi.getMassNews(msgId);
}
/**
* oauth授权code换取token
*
* @param code
* 用户授权后返回的code
* @return token对象
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">获取用户token</a>
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @see com.foxinmy.weixin4j.mp.api.UserApi
*/
public OauthToken getOauthToken(String code, String appid, String appsecret)
throws WeixinException {
return userApi.getOauthToken(code, appid, appsecret);
}
/**
* ouath获取用户信息
*
* @param token
* 授权票据
* @return 用户对象
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">拉取用户信息</a>
* @see com.foxinmy.weixin4j.mp.model.User
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @see com.foxinmy.weixin4j.mp.api.UserApi
*/
public User getUser(OauthToken token) throws WeixinException {
return userApi.getUser(token);
}
/**
* 获取用户信息
*

View File

@ -11,7 +11,6 @@ import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.model.Following;
import com.foxinmy.weixin4j.mp.model.OauthToken;
import com.foxinmy.weixin4j.mp.model.User;
import com.foxinmy.weixin4j.mp.type.Lang;
import com.foxinmy.weixin4j.token.TokenHolder;
@ -33,51 +32,6 @@ public class UserApi extends MpApi {
this.tokenHolder = tokenHolder;
}
/**
* oauth授权code获取token
*
* @param code
* 用户授权后返回的code
* @param appid
* @param appsecret
* @return token对象
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">获取用户token</a>
* @see com.foxinmy.weixin4j.mp.model.OauthToken
*/
public OauthToken getOauthToken(String code, String appid, String appsecret)
throws WeixinException {
String user_token_uri = getRequestUri("sns_user_token_uri");
Response response = request.get(String.format(user_token_uri, appid,
appsecret, code));
return response.getAsObject(new TypeReference<OauthToken>() {
});
}
/**
* oauth获取用户信息
*
* @param token
* 授权票据
* @return 用户对象
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">拉取用户信息</a>
* @see com.foxinmy.weixin4j.mp.model.User
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @see {@link com.foxinmy.weixin4j.mp.api.UserApi#getOauthToken(String)}
*/
public User getUser(OauthToken token) throws WeixinException {
String user_info_uri = getRequestUri("sns_user_info_uri");
Response response = request.get(String.format(user_info_uri,
token.getAccessToken(), token.getOpenid()));
return response.getAsObject(new TypeReference<User>() {
});
}
/**
* 获取用户信息
*

View File

@ -16,7 +16,10 @@ tenpay_gw_base_url=https://gw.tenpay.com
# \u7f51\u9875\u6388\u6743\u83b7\u53d6\u7528\u6237\u4fe1\u606f
user_auth_uri=https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect
sns_user_auth_uri=https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect
sns_user_token_uri=https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code
sns_token_refresh_uri=https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s
sns_auth_token_uri=https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s
sns_user_info_uri=https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN
# \u76f4\u63a5\u83b7\u53d6\u7528\u6237\u4fe1\u606f

View File

@ -1,6 +1,6 @@
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
# \u516c\u4f17\u53f7\u4fe1\u606f
account={"id":"wxb72a4f2dbf27b59d","secret":"7ecc93fa8662f518a39c037fd599e1f5",\
account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
"token":"\u5f00\u653e\u8005\u7684token",\
"encodingAesKey":"\u516c\u4f17\u53f7\u8bbe\u7f6e\u4e86\u52a0\u5bc6\u65b9\u5f0f\u4e14\u4e3a\u300c\u5b89\u5168\u6a21\u5f0f\u300d\u65f6\u9700\u8981\u586b\u5165",\
"mchId":"V3.x\u7248\u672c\u4e0b\u7684\u5fae\u4fe1\u5546\u6237\u53f7 \u670d\u52a1\u53f7\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\

View File

@ -57,6 +57,20 @@ public class MenuTest extends TokenTest {
Assert.assertEquals(0, result.getCode());
}
@Test
public void create1() throws WeixinException {
btnList = new ArrayList<Button>();
Button b1 = new Button("我要订餐", "ORDERING", ButtonType.click);
btnList.add(b1);
Button b2 = new Button("查询订单", "http://802.canyi.net/order/list", ButtonType.view);
btnList.add(b2);
Button b3 = new Button("最新资讯", "NEWS", ButtonType.click);
btnList.add(b3);
JsonResult result = menuApi.createMenu(btnList);
Assert.assertEquals(0, result.getCode());
}
@Test
public void get() throws WeixinException {
btnList = menuApi.getMenu();

View File

@ -25,151 +25,151 @@ import com.foxinmy.weixin4j.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.type.AccountType;
public class PayTest {
private final static WeixinPayProxy PAY2;
private final static WeixinPayProxy PAY3;
private final static WeixinMpAccount ACCOUNT2;
private final static WeixinMpAccount ACCOUNT3;
static {
ACCOUNT2 = new WeixinMpAccount(
"appid",
"appsecret",
"paysignkey",
"partnerId", "partnerKey");
PAY2 = new WeixinPayProxy(ACCOUNT2, new FileTokenHolder(
new WeixinTokenCreator(ACCOUNT2.getId(), ACCOUNT2.getSecret(),
AccountType.MP)));
ACCOUNT3 = new WeixinMpAccount("appid",
"appsecret",
"paysignkey", "mchId");
PAY3 = new WeixinPayProxy(ACCOUNT3, new FileTokenHolder(
new WeixinTokenCreator(ACCOUNT3.getId(), ACCOUNT3.getSecret(),
AccountType.MP)));
}
private final static WeixinPayProxy PAY2;
private final static WeixinPayProxy PAY3;
private final static WeixinMpAccount ACCOUNT2;
private final static WeixinMpAccount ACCOUNT3;
static {
ACCOUNT2 = new WeixinMpAccount(
"wxba294f2c6f330361",
"8e33f5371a1afea1f7bce88088cb4bba",
"gADrKITv3qYWu9JEg1NS0WPaU5yFgTwS9WfPueskfPpt3OZGpnUN1uBom36G2tP701vi2pPRJLZF9dEDFj9pqxidPn10Y91Lj8kK37Svz6S4MfeAHo9svFZmHkIKScGb",
"1221928801", "8d1b26231827a965ef54fe6a3a151551");
PAY2 = new WeixinPayProxy(ACCOUNT2, new FileTokenHolder(
new WeixinTokenCreator(ACCOUNT2.getId(), ACCOUNT2.getSecret(),
AccountType.MP)));
ACCOUNT3 = new WeixinMpAccount("wx0d1d598c0c03c999",
"2513ac683f1beabdb6b98d9ddd9e5755",
"GATFzDwbQdbbci3QEQxX2rUBvwTrsMiZ", "10020674");
PAY3 = new WeixinPayProxy(ACCOUNT3, new FileTokenHolder(
new WeixinTokenCreator(ACCOUNT3.getId(), ACCOUNT3.getSecret(),
AccountType.MP)));
}
@Test
public void orderQueryV2() throws WeixinException {
System.err.println(PAY2.orderQueryV2("D14110500021"));
}
@Test
public void orderQueryV2() throws WeixinException {
System.err.println(PAY2.orderQueryV2("D14110500021"));
}
@Test
public void refundV2() throws WeixinException {
File caFile = new File(
"/path/xxx.pfx");
IdQuery idQuery = new IdQuery("D15020300005", IdType.TRADENO);
System.err.println(PAY2.refundV2(caFile, idQuery, "1422925555037", 16d, 16d,
"partnerId", "password", null, null, null));
}
@Test
public void refundV2() throws WeixinException {
File caFile = new File(
"/Users/jy/workspace/feican/canyi-weixin-parent/canyi-weixin-service/src/main/resources/1221928801.pfx");
IdQuery idQuery = new IdQuery("D15020300005", IdType.TRADENO);
System.err.println(PAY2.refundV2(caFile, idQuery, "1422925555037", 16d, 16d,
"1221928801", "111111", null, null, null));
}
@Test
public void refundQueryV2() throws WeixinException {
System.err.println(PAY2.refundQueryV2(new IdQuery("D14123000004",
IdType.TRADENO)));
refundQueryV3();
}
@Test
public void refundQueryV2() throws WeixinException {
System.err.println(PAY2.refundQueryV2(new IdQuery("D14123000004",
IdType.TRADENO)));
refundQueryV3();
}
@Test
public void downbillV2() throws WeixinException {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2014);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DAY_OF_MONTH, 22);
File file = PAY2.downloadbill(c.getTime(), null);
System.err.println(file);
}
@Test
public void downbillV2() throws WeixinException {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2014);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DAY_OF_MONTH, 22);
File file = PAY2.downloadbill(c.getTime(), null);
System.err.println(file);
}
@Test
public void orderQueryV3() throws WeixinException {
Order order = PAY3.orderQueryV3(new IdQuery("T0002", IdType.TRADENO));
System.err.println(order);
String sign = order.getSign();
order.setSign(null);
String valiSign = PayUtil.paysignMd5(order, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void orderQueryV3() throws WeixinException {
Order order = PAY3.orderQueryV3(new IdQuery("T0002", IdType.TRADENO));
System.err.println(order);
String sign = order.getSign();
order.setSign(null);
String valiSign = PayUtil.paysignMd5(order, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void refundQueryV3() throws WeixinException {
System.err.println(PAY3.refundQueryV3(new IdQuery("T00015",
IdType.TRADENO)));
}
@Test
public void refundQueryV3() throws WeixinException {
System.err.println(PAY3.refundQueryV3(new IdQuery("T00015",
IdType.TRADENO)));
}
@Test
public void downbillV3() throws WeixinException {
Calendar c = Calendar.getInstance();
System.err.println(c.getTime());
c.set(Calendar.YEAR, 2014);
c.set(Calendar.MONTH, 9);
c.set(Calendar.DAY_OF_MONTH, 22);
System.err.println(c.getTime());
File file = PAY3.downloadbill(c.getTime(), null);
System.err.println(file);
}
@Test
public void downbillV3() throws WeixinException {
Calendar c = Calendar.getInstance();
System.err.println(c.getTime());
c.set(Calendar.YEAR, 2014);
c.set(Calendar.MONTH, 9);
c.set(Calendar.DAY_OF_MONTH, 22);
System.err.println(c.getTime());
File file = PAY3.downloadbill(c.getTime(), null);
System.err.println(file);
}
@Test
public void refundV3() throws WeixinException {
File caFile = new File(
"/path/xxx.p12");
IdQuery idQuery = new IdQuery("T00015", IdType.TRADENO);
com.foxinmy.weixin4j.mp.payment.v3.RefundResult result = PAY3.refundV3(
caFile, idQuery, "R0002", 1d, 1d, "10020674");
System.err.println(result);
String sign = result.getSign();
result.setSign(null);
String valiSign = PayUtil.paysignMd5(result, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void refundV3() throws WeixinException {
File caFile = new File(
"/Users/jy/workspace/feican/canyi-weixin-parent/canyi-weixin-service/src/main/resources/10020674.p12");
IdQuery idQuery = new IdQuery("T00015", IdType.TRADENO);
com.foxinmy.weixin4j.mp.payment.v3.RefundResult result = PAY3.refundV3(
caFile, idQuery, "R0002", 1d, 1d, "10020674");
System.err.println(result);
String sign = result.getSign();
result.setSign(null);
String valiSign = PayUtil.paysignMd5(result, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void nativeV3() throws WeixinException {
PayPackageV3 payPackageV3 = new PayPackageV3(ACCOUNT3,
"oyFLst1bqtuTcxK-ojF8hOGtLQao", "native测试", "T0001", 0.1d,
"127.0.0.1", TradeType.NATIVE);
payPackageV3.setProductId("0001");
payPackageV3.setNotifyUrl("xxxx");
PrePay prePay = null;
try {
prePay = PayUtil.createPrePay(payPackageV3,
ACCOUNT3.getPaySignKey());
} catch (PayException e) {
e.printStackTrace();
}
System.err.println(prePay);
}
@Test
public void nativeV3() throws WeixinException {
PayPackageV3 payPackageV3 = new PayPackageV3(ACCOUNT3,
"oyFLst1bqtuTcxK-ojF8hOGtLQao", "native测试", "T0001", 0.1d,
"127.0.0.1", TradeType.NATIVE);
payPackageV3.setProductId("0001");
payPackageV3.setNotifyUrl("xxxx");
PrePay prePay = null;
try {
prePay = PayUtil.createPrePay(payPackageV3,
ACCOUNT3.getPaySignKey());
} catch (PayException e) {
e.printStackTrace();
}
System.err.println(prePay);
}
@Test
public void closeOrder() throws WeixinException {
ApiResult result = PAY3.closeOrder("D111");
System.err.println(result);
String sign = result.getSign();
result.setSign(null);
String valiSign = PayUtil.paysignMd5(result, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void closeOrder() throws WeixinException {
ApiResult result = PAY3.closeOrder("D111");
System.err.println(result);
String sign = result.getSign();
result.setSign(null);
String valiSign = PayUtil.paysignMd5(result, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void shortUrl() throws WeixinException {
String url = "weixin://wxpay/bizpayurl?xxxxxx";
String shortUrl = PAY3.getPayShorturl(url);
System.err.println(shortUrl);
}
@Test
public void shortUrl() throws WeixinException {
String url = "weixin://wxpay/bizpayurl?xxxxxx";
String shortUrl = PAY3.getPayShorturl(url);
System.err.println(shortUrl);
}
@Test
public void interfaceReport() throws WeixinException {
String interfaceUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
int executeTime = 2500;
String outTradeNo = null;
String ip = "127.0.0.1";
Date time = new Date();
XmlResult returnXml = new XmlResult("SUCCESS", "");
returnXml.setResultCode("SUCCESS");
returnXml = PAY3.interfaceReport(interfaceUrl, executeTime, outTradeNo,
ip, time, returnXml);
System.err.println(returnXml);
}
@Test
public void interfaceReport() throws WeixinException {
String interfaceUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
int executeTime = 2500;
String outTradeNo = null;
String ip = "127.0.0.1";
Date time = new Date();
XmlResult returnXml = new XmlResult("SUCCESS", "");
returnXml.setResultCode("SUCCESS");
returnXml = PAY3.interfaceReport(interfaceUrl, executeTime, outTradeNo,
ip, time, returnXml);
System.err.println(returnXml);
}
}

View File

@ -98,6 +98,8 @@ public class WeixinProxy {
/**
* 查询部门列表(以部门的order字段从小到大排列)
*
* @param departId
* 部门ID获取指定部门ID下的子部门
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E5.88.97.E8.A1.A8">获取部门列表</a>
@ -105,8 +107,8 @@ public class WeixinProxy {
* @return 部门列表
* @throws WeixinException
*/
public List<Department> listDepart() throws WeixinException {
return departApi.listDepart();
public List<Department> listDepart(int departId) throws WeixinException {
return departApi.listDepart(departId);
}
/**

View File

@ -75,13 +75,14 @@ public class DepartApi extends QyApi {
/**
* 查询部门列表(以部门的order字段从小到大排列)
*
* @param departId 部门ID获取指定部门ID下的子部门
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E5.88.97.E8.A1.A8">获取部门列表</a>
* @return 部门列表
* @throws WeixinException
*/
public List<Department> listDepart() throws WeixinException {
public List<Department> listDepart(int departId) throws WeixinException {
String department_list_uri = getRequestUri("department_list_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(department_list_uri,

View File

@ -45,7 +45,7 @@ public class DepartTest extends TokenTest {
@Test
public void list() throws WeixinException {
List<Department> list = departApi.listDepart();
List<Department> list = departApi.listDepart(1);
Assert.assertFalse(list.isEmpty());
System.out.println(list);
}