微信分享到朋友圈

微信内置浏览器(WebView)中特有的javascript API(javascript Interface),随着微信官方的调整,部分API已经不能直接使用,比如类似直接分享到朋友圈WeixinJSBridge.invoke(shareTimeline,data,callback) 这样的功能,直接调用,会得到一个访问拒绝的response
 function WeiXinShareBtn() {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
                alert("请先通过微信搜索 wow36kr 添加36氪为好友,通过微信分享文章 ");
            } else {
                WeixinJSBridge.invoke('shareTimeline', {
                    "title": "36氪",
                    "link": "http://www.36kr.com",
                    "desc": "关注互联网创业",
                    "img_url": "http://www.36kr.com/assets/images/apple-touch-icon.png"
                });
            }
        }


此方法已过时,被微信禁用了。只能用微信官方的接口

1、前台脚本

 
     
    


2、后台代码

using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jkmobile.share
{
    public partial class ReportShare : System.Web.UI.Page
    {
        
        public string appid = "", timestamp="", nonce="",thisUrl="", signature="";
        protected void Page_Load(object sender, EventArgs e)
        {
            WXShareModel wxmodel = new WXShareModel();
            wxmodel = Ge_WXShareModel();
            appid = wxmodel.appid;
            timestamp = wxmodel.timestamp;
            nonce = wxmodel.nonce;
            thisUrl = wxmodel.thisUrl;
            signature = wxmodel.signature; 
        }
        #region 微信分享
          public WXShareModel Ge_WXShareModel()
        {
            string APPID = ConfigurationManager.AppSettings["appid"];
            string secret = ConfigurationManager.AppSettings["secret"];
            WXShareModel fxModel = new WXShareModel();
            fxModel.appid = APPID;
            fxModel.timestamp = JSSDKHelper.GetTimestamp();
            fxModel.nonce = JSSDKHelper.GetNoncestr();
            fxModel.thisUrl = Request.Url.ToString().Split('#')[0];
            fxModel.ticket = JsApiTicketContainer.TryGetJsApiTicket(APPID, secret);
            //最后一个参数url,必须为当前的网址  
            var signature = JSSDKHelper.GetSignature(fxModel.ticket, fxModel.nonce, fxModel.timestamp, fxModel.thisUrl);
            fxModel.signature = signature;
            return fxModel;

        }
        #endregion
    }

    public class WXShareModel
    {
        /// 
        /// 公众号的唯一标识
        /// 
        public string appid { get; set; }
        /// 
        /// 生成签名的随机串
        /// 
        public string nonce { get; set; }
        /// 
        /// 生成签名的时间戳
        /// 
        public string timestamp { get; set; }
        /// 
        /// 签名
        /// 
        public string signature { get; set; }

        public string ticket { get; set; }
        /// 
        /// 分享的地址
        /// 
        public string thisUrl { get; set; }


    }
}

请引用微信官方封装好的dll

Senparc.Weixin.dll

Senparc.Weixin.MP.dll

见下载资源 源码下载

你可能感兴趣的:(微信分享到朋友圈)