微信分享
想要最终实现这样的效果, 首先前端要写页面样式以及js, 调用微信的接口所需的签名最好在后端进行.这里也主要说.net的后端代码
首先: 你的服务号需要实名认证, 找好服务号的AppId 以及Secret 否则调用没有权限调用接口
然后配置域名白名单
获取签名分3步走
One: 获取access_token: 注它的有限期是7200s 即2个小时
public string Getaccesstoken()
{
var accesstoken = CacheService.GetOrDefault<string>("access_token"); // 从redis里获取的, 有则直接返回 无则请求微信接口
if (!string.IsNullOrEmpty(accesstoken))
{
return accesstoken;
}
var appId = "XXXXXXXXX"; // 找自己微信公众号对应配置
var secret = "XXXXXXXXX"; // 找自己微信公众号对应配置
var urljson = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
var 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")) // srcString 就是返回的包含access_token, 的json
{
Console.WriteLine(srcString);
CacheService.Set("access_token", srcString, TimeSpan.FromSeconds(7200)); // 将它存在redis中了 过期时间也是7200s
}
return strjson;
}
打印出来是:{\"access_token\":\"15_NnETzcScg5gEXG28A173I5ZCpjPZcVq5iGpQLcW7VLAmQvxEXwLiwQZh0zNtGoJrSB0F60AxiXbdU135SgIzkAPc5QO-Zh3cfWN9hmHRRZ2eYXJhSet00w0eWW2nNTlIEnEexJj-RxxIchBsAEGjAGAMQS\",\"expires_in\":7200}
Two: 根据第一步获取到的token获取票据ticket 过期时间也是7200s
public string Getjsapi_ticket()
{
var ticket = CacheService.GetOrDefault<string>("ticket"); // 从redis里获取的, 有则直接返回 无则请求微信接口
if (!string.IsNullOrEmpty(ticket))
{
return ticket;
}
var accesstoken = CacheService.GetOrDefault<string>("access_token"); // 从redis中获取access_token
var result = JsonConvert.DeserializeObject<WeChatSharTokenVM>(accesstoken); // 将json 序列化成vm
string urljson = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + result.access_token + "&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"))
{
CacheService.Set("ticket", srcString, TimeSpan.FromSeconds(7200)); // 将它存在redis中了 过期时间也是7200s
}
return strjson;
}
Three: 获取签名: 由第一步的access_token与第二步的ticket 获取签名
注:nonceStr 与timesapnstr, url 都是比较容易获取的,
public string Getsignature(string nonceStr, string timespanstr, string url)
{
var token = CacheService.GetOrDefault<string>("access_token");
var result = JsonConvert.DeserializeObject<WeChatSharTokenVM>(token);
var ticket = CacheService.GetOrDefault<string>("ticket");
var resultTicket = JsonConvert.DeserializeObject<WeChatSharTokenVM>(ticket);
if (result.access_token == null)
{
Getaccesstoken();
}
if (resultTicket.ticket == null)
{
Getjsapi_ticket();
}
string str = "jsapi_ticket=" + resultTicket.ticket + "&noncestr=" + nonceStr +
"×tamp=" + timespanstr + "&url=" + url;// +"&wxref=mp.weixin.qq.com";
string singature = NETCore.Encrypt.EncryptProvider.Sha1(str);
return singature;
}
最后将他们组装到一起,返回即可: url 即是前端页面的url
/// <summary>
/// Wes the chat shar.
/// </summary>
/// <returns></returns>
public Response<WeChatSharVM> WeChatShar(string url)
{
if (string.IsNullOrEmpty(url))
{
return FastResponse.Response(HttpStatusCode.BadRequest, new WeChatSharVM { }, "url不能为空");
}
var nonceStr = TenPayV3Util.GetNoncestr();
var timeStamp = TenPayV3Util.GetTimestamp();
var token = Getaccesstoken();
Console.WriteLine("————-access_token:" + token + "———–");
var ticket = Getjsapi_ticket();
Console.WriteLine("————-ticket:" + ticket + "———–");
var signature = Getsignature(nonceStr, timeStamp, url);
Console.WriteLine("————-签名:"+signature+"———–");
var result = new WeChatSharVM
{
AppId = WechatModel.AppId,
TimeStamp = timeStamp, //获取微信的时间格式
NonceStr = nonceStr, //随机生成nonceStr
Signature = signature
};
return FastResponse.Response(HttpStatusCode.OK, result);
}
注: WeChatSharVM 如下
/// <summary>
/// 微信分享获取token和签名
/// </summary>
// [BsonIgnoreExtraElements]
public class WeChatSharTokenVM
{
/// <summary>
/// accessToken
/// </summary>
public string access_token { get; set; }
/// <summary>
/// 票据
/// </summary>
public string ticket { get; set; }
/// <summary>
/// 过期时间
/// </summary>
public int expires_in { get; set; }
/// <summary>
/// 签名
/// </summary>
public string signature { get; set; }
}
打完收工!
最终api调用的效果是
然后前端就可以用它愉快的玩耍了~