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

该授权回掉页面域名为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:dd
URL 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;
}
}

微信之获取微信的openid(二)详细版的更多相关文章

  1. 微信获取用户的openid和详细信息

    获取用户的信息的原理,首先用户会点击一个url,这个url会包含一个参数redirect_uri,这个url是指向微信那边的服务器的,然后微信会把这个http请求重定向到redirect_uri,即我 ...

  2. 前端微信登录获取code,userInfo,openid

    getUser(e) { wx.getUserProfile({ desc: '用户完善会员资料', success: res => { let userInfo = res.userInfo; ...

  3. 微信授权获取用户openid前端实现

    近来,倒霉的后台跟我说让我拿个openid做微信支付使用,寻思很简单,开始干活.   首先引导用户打开如下链接,只需要将appid修改为自己的就可以,redirect_url写你的重定向url   h ...

  4. webform获取微信用户的授权

    这是一个利用webform做出来的简单demo,微信授权,获取微信用户的基本信息.方便以后加深记忆. public partial class Index : System.Web.UI.Page { ...

  5. 微信公众号开发系列-获取微信OpenID

    在微信开发时候在做消息接口交互的时候须要使用带微信加密ID(OpenId),下面讲讲述2中类型方式获取微信OpenID.接收事件推送方式和网页授权获取用户基本信息方式获取. 1.通过接收被动消息方式获 ...

  6. PHP PC端微信扫码支付【模式二】详细教程-附带源码(转)

    博主写这破玩意儿的时候花了大概快两天时间才整体的弄懂逻辑,考虑了一下~还是把所有代码都放出来给大家~抱着开源大无私的精神!谁叫我擅长拍黄片呢?同时也感谢我刚入行时候那些无私帮过我的程序员们! 首先还是 ...

  7. Android (微信扫码登录) 获取微信二维码+扫码登录

    话不多说  直接上菜! 一.因为是微信扫码登录,所有要在微信开放平台  微信开放平台 (qq.com) 进行注册----- 如下 1.资源中心 里面也有详细的官方讲解,里面也有demo  可以下载 2 ...

  8. 微信接口-获取用户openid基本信息

    一.协助获取微信用户openid功能 https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri= ...

  9. 微信公众平台如何获取用户的OpenID(一)

    如何获取用户的OpenID,对于微信开发模式下的开发来说,那就是一个非常简单的小功能了.简单介绍一下我是怎样去获取OpenID的. 微信服务器与公众账号服务器交互的信息可以分为3类:请求消息.事件和响 ...

随机推荐

  1. Ubuntu下重启mysql

    启动mysql: 方式一:sudo /etc/init.d/mysql start 方式二:sudo service mysql start 停止mysql: 方式一:sudo /etc/init.d ...

  2. QT 学习基础问题记录

    1. connect 函数 需要先创建发送者和接收者实例,并且信号函数和槽函数如果有参数,需要在 connect 函数使用时指定相关参数类型. 2.窗口控件设置 设置窗口的最大化.最小化.问号提示等控 ...

  3. C++中几种字符串表示方法

    最近学习C++时,被几种字符串搞的有点乱,这里记录一下. c++中有两种风格字符串,分别是: C++风格字符串 C风格字符串 它们各自的声明方式如下: void main(){ string a = ...

  4. vue中sessionStorage的使用

    转载:https://www.cnblogs.com/denken/p/11197612.html localStorage 和 sessionStorage 属性允许在浏览器中存储 key/valu ...

  5. [SOJ #498]隔膜(2019-10-30考试)/[POJ2152]Fire

    题目大意:有一棵$n$个点的带边权树,第$i$个点有两个值$w_i,d_i$,表示在这个点做标记的代价为$w_i$,且这个点距离$d_i$以内至少要有一个点被标记,为最小代价.$n\leqslant6 ...

  6. arc079

    D. Decrease (Contestant ver.) 大意: 每次操作选一个最大数$-n$,其余数全$+1$. 要求构造一个序列$a$, 使得恰好$k$次操作后最大值不超过$n-1$. 只要让$ ...

  7. 介绍ArcGIS中各种数据的打开方法——mdb(个人数据库)

    3.打开存储在Access GeoDatabase的要素类 使用工作空间打开一个Access库中的一个要素类. private void OpenWorkspaceFromFileAccess(str ...

  8. C#泛型集合之——链表

    链表基础 1.概述:C#中泛型集合中的链表—LinkedList 是一个双向链表,其结点为LinkedListNode 结构 其中,结点结构包含:Next,Previous,Value三部分.且结点中 ...

  9. Entity Framework Codefirst的配置步骤

    Entity Framework Codefirst的配置步骤: (1) 安装命令: install-package entityframework (2) 创建实体类,注意virtual关键字在导航 ...

  10. 自学Python编程的第\七天----------来自苦逼的转行人

    2019-09-17-23:09:48 今天学的内容是有关小数据池的,学的有点懵逼,感觉越来越难学了,但是得坚持下去 明天学习下一个课程时,感觉要跟不上,看来明天得先看好几遍今天的内容 不然肯定会听的 ...