1. 程式人生 > >關於微信公眾號呼叫微信分享

關於微信公眾號呼叫微信分享

轉自:https://blog.csdn.net/u012291108/article/details/51861012

自從微信6.0以後,,,WeixinJSBridge.on這個方法不能被輕易使用了,解決的辦法有兩個,

第一個是你們的公眾號去微信申請,申請成功,這個方法依然可以使用,另一個方法就是用JS-SDK,方法如下

備註:在所有操作之前微信公眾號各項功能保證開通:

http://jingyan.baidu.com/article/d3b74d64c517051f77e609ed.html

 

一,呼叫介面獲取access_token

返回報文為json格式,例如:

第二步:獲取jsapi-ticket

第三步:獲取簽名演算法

簽名演算法程式碼如下:

 

class Sign {

    public static void main(String[] args) {

        String jsapi_ticket = "jsapi_ticket";

 

        // 注意 

URL 一定要動態獲取,不能 hardcode

        String url = "http://example.com";

        Map<String, String> ret = sign(jsapi_ticket, url);

        for (Map.Entry

 entry : ret.entrySet()) {

            System.out.println(entry.getKey() + ", " + entry.getValue());

        }

    };

 

    public static Map<String, String> sign(String jsapi_ticket, String url) {

        Map<String, String> ret = new HashMap<String, String>();

        String nonce_str = create_nonce_str();

        String timestamp = create_timestamp();

        String string1;

        String signature = "";

 

        //注意這裡引數名必須全部小寫,且必須有序

        string1 = "jsapi_ticket=" + jsapi_ticket +

                  "&noncestr=" + nonce_str +

                  "×tamp=" + timestamp +

                  "&url=" + url;

        System.out.println(string1);

 

        try

        {

            MessageDigest crypt = MessageDigest.getInstance("SHA-1");

            crypt.reset();

            crypt.update(string1.getBytes("UTF-8"));

            signature = byteToHex(crypt.digest());

        }

        catch (NoSuchAlgorithmException e)

        {

            e.printStackTrace();

        }

        catch (UnsupportedEncodingException e)

        {

            e.printStackTrace();

        }

 

        ret.put("url", url);

        ret.put("jsapi_ticket", jsapi_ticket);

        ret.put("nonceStr", nonce_str);

        ret.put("timestamp", timestamp);

        ret.put("signature", signature);

 

        return ret;

    }

 

    private static String byteToHex(final byte[] hash) {

        Formatter formatter = new Formatter();

        for (byte b : hash)

        {

            formatter.format("%02x", b);

        }

        String result = formatter.toString();

        formatter.close();

        return result;

    }

 

    private static String create_nonce_str() {

        return UUID.randomUUID().toString();

    }

 

    private static String create_timestamp() {

        return Long.toString(System.currentTimeMillis() / 1000);

    }

}