java 实现微信自定义分享功能,总是报config:invalid signature.
澳门新葡京官网
澳门新葡京官网
当前位置 : 澳门新葡京官网 > 新葡京娱乐场

java 实现微信自定义分享功能,总是报config:invalid signature.

1.WeixinUtil.class
package com.weixin.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

public class WeixinUtil {


/**
* 方法名:httpRequest</br>
* 详述:发送http请求</br>
* @param requestUrl
* @param requestMethod
* @param outputStr
* @return 说明返回值含义
* @throws 说明发生此异常的条件
 */
public static JSONObject httpRequestString requestUrl,String requestMethod, String outputStr {
    JSONObject jsonObject = null;
    StringBuffer buffer = new StringBuffer;
    try {
        TrustManager[] tm = { new MyX509TrustManager };
        
        SSLContext sslContext = SSLContext.getInstance"SSL", "SunJSSE";
        sslContext.initnull, tm, new java.security.SecureRandom;
        SSLSocketFactory ssf = sslContext.getSocketFactory;
        URL url = new URLrequestUrl;
        HttpsURLConnection httpUrlConn = HttpsURLConnection url.openConnection;
        httpUrlConn.setSSLSocketFactoryssf;
        httpUrlConn.setDoOutputtrue;
        httpUrlConn.setDoInputtrue;
        httpUrlConn.setUseCachesfalse;
        httpUrlConn.setRequestMethodrequestMethod;
        if "GET".equalsIgnoreCaserequestMethod
            httpUrlConn.connect;
        if null != outputStr {
            OutputStream outputStream = httpUrlConn.getOutputStream;
            outputStream.writeoutputStr.getBytes"UTF-8";
            outputStream.close;
        }
        InputStream inputStream = httpUrlConn.getInputStream;
        InputStreamReader inputStreamReader = new InputStreamReaderinputStream, "utf-8";
        BufferedReader bufferedReader = new BufferedReaderinputStreamReader;
        String str = null;
        while str = bufferedReader.readLine != null {
            buffer.appendstr;
        }
        bufferedReader.close;
        inputStreamReader.close;
        inputStream.close;
        inputStream = null;
        httpUrlConn.disconnect;
        jsonObject = JSONObject.fromObjectbuffer.toString;
    } catch ConnectException ce {
        ce.printStackTrace;
    } catch Exception e {
        e.printStackTrace;
    }
    return jsonObject;
}


/**
* 方法名:getWxConfig</br>
* 详述:获取微信的配置信息 </br>
* @param request
* @return 说明返回值含义
* @throws 说明发生此异常的条件
 */
public static Map<String, Object> getWxConfigHttpServletRequest request {
    Map<String, Object> ret = new HashMap<String, Object>;
  
    String appId = "wxbf547a5a1d4c13f7"; // 必填,公众号的唯一标识
    String secret = "d4624c36b6795d1d99dcf0547af5443d";

    String requestUrl = request.getRequestURL.toString;
    String access_token = "";
    String jsapi_ticket = "";
    String timestamp = Long.toStringSystem.currentTimeMillis / 1000; // 必填,生成签名的时间戳
    String nonceStr = UUID.randomUUID.toString; // 必填,生成签名的随机串
    String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" + secret;
    
    JSONObject json = WeixinUtil.httpRequesturl, "GET", null;
    
    if json != null {
        //要注意,access_token需要缓存
        access_token = json.getString"access_token";
        url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token + "&type=jsapi";
        
        json = WeixinUtil.httpRequesturl, "GET", null;
        if json != null {
            jsapi_ticket = json.getString"ticket";
            
        }
    }
    String signature = "";
    // 注意这里参数名必须全部小写,且必须有序
    String sign = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonceStr+ "&timestamp=" + timestamp + "&url=" + requestUrl;
    try {
        MessageDigest crypt = MessageDigest.getInstance"SHA-1";
        crypt.reset;
        crypt.updatesign.getBytes"UTF-8";
        signature = byteToHexcrypt.digest;
    } catch NoSuchAlgorithmException e {
        e.printStackTrace;
    } catch UnsupportedEncodingException e {
        e.printStackTrace;
    }
    
    ret.put"token", access_token;
    ret.put"ticket", jsapi_ticket;
    ret.put"ksy", json;
    
    ret.put"appId", appId;
    ret.put"timestamp", timestamp;
    ret.put"nonceStr", nonceStr;
    ret.put"signature", signature;
    return ret;
}


/**
* 方法名:byteToHex</br>
* 详述:字符串加密辅助方法 </br>
* @param hash
* @return 说明返回值含义
* @throws 说明发生此异常的条件
 */
private static String byteToHexfinal byte[] hash {
    Formatter formatter = new Formatter;
    for byte b : hash {
        formatter.format"%02x", b;
    }
    String result = formatter.toString;
    formatter.close;
    return result;

}

}

2.share.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="com.weixin.util.WeixinUtil" %>
<%-- <%
String path = request.getContextPath;
String basePath = request.getScheme+"://"+request.getServerName+":"+request.getServerPort+path+"/";
%> --%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<%-- <base href="<%=basePath%>"> --%>
<title>微信分享测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<%

    Map<String,Object>  ret = new HashMap<String,Object> ;
    ret=WeixinUtil.getWxConfigrequest;
    request.setAttribute"appId", ret.get"appId";
    request.setAttribute"timestamp", ret.get"timestamp";
    request.setAttribute"nonceStr", ret.get"nonceStr";
    request.setAttribute"signature", ret.get"signature";
    //自测参数
    request.setAttribute"ticket", ret.get"ticket";
    request.setAttribute"token", ret.get"token";
    request.setAttribute"ksy", ret.get"ksy";
    request.setAttribute"ksy1", ret;
    %>  
<!--封装微信接口 -->
<%-- <%@include file="wxjshead.jsp"%> --%>
<!--引入js文件  -->
<!-- <script type="text/javascript" src="share.js"></script> -->
<style type="text/css">
    a:link {
     text-decoration: none;
    }
</style> 

</head>

<body>

   <a href="javascript:void0;" onclick="shareFriend"><h1>点我进行分享</h1></a>
   <h1>${ticket}+"*1"</h1>   
   <h1>${token}+"*2"</h1>
   <h1>"json数据"${ksy}+"*3json"</h1>
   <h1>"ret数据"${ksy1}+"*4"</h1>                 

</body>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/...
<script type="text/javascript">

alert"start";

// 微信信息的以及调用的配置
wx.config{

debug: true, 
appId: ${appId}, 
timestamp: ${timestamp}, 
nonceStr: ${nonceStr}, 
signature: ${signature},
jsApiList: [chooseImage,onMenuShareTimeline, onMenuShareAppMessage,onMenuShareQQ,onMenuShareWeibo,onMenuShareQZone] 

};

wx.readyfunction{

alert"wx.ready";
/* wx.checkJsApi{
    jsApiList: [onMenuShareAppMessage], // 需要检测的JS接口列表,所有JS接口列表见附录2,
    success: functionres {
        // 以键值对的形式返回,可用的api值true,不可用为false
        // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
        $.scojs_messageJSON.stringifyres,$.scojs_message.TYPE_OK;
    }
}; */
//wx.hideOptionMenu;
// 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
wx.onMenuShareTimeline{
    title: 第八篇 :微信公众平台开发实战Java版之如何网页授权获取用户基本信息, // 分享标题
    link:"http://www.cnblogs.com/liuhongfeng/p/5099149.html",
    imgUrl: "http://images.cnblogs.com/cnblogs_com/liuhongfeng/737147/o_1442809977405.jpg", // 分享图标
    success: function  { 
         // 用户确认分享后执行的回调函数
         alert"成功分享到朋友圈";
    },
    cancel: function  { 
        // 用户取消分享后执行的回调函数
    },
    fail: function res {
        alert分享到朋友圈失败;
    }
};
// 获取“分享给朋友”按钮点击状态及自定义分享内容接口
wx.onMenuShareAppMessage{
    title: 第七篇 :微信公众平台开发实战Java版之如何获取微信用户基本信息, // 分享标题
    desc: "第七篇 :微信公众平台开发实战Java版之如何获取微信用户基本信息", // 分享描述
    link:"http://www.cnblogs.com/liuhongfeng/p/5057167.html",
    imgUrl: "http://images.cnblogs.com/cnblogs_com/liuhongfeng/737147/o_QQ%E5%9B%BE%E7%89%8720151118180508.png", // 分享图标
    type: link, // 分享类型,music、video或link,不填默认为link
    success: function  { 
        // 用户确认分享后执行的回调函数
        alert"成功分享给朋友";
     },
     cancel: function  { 
        // 用户取消分享后执行的回调函数
     },
     fail: function res {
            alert分享给朋友失败;
     }
     
}; 

//获取“分享到QQ”按钮点击状态及自定义分享内容接口

wx.onMenuShareQQ{
    title: 第六篇 :微信公众平台开发实战Java版之如何自定义微信公众号菜单, // 分享标题
    desc: 第六篇 :微信公众平台开发实战Java版之如何自定义微信公众号菜单, // 分享描述
    link: http://www.cnblogs.com/liuhongfeng/p/4857312.html, // 分享链接
    imgUrl: http://images.cnblogs.com/cnblogs_com/liuhongfeng/737147/o_qrcode_for_gh_228cd30523bc_258.jpg, // 分享图标
    success: function  { 
       // 用户确认分享后执行的回调函数
    },
    cancel: function  { 
       // 用户取消分享后执行的回调函数
    }
};

};
</script>
</html>

估计是签名所使用的url不一样。
微信所使用的签名url是location.href.split#[0]这个值。

栏目列表

广告位

澳门新葡京官网