第三方调用微信登录API接口
APP调用第三方调用微信登录API接口,根据微信官提供的验证要求开发。
需要提供定制服务,请到下列店铺咨询!
https://item.taobao.com/item.htm?&id=564448421625
微信API官方接口文档地址:
打开地址后,点击“移动应用”–“微信登录功能”(切记接口文档不能用错,里面的接口比较多)
还有一点要说明的是,微信登录验证功能可以是APP端口直接与微信做接口,也可以APP请求服务端在服务端做接口请求微信后返回给APP端,这里面我们介绍的是第二种方式,APP端口只负责传输和接受数据,所有的逻辑处理都放在服务端,这样安全性高。
演示案例地址:
http://www.apicool.cn/Allincare/Service.asmx
接入步骤:
1、第一步是前端做的,获取Code
前端需要根据微信的接入要求获取当前登录在APP端的微信用户的唯一Code,微信提供了SDK包可以参考获取Code。也在上面的微信API官方文档中。
2、第二步通过Code获取access_token
APP端拿到Code后请求服务端,服务端拿到Code后请求微信获取access_token
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + WebChatCode + "&grant_type=authorization_code";
3、第三步刷新access_token
解析得到返回值中的refresh_token
string refresh_token = rb.refresh_token; url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + refresh_token;
4、第四步根据access_token获取微信用户信息
url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid
5、第五步,将接受到的用户 信息返回给APP端口
附一段完整代码:
public static string CheckWebChatLoginStr(string WebChatCode) { string sRemoteInfo = string.Empty; string appid = "wxcb4c39b4227184e5"; string secret = "41e1cf30a441ea8559be1295910122e0"; //第一步是前端做的,获取Code //第二步通过Code获取access_token string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + WebChatCode + "&grant_type=authorization_code"; //执行http请求得到返回值 sRemoteInfo = GetHttpResponse(url, 6000); //解析,序列化返回的json RootObject rb = JsonConvert.DeserializeObject<RootObject>(sRemoteInfo); //第三步刷新access_token //解析得到返回值中的refresh_token string refresh_token = rb.refresh_token; url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + refresh_token; //执行http请求,得到返回值 sRemoteInfo = GetHttpResponse(url, 6000); //解析,序列化返回的json RootObject1 rb1 = JsonConvert.DeserializeObject<RootObject1>(sRemoteInfo); //取token和openid string access_token = rb1.access_token; string openid = rb1.openid; //第四步根据access_token获取微信用户信息 url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid; //执行http请求得到返回值 sRemoteInfo = GetHttpResponse(url, 6000); //解析,序列化返回的json RootObject2 rb2 = JsonConvert.DeserializeObject<RootObject2>(sRemoteInfo); if (rb2.nickname != null) { //插入数据库用户 operateCon.UserLogin(rb2.nickname, "123456"); } return sRemoteInfo; }
案例截图:
未经允许不得转载:软件接口开发,api接口开发,webservice接口开发,设备接口,xml数据解析,json数据解析 » 第三方调用微信登录API接口