微信公眾號授權登錄兩種方式


微信網頁授權:scope有兩種方式:

scope=snsapi_userinfo:需要用戶點擊確認,才會去登錄
scope=snsapi_base:不需要用戶點擊確認,默認登錄

var callbackUrl ="http://www.xxx.com";
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=' +
encodeURI(callbackUrl) + '&response_type=code'+'&scope=snsapi_userinfo#wechat_redirect';
window.location.href = url;


var callbackUrl = 'http://www.xxx.com';
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=' +
encodeURI(callbackUrl) + '&response_type=code'+'&scope=snsapi_base#wechat_redirect';
window.location.href = url;


兩種方式,都可以拿到code,再通過code,獲取token和openid

private String getWxToken(String code) throws IOException {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WxPayParam.APPID +
"&secret="+WxPayParam.APPSECRET+"&code=" + code + "&grant_type=authorization_code";
PostMethod post = new PostMethod(url);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response = post.getResponseBodyAsString();
logger.info("response: " + response);
post.releaseConnection();
return response;
}

{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}


public class JsonUtils {

/**
* json字符串轉成map類型數據
*/
public static Map<String, String> jsonStringToMap(String jsonString) {
return new GsonBuilder().create().fromJson(jsonString, new TypeToken<Map<String, String>>(){}.getType());
}

/**
* json字符串轉成map對象
* @param jsonString
* @return
*/
public static Map<String, Object> jsonStringToObjectMap(String jsonString) {
return new GsonBuilder().create().fromJson(jsonString, new TypeToken<Map<String, Object>>(){}.getType());
}

}


String token = JsonUtils.jsonStringToMap(obj).get("access_token");
String openid = JsonUtils.jsonStringToMap(obj).get("openid");
String userInfo = getGzhUserInfo(token, openid);


private String getGzhUserInfo(String token, String openid) {
try {
String url ="https://api.weixin.qq.com/sns/userinfo?access_token="+token+"&openid="+openid+"&lang=zh_CN";
GetMethod get = new GetMethod(url);
HttpClient client = new HttpClient();
client.executeMethod(get);
String userInfo = get.getResponseBodyAsString();
get.releaseConnection();
userInfo = new String(userInfo.getBytes("iso-8859-1"), "utf-8").
replace("[", "\"").replace("]", "\"");
return userInfo;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

還有一個獲取用戶信息的接口:

https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
(此接口的access_token 是接口基礎調用access_token 不是網頁授權access_token),只有注了公眾號后,才能調用成功

基礎接口的token 獲取接口是

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

用戶網頁授權access_token 獲取接口地址是

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

{
"openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}


更多詳細內容請查看官網:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM