您的位置:首页 > 移动开发 > 微信开发

微信公众号第三方平台全网发布测试程序

2015-07-07 23:09 761 查看

1:微信官方文档部分

全网发布测试

在平台接收微信回调的servlet的doPost方法中,增加代码:

[code]//全网发布单元测试
                if("gh_3c884a361561".equals(wechatRequest.getToUserName())){
    FullWebPublishUtil.test(wechatRequest, response);
    }


FullWebPublishUtil全网发布测试工具类

[code]import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import weixin.wechat4j.message.CustomerMsg;
import weixin.wechat4j.request.WechatRequest;
/**
 * 微信公众号第三方平台:全网发布测试程序
 * 创建:刘彦亮  2015年7月7日
 */
public class FullWebPublishUtil {
    public  static String query_auth_code = "";
    public static void test(WechatRequest wechatRequest, HttpServletResponse response){
        String fromUser = wechatRequest.getFromUserName();
        String content = wechatRequest.getContent();
        System.out.println("content======>"+content);
        String content_reply1 = "";
        String content_reply2 = "";
        String content_reply3 = "";
        //1、模拟粉丝触发专用测试公众号的事件
        if(StringUtils.isNotNull(wechatRequest.getEvent())){
            content_reply1 = wechatRequest.getEvent() + "from_callback";
            System.out.println("content_reply1======>"+content_reply1);
            CustomerMsg customerMsg = new CustomerMsg(fromUser,"wizincloud");
            customerMsg.sendText(content);
        }
        //2、模拟粉丝发送文本消息给专用测试公众号
         if(StringUtils.isNotNull(content) && "TESTCOMPONENT_MSG_TYPE_TEXT".equals(content)){
             content_reply2 = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
        //3、模拟粉丝发送文本消息给专用测试公众号  
        }else if(StringUtils.isNotNull(content) && content.contains("QUERY_AUTH_CODE")){
            int index = content.indexOf(":");
            content = content.substring(index+1);
            query_auth_code = content;
            System.out.println("query_auth_code======>"+query_auth_code);
            content_reply3 = query_auth_code+"_from_api";
        }

        // 使用授权码换取公众号的授权信息
        String component_access_token = WeixinUtil.getComponentAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token="
                + component_access_token;
        Map<String, String> paraMap = new HashMap<String, String>();
        paraMap.put("component_appid", Constants.WX_OPEN_APPID);
        paraMap.put("authorization_code", query_auth_code);
        JSONObject job = WeixinUtil.httpRequest(url, "POST", JSONObject.fromObject(paraMap).toString());
        JSONObject authjob = (JSONObject) job.get("authorization_info");
        String authorizer_appid = authjob.getString("authorizer_appid");
        String authorizer_access_token = authjob.getString("authorizer_access_token");
        System.out.println("authorizer_appid="+authorizer_appid);
        //获取access_token
        System.out.println("authorizer_access_token="+authorizer_access_token);
        CustomerMsg customerMsg = new CustomerMsg(fromUser,authorizer_access_token);
        //回复测试信息
       if(StringUtils.isNotNull(content_reply1)){
            customerMsg.sendText(content_reply1);
        }
        if(StringUtils.isNotNull(content_reply2)){
            customerMsg.sendText(content_reply2);
        }
        if(StringUtils.isNotNull(content_reply3)){
            customerMsg.sendText(content_reply3);
        }
     }

}


3:微信开放平台全网发布

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: