日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

微信公众号授权登录两种方式

發布時間:2023/12/13 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 微信公众号授权登录两种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

微信網頁授權: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

總結

以上是生活随笔為你收集整理的微信公众号授权登录两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。