asp.net 微信分享到朋友圈,分享给朋友接口

asp.net 微信分享到朋友圈,分享给朋友接口微信分享到朋友圈,分享给朋友说明:转载:http://www.cnblogs.com/ysyn/archive/2015/07/23/4665897.html、引言:  工作中开发微信网站,简称微网站。由于微网站的分享内容是系统自动选取的当前网址,客户需要改变分享的内容,即点击屏幕右上角的分享按钮,选择发送给朋友和发送到朋友圈,其中的内容和图片需要自定义。于

大家好,又见面了,我是你们的朋友全栈君。

微信分享到朋友圈,分享给朋友说明:

转载:http://www.cnblogs.com/ysyn/archive/2015/07/23/4665897.html、

引言:

  工作中开发微信网站,简称微网站。由于微网站的分享内容是系统自动选取的当前网址,客户需要改变分享的内容,即点击屏幕右上角的分享按钮,选择发送给朋友和发送到朋友圈,其中的内容和图片需要自定义。于是查找文档微信JS-SDK说明文档一文和网站众多高手的经验,大体知道了调用的步骤,但是具体如何调用才能成功却是不了解的。经过一番试验,终于成功调用发送朋友和发送到朋友圈两个接口,此处记录调用的具体过程。

 

步骤一:引用js文件。

在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.0.0.js

 

步骤二:通过config接口注入权限验证配置

