一、登錄微信公眾號的測試環(huán)境,找到“網(wǎng)頁授權(quán)獲取用戶基本信息”點擊修改,添加上自己的回調(diào)地址域名。測試時可以寫IP:端口號,正式環(huán)境只支持域名不要寫http://或https://。
?二、步驟:
?1、用戶同意授權(quán),獲取code:
? ? ? 參考鏈接:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
- scope的參數(shù)有2種:一種是snsapi_base權(quán)限只能獲取OpenId而且不會彈出授權(quán)頁面,另為一種是snsapi_userinfo權(quán)限會彈出授權(quán)頁面并且會獲取用戶信息。
- redirect_url:是回調(diào)地址,當(dāng)請求成功的時候會重定向到回調(diào)頁面,并返回code和state?;卣{(diào)地址必須是設(shè)置的回調(diào)地址域名下且需要進行urlEncode處理。
如:http://xxxx.xxxx.cn/test/wechat/redirect?code=CODE&state=1
?2、通過code獲取access_token:
???????參考鏈接:
https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code
?3、通過access_token獲取用戶信息:
? ? ? ?參考鏈接:
https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId+"&lang=en
三、代碼
1、通過消息模板推送授權(quán)鏈接:文章來源:http://www.zghlxwxcb.cn/news/detail-599416.html
/**
* 推送給新關(guān)注的用戶授權(quán)
* @param openId 用戶的openId
*/
public static void followMessage(String openId) {
try {
// 發(fā)送消息的時間
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(new Date());
// 獲取AccessToken的值
String accessToken = WaChatServiceUtil.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
// 獲取消息模版id
String Message = MessageTemplateEnum.FOLLOW_MESSAGE_TEMPLATE.getKey();
// 回調(diào)地址,需要用urlEncode對鏈接進行處理,微信公眾號中需要將授權(quán)回調(diào)頁面域名配置好回調(diào)地址需要在回調(diào)頁面域名下
String callbackUrl = "http%3A%2F%2F"+DOMAINNAME+"%2Fsampling-merchant-web%2Fherman%2Ftest%2Fwechat%2Fredirect.cgi";
// appId
String appId = WaChatServiceUtil.getAPPID();
// 授權(quán)地址,用戶點擊后將進行授權(quán),返回給回調(diào)地址code值,state為任意a-zA-Z0-9的參數(shù)值,最多128字節(jié)
String empowerUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + callbackUrl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
// 設(shè)置模版消息的內(nèi)容
WeChatTemplate wc = new WeChatTemplate();
wc.setTouser(openId);
wc.setTemplate_id(Message);
wc.setUrl(empowerUrl);
Map<String, TemplateData> m = new HashMap<>();
m.put("first", new TemplateData("歡迎關(guān)注mamain,點擊下方進行授權(quán)", "#000000"));
m.put("time", new TemplateData(dateFormat, "#000000"));
m.put("url", new TemplateData("點擊進行授權(quán),獲取您的微信名。", "#173177"));
m.put("remark", new TemplateData("有疑問請聯(lián)系客服!", "#FF0000"));
wc.setData(m);
//post發(fā)送授權(quán)消息模版
String rString = WeChatUtils.sendPost(url, JSON.toJSONString(wc));
logger.info("發(fā)送授權(quán)模版消息結(jié)果為:{}", rString);
} catch (Exception e) {
logger.error("發(fā)送授權(quán)模版消息失敗!", e);
}
}
2、獲取用戶信息文章來源地址http://www.zghlxwxcb.cn/news/detail-599416.html
/**
* 回調(diào):微信授權(quán),獲取用戶code
*/
@RequestMapping(value = "wechat/redirect", method = RequestMethod.GET,produces = "text/html; charset=UTF-8")
@ResponseBody
public String wechatRedirect(HttpServletRequest request) throws IOException {
try {
// 獲取用戶code
String code = request.getParameter("code");
if (null==code){
logger.error("code為空!");
return "error:code為空,授權(quán)失敗";
}
logger.info("code為{}",code);
// 獲取 access_token
String appid= WaChatServiceUtil.getAPPID();
String appSecret=WaChatServiceUtil.getAPPSECRET();
String getToken="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";
JSONObject token = JSONObject.parseObject(WeChatUtils.sendGet(getToken));
Object access_token=token.get("access_token");
String openId= (String) token.get("openid");
logger.info("access_token:{},openid{}",access_token,openId);
// 獲取用戶信息
String getUser="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
JSONObject user = JSONObject.parseObject(WeChatUtils.sendGet(getUser));
String wechatName= (String) user.get("nickname");
logger.info("用戶名為:{},openid為:{}",wechatName,openId);
}catch (Exception e){
logger.error("獲取用戶信息異常!", e);
}
return "<h1 align=\"center\">您已授權(quán)成功</h1>";
}
到了這里,關(guān)于微信公眾號--根據(jù)用戶opneId獲取用戶信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!