求大神帮忙,获取不了openid
发布于 5 年前 作者 yiqiang 3774 次浏览 来自 问答

小程序app.js

wx.login({

    success:res=>{

        console.log(res.code);

    }

})

用以上的res.code通过服务端获取openid报{“errcode”:41008,“errmsg”:“missing code, hints: [ req_id: rn6PNA0941hc52 ]”}

以下是c#的服务端代码

public string GetOpenIdByCode(string code)

        {

            string html = string.Empty;

            string url = “https://api.weixin.qq.com/sns/jscode2session?appid=XXX&secret=XXX&code=” + code + “&grant_type=authorization_code”;

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method = “GET”;

            request.ContentType = “text/html;charset=UTF-8”;

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            Stream ioStream = response.GetResponseStream();

            StreamReader sr = new StreamReader(ioStream, Encoding.UTF8);

            html = sr.ReadToEnd();

            sr.Close();

            ioStream.Close();

            response.Close();

            return html;

        }


2 回复

看报错是说  请求微信接口  传了个空的code   看下后台收到前端传过来的code是个什么情况

前端把code,iv,data给后台,后台解密后返你openid,后台配置文件的appid和秘钥要与前端appid保持一致

回到顶部