复制代码
wx.config({
    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: '', // 必填,公众号的唯一标识
    timestamp: , // 必填,生成签名的时间戳
    nonceStr: '', // 必填,生成签名的随机串
    signature: '',// 必填,签名,见附录1
    jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
复制代码

网上众多网友也是这样写的,但是具体如果配却谈之甚少,接下来介绍本文是如何调用的。

debug和appId,都不用说,很简单。

timespan生成签名的时间戳:

复制代码
/// <summary>
        /// 生成时间戳
        /// 从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间,且最终需要转换为字符串形式
        /// </summary>
        /// <returns></returns>
        public string getTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
复制代码

 

nonceStr生成签名的随机串:

复制代码
/// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <returns></returns>
        public string getNoncestr()
        {
            Random random = new Random();
            return MD5Util.GetMD5(random.Next(1000).ToString(), "GBK");
        }
复制代码
asp.net 微信分享到朋友圈,分享给朋友接口

复制代码
/// <summary>
    /// MD5Util 的摘要说明。
    /// </summary>
    public class MD5Util
    {
        public MD5Util()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /** 获取大写的MD5签名结果 */
        public static string GetMD5(string encypStr, string charset)
        {
            string retStr;
            MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();

            //创建md5对象
            byte[] inputBye;
            byte[] outputBye;

            //使用GB2312编码方式把字符串转化为字节数组.
            try
            {
                inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
            }
            catch (Exception ex)
            {
                inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
            }
            outputBye = m5.ComputeHash(inputBye);

            retStr = System.BitConverter.ToString(outputBye);
            retStr = retStr.Replace("-", "").ToUpper();
            return retStr;
        }
    }
复制代码

 

singature签名的生成比较麻烦。

首先生成获取access_token(有效期7200秒,开发者必须在自己的服务全局缓存access_token

复制代码
public string Getaccesstoken()
        {
            string appid = System.Configuration.ConfigurationManager.AppSettings["WXZjAppID"].ToString();
            string secret = System.Configuration.ConfigurationManager.AppSettings["WXZjAppSecret"].ToString();
            string urljson = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            string strjson = "";
            UTF8Encoding encoding = new UTF8Encoding();
            HttpWebRequest myRequest =
            (HttpWebRequest)WebRequest.Create(urljson);
            myRequest.Method = "GET";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse response;
            Stream responseStream;
            StreamReader reader;
            string srcString;
            response = myRequest.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString = reader.ReadToEnd();
            reader.Close();
            if (srcString.Contains("access_token"))
            {
                //CommonJsonModel model = new CommonJsonModel(srcString);
                HP.CPS.BLL.WeiXin.CommonJsonModel model = new BLL.WeiXin.CommonJsonModel(srcString);
                strjson = model.GetValue("access_token");
                Session["access_tokenzj"] = strjson;
            }
            return strjson;
        }
复制代码
asp.net 微信分享到朋友圈,分享给朋友接口

复制代码
public class CommonJsonModelAnalyzer
{
protected string _GetKey(string rawjson)
{
if (string.IsNullOrEmpty(rawjson))
return rawjson;
rawjson = rawjson.Trim();
string[] jsons = rawjson.Split(new char[] { ':' });
if (jsons.Length < 2)
return rawjson;
return jsons[0].Replace("\"", "").Trim();
}
protected string _GetValue(string rawjson)
{
if (string.IsNullOrEmpty(rawjson))
return rawjson;
rawjson = rawjson.Trim();
string[] jsons = rawjson.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (jsons.Length < 2)
return rawjson;
StringBuilder builder = new StringBuilder();
for (int i = 1; i < jsons.Length; i++)
{
builder.Append(jsons[i]);
builder.Append(":");
}
if (builder.Length > 0)
builder.Remove(builder.Length - 1, 1);
string value = builder.ToString();
if (value.StartsWith("\""))
value = value.Substring(1);
if (value.EndsWith("\""))
value = value.Substring(0, value.Length - 1);
return value;
}
protected List<string> _GetCollection(string rawjson)
{
//[{},{}]
List<string> list = new List<string>();
if (string.IsNullOrEmpty(rawjson))
return list;
rawjson = rawjson.Trim();
StringBuilder builder = new StringBuilder();
int nestlevel = -1;
int mnestlevel = -1;
for (int i = 0; i < rawjson.Length; i++)
{
if (i == 0)
continue;
else if (i == rawjson.Length - 1)
continue;
char jsonchar = rawjson[i];
if (jsonchar == '{
')
{
nestlevel++;
}
if (jsonchar == '}')
{
nestlevel--;
}
if (jsonchar == '[')
{
mnestlevel++;
}
if (jsonchar == ']')
{
mnestlevel--;
}
if (jsonchar == ',' && nestlevel == -1 && mnestlevel == -1)
{
list.Add(builder.ToString());
builder = new StringBuilder();
}
else
{
builder.Append(jsonchar);
}
}
if (builder.Length > 0)
list.Add(builder.ToString());
return list;
}
}
public class CommonJsonModel : CommonJsonModelAnalyzer
{
private string rawjson;
private bool isValue = false;
private bool isModel = false;
private bool isCollection = false;
public CommonJsonModel(string rawjson)
{
this.rawjson = rawjson;
if (string.IsNullOrEmpty(rawjson))
throw new Exception("missing rawjson");
rawjson = rawjson.Trim();
if (rawjson.StartsWith("{
"))
{
isModel = true;
}
else if (rawjson.StartsWith("["))
{
isCollection = true;
}
else
{
isValue = true;
}
}
public string Rawjson
{
get { return rawjson; }
}
public bool IsValue()
{
return isValue;
}
public bool IsValue(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsValue();
}
}
return false;
}
public bool IsModel()
{
return isModel;
}
public bool IsModel(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsModel();
}
}
return false;
}
public bool IsCollection()
{
return isCollection;
}
public bool IsCollection(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsCollection();
}
}
return false;
}
/// <summary>
/// 当模型是对象,返回拥有的key
/// </summary>
/// <returns></returns>
public List<string> GetKeys()
{
if (!isModel)
return null;
List<string> list = new List<string>();
foreach (string subjson in base._GetCollection(this.rawjson))
{
string key = new CommonJsonModel(subjson).Key;
if (!string.IsNullOrEmpty(key))
list.Add(key);
}
return list;
}
/// <summary>
/// 当模型是对象,key对应是值,则返回key对应的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetValue(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
return model.Value;
}
return null;
}
/// <summary>
/// 模型是对象,key对应是对象,返回key对应的对象
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public CommonJsonModel GetModel(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
if (!submodel.IsModel())
return null;
else
return submodel;
}
}
return null;
}
/// <summary>
/// 模型是对象,key对应是集合,返回集合
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public CommonJsonModel GetCollection(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
if (!submodel.IsCollection())
return null;
else
return submodel;
}
}
return null;
}
/// <summary>
/// 模型是集合,返回自身
/// </summary>
/// <returns></returns>
public List<CommonJsonModel> GetCollection()
{
List<CommonJsonModel> list = new List<CommonJsonModel>();
if (IsValue())
return list;
foreach (string subjson in base._GetCollection(rawjson))
{
list.Add(new CommonJsonModel(subjson));
}
return list;
}
/// <summary>
/// 当模型是值对象,返回key
/// </summary>
private string Key
{
get
{
if (IsValue())
return base._GetKey(rawjson);
return null;
}
}
/// <summary>
/// 当模型是值对象,返回value
/// </summary>
private string Value
{
get
{
if (!IsValue())
return null;
return base._GetValue(rawjson);
}
}
}
复制代码

 

接着获取jsapi_ticket:

用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket

复制代码
public string Getjsapi_ticket()
{
string accesstoken = (string)Session["access_tokenzj"];
string urljson = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accesstoken + "&type=jsapi";
string strjson = "";
UTF8Encoding encoding = new UTF8Encoding();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson);
myRequest.Method = "GET";
myRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string srcString = reader.ReadToEnd();
reader.Close();
if (srcString.Contains("ticket"))
{
HP.CPS.BLL.WeiXin.CommonJsonModel model = new BLL.WeiXin.CommonJsonModel(srcString);
strjson = model.GetValue("ticket");
Session["ticketzj"] = strjson;
}
return strjson;
}
复制代码

 

最后生成signature:

复制代码
public string Getsignature(string nonceStr, string timespanstr)
{
if (Session["access_tokenzj"] == null)
{
Getaccesstoken();
}
if (Session["ticketzj"] == null)
{
Getjsapi_ticket();
}
string url = Request.Url.ToString();
string str = "jsapi_ticket=" + (string)Session["ticketzj"] + "&noncestr=" + nonceStr +
"&timestamp=" + timespanstr + "&url=" + url;// +"&wxref=mp.weixin.qq.com";
string singature = SHA1Util.getSha1(str);
string ss = singature;
return ss;
}
复制代码
asp.net 微信分享到朋友圈,分享给朋友接口 
View Code

 

本文调用实例:

复制代码
<script type="text/javascript">
wx.config({
debug: false,
appId: '<%=corpid %>',
timestamp: <%=timestamp%>,
nonceStr: '<%=nonceStr%>',
signature: '<%=signature %>',
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
});
</script>
复制代码

 

步骤三:调用接口

在进行完第二步的调用后,步骤三就显得非常轻巧了。

wx.ready(function(){ // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。 });

本文的调用实例是:
复制代码
<script type="text/javascript" >
wx.ready(function () {
wx.onMenuShareAppMessage({
title: '<%=shareTitle %>',
desc: '<%=shareContent %>',
link: '<%=currentUrl %>',
imgUrl: '<%=shareImageUrl %>'
});
wx.onMenuShareTimeline({
title: '<%=shareContent %>',
link: '<%=currentUrl %>',
imgUrl: '<%=shareImageUrl %>'
});
})     
</script>
复制代码

本文基本上总结到此处。

易出现的问题:

1、检查后台是否设置:右上角公众号名称–功能设置–JS接口安全域名

2、检查代码里的appid和公众号后台的id是否一致

3、图片的调用地址是绝对路径(相对路径好像不行)。



引用文档:
微信JS-SDK说明文档

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/140755.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • django自定义用户认证_用户自定义的类

    django自定义用户认证_用户自定义的类前言如果我们不用使用drf那套认证规则,我们想自定义认证类,那么我们首先要知道,drf本身是如何定义认证规则的,也就是要查看它的源码是如何写的源码分析源码的入口在APIView.py文件下的di

  • AutoScaling 目标追踪伸缩规则概述「建议收藏」

    AutoScaling 目标追踪伸缩规则概述「建议收藏」AutoScaling 目标追踪伸缩规则概述

  • artcam浮雕实例教程_artcam2018入门教程

    artcam浮雕实例教程_artcam2018入门教程记录过去,奋斗现在,展望未来(给未来即将结束的工作的’存档’)ArtCAM入门简单教程二、浮雕前言:这不是个很专业的浮雕设计教程,不过是很简单即可实现的入门教程,而更深入的浮雕学习建议查找更全面的网上教程或CAM浮雕书籍。   常用的矢量雕刻,详见:http://blog.sina.com.cn/s/blog_647ef76d0101jgwn.html开始之

  • ADRC自抗扰控制自学笔记(包含simulink仿真)(转载)

    ADRC自抗扰控制自学笔记(包含simulink仿真)(转载)摘自:https://blog.csdn.net/zouxu634866/article/details/106287879#comments_12978720ADRC自抗扰控制自学笔记(包含simulink仿真)总被蚊子叮的小旭2020-05-2217:59:361856收藏28分类专栏:控制版权ADRC控制中包含三个主要的部分:跟踪微分器,非线性状态反馈(非线性组合),扩张观测器。ADRC特点:继承了经典PID控制器的精华,对被控对…

  • PLD- FPGA与CPLD的区别[通俗易懂]

    PLD- FPGA与CPLD的区别[通俗易懂]http://home.eeworld.com.cn/my/space.php?uid=170289&do=blog&id=31215FPGA与CPLD的区别多篇整合系统的比较,与大家共享:尽管FPGA和CPLD都是可编程ASIC器件,有很多共同特点,但由于CPLD和FPGA结构上的差异,具有各自的特点:①CPLD更适合完成各种算法和组合逻辑,FPGA更适合于完成时序逻辑

  • Ubuntu如何卸载软件_linux卸载软件包命令

    Ubuntu如何卸载软件_linux卸载软件包命令步骤:1、Ctrl+Alt+T或者空白处右键—>选择openterminal,打开终端;2、输入命令:dpkg–list浏览并找到已安装的程序名字,baidunetdisk3、输入命令:不完全卸载:sudoapt-getremovebaidunetdisk完全卸载:sudoapt-get–purgeremovebaidunetdisk————————————————原文链接:https://blog.csdn.net/UPPER_lucky/article/

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号