无标题文章

转 风吹过01


 微信公众号获取用户的openid 

第一步:理解逻辑。

获得微信的openid,需要先访问微信提供的一个网址:这个网址名为url1,下面有赋值。通过这个网址,微信用来识别appid信息,在这个网址中,有一个属性redirect_uri,是微识别完appid后,进行跳转的操作,可以是网页,也可以是servlet,我这里用的是servlet,微信跳转到这个servlet中,会传递一个code值,我们用这个code值,再访问微信提供的另一网址url2,下面有赋值,则可以获得json类型的返回数据,其中就有我们需要的openid。

String url1 = https://open.weixin.qq.com/connect/oauth2/authorize

appid=APPID&

redirect_uri=REDIRECT_URI

&response_type=code

&scope=snsapi_base

&state=STATE

#wechat_redirect

String url2 = https://api.weixin.qq.com/sns/oauth2/access_token?

appid=AppId

&secret=AppSecret

&code=CODE

&grant_type=authorization_code

第二步:注意事项

知道逻辑之后,我们需要具体操作,在实际操作中,我们还需要注意几点,首先,是理解我们第一个访问的网址url1,它有6个参数。

appid,填写自己的appid.

redirect_uri,填写微信识别成功之后,跳转的url(需要encode编码)。

response_type,就填code,不用修改。

scope,可填(snsapi_base和snsapi_userinfo两个值,其中前者为只获得openid,不需要用户授权,后者为获得用户信息,需要用户授权)

state,自定义参数,可随意填也可不填。

#wechat_redirect,指定在微信内跳转,平时可以不填,在302重定向时,必须填!

这里值得注意的有两点,第一点,redirect_uri需要encode编码,否则页面会显示“redirect_ur参数错误!”!

第二点,redirect_uri网址的域名必须是,你在微信公众平台账号中填写授权回调页的域名,具体需要登录微信公众平台后台,在用户信息那里点击修改,填上自己的域名即可,注意:授权回调页中的域名没有http://!

理解第二个网址,它有4个参数。

appid,登录公众号 就有。

secret,登录公众号就有。

code,访问url1,在servlet中,获得code。

grant_type,不用改,填它authorization_code即可!

第三步:代码:

servlet中的代码:

String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&stat e=STATE#wechat_redirect";

try {

String appid= "wx804e5da12c0a50c0";

String REDIRECT_URI = "http://ipcloud.lstn.cn:8081/PeanutWx/UserOpenIDServlet";

url  = url.replace("APPID", WxMenuUtils.urlEnodeUTF8(appid));

url  = url.replace("REDIRECT_URI",WxMenuUtils.urlEnodeUTF8(REDIRECT_URI));

response.sendRedirect(url);

} catch (Exception e) {

e.printStackTrace();

}

其中getopendid()方法的代码:

public static String getopendid(String code) throws ParseException, IOException {

String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppId&secret=AppSecret&code=CODE&grant_type=authorization_code";

url = url.replace("AppId", appid)

.replace("AppSecret", secret)

.replace("CODE", code);

HttpGet get = HttpClientConnectionManager.getGetMethod(url);

HttpResponse response = httpclient.execute(get);

String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");

JSONObject jsonTexts = (JSONObject) JSON.parse(jsonStr);

String openid = "";

if (jsonTexts.get("openid")!=null) {

openid = jsonTexts.get("openid").toString();

}

return openid;

}

最后附上模拟浏览器提交请求的代码:

package ;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.conn.ClientConnectionManager;

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.scheme.SchemeRegistry;

import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientConnectionManager {

/**

* 获取SSL验证的HttpClient

* @param httpClient

* @return

*/

public static HttpClient getSSLInstance(HttpClient httpClient){

ClientConnectionManager ccm = httpClient.getConnectionManager();

SchemeRegistry sr = ccm.getSchemeRegistry();

sr.register(new Scheme("https", MySSLSocketFactory.getInstance(), 443));

httpClient =  new DefaultHttpClient(ccm, httpClient.getParams());

return httpClient;

}

/**

* 模拟浏览器post提交

*

* @param url

* @return

*/

public static HttpPost getPostMethod(String url) {

HttpPost pmethod = new HttpPost(url); // 设置响应头信息

pmethod.addHeader("Connection", "keep-alive");

pmethod.addHeader("Accept", "*/*");

pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

pmethod.addHeader("Host", "mp.weixin.qq.com");

pmethod.addHeader("X-Requested-With", "XMLHttpRequest");

pmethod.addHeader("Cache-Control", "max-age=0");

pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");

return pmethod;

}

/**

* 模拟浏览器GET提交

* @param url

* @return

*/

public static HttpGet getGetMethod(String url) {

HttpGet pmethod = new HttpGet(url);

// 设置响应头信息

pmethod.addHeader("Connection", "keep-alive");

pmethod.addHeader("Cache-Control", "max-age=0");

pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");

pmethod.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");

return pmethod;

}

}

只要按照逻辑来,在加上附上的代码,可以获得openid,到此搞定!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 9af0e05c6c75阅读 375评论 0 0
  • # 一度蜜v3.0协议 --- # 交互协议 [TOC] ## 协议说明 ### 请求参数 下表列出了v3.0版协...
    c5e350bc5b40阅读 665评论 0 0
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,181评论 1 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 我爱过一个美丽的朋友,她如春天的风夏日的荷,她孤独起来有秋的寂寥,也不失冬的傲雪寒霜。多希望她生命中有一部...
    鲜研阅读 294评论 0 4