第一步 :配置测试号,网页授权获取用户基本信息。

该授权回掉页面域名为ngrok 映射的域名,我的映射地址是127.0.0.1:8080。

到此微信配置完毕,接下来就是直接上代码了

2.用户同意授权

    我是把这个url写在微信菜单下的,当进入这个页面的时候就让用户同意。注意:好像是静默授权的,用户不知道

    1.url:
      https://open.weixin.qq.com/connect/oauth/authorize?appid=appid&redirect_uri=url&response_type=code&scope=snsapi_userinfo&state=park#wechat_redirect

参数:appid:公众号的唯一标识

       redirect_uri:重定向的url,就是授权后要跳转的页面

       scope:应用授权作用域

          snsapi_base:不弹出授权页面,直接跳转,只能获取用户openid

          snsapi_userinfo:弹出授权页面,可通过openid拿到昵称、性别、所在地

       state:重定向后带的参数

    2.用户同意后会产生一个code,只有5分钟时间的有效期。

先说第一种

  (1)首先需要先访问微信的链接

    https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxxxxxxx&redirect_uri=http://xxxxxx/open/openid&response_type=code&scope=snsapi_base

这里的 uri就是直接回掉我们的服务地址,一定要记住,服务校验的判断,我是按照来判断的echostr(第二种方式也是这样)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
@Controller
@RequestMapping("/open")
public class OpenController {@RequestMapping("/toOpenId")public @ResponseBody String getOpenId(String code,String echostr,HttpServletResponse res) throws IOException{if(echostr==null){String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx24d47d2080f54c5b&secret=95011ac70909e8cca2786217dd80ee3f&code="+code+"&grant_type=authorization_code";System.out.println(code);String openId="";try {URL getUrl=new URL(url);HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();http.setRequestMethod("GET"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setDoOutput(true);http.setDoInput(true);http.connect();InputStream is = http.getInputStream(); int size = is.available(); byte[] b = new byte[size];is.read(b);String message = new String(b, "UTF-8");JSONObject json = JSONObject.parseObject(message);openId = json.getString("openid");} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return openId;}else{PrintWriter out = res.getWriter();out.print(echostr);return null;}}//做服务器校验@RequestMapping("/tovalid")public void valid(String echostr,HttpServletResponse res) throws IOException{PrintWriter out = res.getWriter();out.print(echostr);}
}

第二种

    (1)

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxx&redirect_uri=http:// 域名

/open/openid&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/weixin")
public class Oauth2Action {@RequestMapping("/oauth")public void auth(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String echostr = request.getParameter("echostr");if(echostr==null){String appId = "wx24d47d2080f54c5b";String appSecret = "95011ac70909e8cca2786217dd80ee3f";//拼接String get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"+ "appid="+ appId+ "&secret="+ appSecret+ "&code=CODE&grant_type=authorization_code";String get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");String code = request.getParameter("code");System.out.println("******************code=" + code);get_access_token_url = get_access_token_url.replace("CODE", code);String json = HttpsGetUtil.doHttpsGetJson(get_access_token_url);JSONObject jsonObject = JSONObject.fromObject(json);String access_token = jsonObject.getString("access_token");String openid = jsonObject.getString("openid");get_userinfo = get_userinfo.replace("ACCESS_TOKEN", access_token);get_userinfo = get_userinfo.replace("OPENID", openid);String userInfoJson = HttpsGetUtil.doHttpsGetJson(get_userinfo);JSONObject userInfoJO = JSONObject.fromObject(userInfoJson);String user_openid = userInfoJO.getString("openid");String user_nickname = userInfoJO.getString("nickname");String user_sex = userInfoJO.getString("sex");String user_province = userInfoJO.getString("province");String user_city = userInfoJO.getString("city");String user_country = userInfoJO.getString("country");String user_headimgurl = userInfoJO.getString("headimgurl");response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the POST method \n");out.println("openid:" + user_openid + "\n\n");out.println("nickname:" + user_nickname + "\n\n");out.println("sex:" + user_sex + "\n\n");out.println("province:" + user_province + "\n\n");out.println("city:" + user_city + "\n\n");out.println("country:" + user_country + "\n\n");out.println("<img src=/" + user_headimgurl + "/");out.println(">");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}else{PrintWriter out = response.getWriter();out.print(echostr);}}
}

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpsGetUtil {public static String doHttpsGetJson(String Url){String message = "";try{System.out.println("doHttpsGetJson");//TODO:ddURL urlGet = new URL(Url);HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET");   //必须是get方式请求  24      http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true);System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒28   System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒29 30
      http.connect();InputStream is =http.getInputStream();int size =is.available();byte[] jsonBytes =new byte[size];is.read(jsonBytes);message=new String(jsonBytes,"UTF-8");} catch (MalformedURLException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();} return message;}
}

转载于:https://www.cnblogs.com/qingmuchuanqi48/p/11628667.html

微信之获取微信的openid(二)详细版相关推荐

  1. 从微信官方获取微信公众号二维码(名片)

    从微信官方获取微信公众号二维码(名片) src="http://open.weixin.qq.com/qr/code?username=微信号" <img src=" ...

  2. 微信中获取微信用户信息的2种方式

    微信中获取微信用户信息的2中方式 1, 在公众号底部菜单栏地址配置h5地址,如 http://test.dingdong.com/page1 然后前端在所有页面前拦截如login页面,查看是否有tok ...

  3. 从微信官方获取微信公众号名片:https://open.weixin.qq.com/qr/code?username=haihongruanjian...

    从微信官方获取微信公众号名片: https://open.weixin.qq.com/qr/code?username=haihongruanjian (http://open.weixin.qq.c ...

  4. 微信授权获取用户的openid和支付宝授权获取用户的userid

    为什么80%的码农都做不了架构师?>>>    当一请求一个链接或者是扫描二维码时,会请求后台方法,当然对于微信和支付宝来说,大多数时候是扫 码 一.首先说微信: 1.首先会判断请求 ...

  5. 微信授权获取code请求openID,js+java

    作者:LoveEmperor-王子様 微信网页获取用户授权 请见另一版: https://blog.csdn.net/qq_31424825/article/details/80272364 背景 * ...

  6. CocosCreator微信小游戏接入微信登录获取微信名、头像、经纬度等信息

    前言 微信小游戏接入微信登录还是很简单的,不像原生平台开发,还需要提供appid,appsecret等信息,并有一系列的和微信平台的交互,才能最终授权成功. 下面TS代码演示了,老的接入流程. exp ...

  7. 微信授权-获取微信授权后用户信息

    微信授权登陆: 我采用的是自定义的子菜单:登陆微信公众平台 第一步:用户同意授权.获取code 第二步:通过code换取网页授权access_token 详细步骤参考微信公众号平台 在页面地址输入授权 ...

  8. 【逆向】【Android微信】获取微信聊天记录

    2020-02-23 晴 郑州 出场人物 沉瓶 - 产品经理 饭咸 - 程序员 工作环境 硬件:MacBook Pro (Retina, 13-inch, Early 2015) 硬件相应系统:mac ...

  9. android微信登录获取微信账号,微信小程序--登录授权,一键获取用户微信手机号并登录...

    一.前言 微信小程序登录流程时序 说明: 小程序调用wx.login() 获取 临时登录凭证code ,并回传到开发者服务器 开发者服务器以code换取 用户唯一标识openid 和 会话密钥sess ...

最新文章

  1. 关于数据库group by需要注意
  2. springboot:自动配置原理入门
  3. c#如何通过ftp上传文件_ftp自动上传文件,如何设置ftp自动上传文件及环境配置...
  4. swiper动态加载数据左右切换失效
  5. 我看ITIL在中国(六):如何建立有中国特色的IT运维管理平台【二】
  6. java quartz实例_一个非常简单的quartz例子
  7. Python_60之迭代器模块
  8. python文件都是脚本吗_脚本语言系列之Python | Python文件IO
  9. Ajax 重新绑定 webgrid 数据,绑定的WebGrid形式AJAX(Bind WebGrid form AJAX)
  10. 递归算法经典实例小结(C#实现)
  11. nginx配置选项try_files详解
  12. NodeJS 基础 API
  13. 画流动虚线框(java)
  14. 如何理解结构化、非结构化和半结构化数据?
  15. API接口名称(item_get - 根据ID取商品详情)[item_search,item_get,item_search_shop等]
  16. Windows CMD命令大全(值得收藏)
  17. softlayer iso_使用IBM SoftLayer标记简化操作
  18. 使用 ALT 键输入特殊符号
  19. informa医药数据库
  20. 百度网盘免费加速下载

热门文章

  1. 阿里fastjson_温绍锦:初心不改的阿里初代开源人 | Gitee 封面人物第18期
  2. Vuex——黑马头条项目(vuex体验版)
  3. 2023.3.8国内免费100个HTTP代理IP
  4. 技术文章 | 企业网站为什么要考虑SEO优化?
  5. linux 企鹅 桌面程序,Linux 企鹅进军桌面,还有多远?(上)
  6. 谁都可以做-几分钱打造完美DIY面膜 - 健康程序员,至尚生活!
  7. 解决element ui 使用container布局时,容器布满全屏
  8. java 获得 大盘 开盘_教你利用开盘十分钟判定当日大盘强弱(建议收藏!)
  9. [转贴]ERP实施呕心沥血谈
  10. XML 外部实体注入漏洞