首页 新闻 会员 周边

获取微信公众号的openid

0
悬赏园豆:50 [已解决问题] 解决于 2017-03-07 15:41

想通过公众号与PC端网站进行绑定,在网站上写接口 获取到openid然后添加到数据库中进行绑定,看完开发者文档 ,还是不知道具体怎么写 大概我理解了  应该先获取到code 然后获取access_token  最后获取到openid
但是网站上怎么写这个接口
代码不会 用.net 
那位大神能给我发一个   小弟感激不尽呀!!

皇刮的主页 皇刮 | 初学一级 | 园豆:12
提问于:2017-02-15 18:18
< >
分享
最佳答案
0

微信获取openid参考代码:

public static string getOAuthTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
public static WeixinOAuthToken GetOAuthToken(string code)
        {
            string url = string.Format(getOAuthTokenUrl, appId, appSecret, code);
            string oauthTokenJson = Commons.RequestByGet(url);
            return JsonHelper.FromJson<WeixinOAuthToken>(oauthTokenJson);
        }

储存微信返回的对象实体WeixinOAuthToken代码:

    /// <summary>
    /// 微信网页授权token类
    /// </summary>
    public class WeixinOAuthToken
    {
        /// <summary>
        /// 返回的token
        /// </summary>
        public string access_token { get; set; }
        /// <summary>
        /// 过期时间(秒)
        /// </summary>
        public int expires_in { get; set; }
        /// <summary>
        /// 用户刷新access_token
        /// </summary>
        public string refresh_token { get; set; }
        /// <summary>
        /// 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
        /// </summary>
        public string openid { get; set; }
        /// <summary>
        /// 用户授权的作用域,使用逗号(,)分隔
        /// </summary>
        public string scope { get; set; }
    }

 Http请求代码:

        /// <summary>
        /// 通过GET方式提交数据并获取返回信息
        /// </summary>
        /// <param name="strUrl">所访问的网址</param>
        /// <param name="encoding">指定的编码格式</param>
        /// <param name="timeout">超时时长(默认2分钟)</param>
        /// <returns></returns>
        public static string RequestByGet(string strUrl, Encoding encoding = null, int timeout = 120000)
        {
            string strResult;
            try
            {
                WebRequest request = WebRequest.Create(strUrl);
                request.Timeout = timeout;
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader sr = new StreamReader(stream, encoding == null ? Encoding.Default : encoding);
                StringBuilder strBuilder = new StringBuilder();
                while (-1 != sr.Peek())
                {
                    strBuilder.Append(sr.ReadLine());
                }

                strResult = strBuilder.ToString();
            }
            catch (Exception exp)
            {
                strResult = "错误:" + exp.Message;
                AllLogFunction.WriteError(strResult);
            }

            return strResult;
        }

 

收获园豆:50
txworld | 初学一级 |园豆:49 | 2017-02-16 10:57
其他回答(1)
0

1、跳转到到微信授权页面,链接去文档里面找

balahoho | 园豆:2050 (老鸟四级) | 2017-02-15 18:37

2、授权成功后回跳回你的页面,同时也在url上带了一个参数code

3、你拿code在后端对微信接口发请求获取accesstoken,再用accesstoken请求接口获取到用户信息

4、用户信息里就有openid了

支持(0) 反对(0) balahoho | 园豆:2050 (老鸟四级) | 2017-02-15 18:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册