1,必须用服务号,开发--接口权限--网页服务--网页账号--绑定网站域名不要加http
<div><a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=&redirect_uri=http%3a%2f%2fgs.zqnb.com.cn%2fapi%2fwxxs%2fhome%2fGetTemp&response_type=
code&scope=snsapi_userinfo&state=STATE#wechat_redirect ">点击</a></div>
public bool GetTemp(string code, string state)
{
var openid = "";
var access_token = "";
//获取用户openid
string get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=&secret=&code=" + code + "&grant_type=authorization_code";
var client = new HttpClient();
var result = client.GetAsync(get_access_token_url).Result;
var jsTicket = result.Content.ReadAsStringAsync().Result;
JObject jo = JObject.Parse(jsTicket);
if (jo["openid"] != null)
{
openid = jo["openid"].ToString();
}
//获取公众号的access_token,此access_token不是用户授权后的access_token
string get_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
var result1 = client.GetAsync(get_token_url).Result;
var jsTicket1 = result1.Content.ReadAsStringAsync().Result;
JObject jo1 = JObject.Parse(jsTicket1);
if (jo1["access_token"] != null)
{
access_token = jo1["access_token"].ToString();
}
string subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
var result2 = client.GetAsync(subscribe_msg).Result;
var jsTicket2 = result2.Content.ReadAsStringAsync().Result;
JObject jo2 = JObject.Parse(jsTicket2);
string subscribe = "";
if (jo2["subscribe"] != null)
{
subscribe = jo2["subscribe"].ToString();
}
if (subscribe == "0")
{
return false;
}
return true;
}