微信授权(获取openid和userinfo信息)

获取openid和userinfo信息

一:基于微信公众号的授权登录,获取openid和用户信息。

第一步:用户同意授权,获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。

代码实现:

package com.cxb.otherWx;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.cxb.wx.accesstoken.Account;
/**
 * @author ChenXb
 *
 *         2018年4月19日
 *                     这里从授权到获取微信openid以及用户信息完结
 */
public class WxAuthor {
//微信授权的地址
private static final String AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

/*
* 获取网页授权url
*/
public static String getAuthorize_url() {
String REDIRECT_URI=null;
try {
//这里的url就是访问你后台的链接地址
REDIRECT_URI = URLEncoder.encode("http://www.joffro.com/Activity/sendMessage/sendMsg?appid=**************","UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//scope 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
//snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
String url = AUTHORIZE_URL.replace("APPID", Account.APPID).replace("REDIRECT_URI", REDIRECT_URI)
.replace("SCOPE", "snsapi_userinfo").replace("STATE", "123");
return url;
}

public static void main(String[] args) {
//这里的链接copy到微信浏览器里面访问
String authorize_url = getAuthorize_url();
System.out.println(authorize_url);
//根据上面的链接在微信里面访问到后台的配置链接  
//这些代码则是后台处理的逻辑,获取openid和userinfo等信息
/*String code = getPara("code");
log.error("*********code****"+code);
//根据code获取openid
String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID
+ "&secret="+APPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
JSONObject jsonObject = doGetJson(url);
String openid = jsonObject.getString("openid");
log.error("*****openid***>"+openid);
String accessToken = jsonObject.getString("access_token");
//根据openid获取用户信息
String getUserInfoUrl="https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken
+ "&openid="+openid
+ "&lang=zh_CN";
JSONObject userInfo = doGetJson(getUserInfoUrl);
log.error("*****userInfo****"+userInfo);*/

}
}


        //以下代码是后台程序中用到的   这里是上面配置的访问链接的后台地址
public static final String APPID="************************"; 
 //这里修改为自己的APPID
public static final String APPSECRET="*********************";    //这里修改为自己的APPSECRET
public static JSONObject doGetJson(String url){
JSONObject jsonObject=null;
//这个方法过时了 换成下面的方式了
//DefaultHttpClient client=new DefaultHttpClient();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet(url);
HttpResponse response = null;
try {
response = httpclient.execute(httpGet);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity=response.getEntity();
if(entity!=null){
String result = null;
try {
result = EntityUtils.toString(entity,"UTF-8");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonObject = JSONObject.fromObject(result);
}
//处理完后,释放链接
httpGet.releaseConnection();
return jsonObject;

}

附录:


猜你喜欢

转载自blog.csdn.net/qq_33371766/article/details/80002831