微信用戶授權後,獲取用戶的基本信息

 

微信開發文檔中寫的不夠清楚,做出來的是網頁授權後獲取用戶信息,不是自己想要的,自己想做的是獲取用戶基本信息,在開發中總結了一下思路,開始的時候使用的是https://api.weixin.qq.com/sns/oauth2/  去獲取用戶信息,後來發現獲取的信息只是網頁授權後能夠獲取的用戶信息,

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

這並不是我想要的,下面是我重寫的思路:

 

1、引導同意授權

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx234b95a4b7fa5574&redirect_uri=REDIRECT_URL&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

用戶同意授權之後,微信平臺會在回調地址中攜帶授權碼code訪問服務REDIRECT_URL

 

2、服務中從請求中獲取code

// 如果用戶同意授權,頁面將跳轉至redirect_uri/?code=CODE&state=STATE。

String code =request.getParameter("code");

 

3、使用appid appsecretid code 獲取用戶的openid

String access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID+"&secret="+APPSECRET+"&code=" + code +"&grant_type=authorization_code";

 

4、使用 appid  appsecretid 獲取用戶的access_token

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

                           

5、使用openid access_token 獲取用戶的信息

String GET_USERINFO_URL="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ access_token+ "&openid=" + openid +"&lang=zh_CN";

 

{
"user_info_list": [
{
"subscribe": 1,
"openid": "otvxTs4dckWG7imySrJd6jSi0CWE",
"nickname": "iWithery",
"language": "zh_CN",
"sex": 1, "city": "揭陽",
"country": "中國",
"province": "廣東",
"headimgurl": "http://thirdwx.qlogo.cn/mmopen/xbIQx1GRqdvyqkMMhEaGOX802l1CyqMJNgUzKP8MeAeHFicRDSnZH7FY4XB7p8XHXIf6uJA2SCunTPicGKezDC4saKISzRj3nz/0",
"subscribe_time": 1434093047, "unionid": "oR5GjjgEhCMJFyzaVZdrxZ2zRRF4", "remark": "", "groupid": 0, "tagid_list":[128,2],
"openid": "otvxTs_JZ6SEiP0imdhpi50fuSZg"
"subscribe_scene": "ADD_SCENE_QR_CODE", "qr_scene": 98765, "qr_scene_str": "" }, { "subscribe": 0, } ]
}

 

代碼就不上了,每個人的實現方法都不一樣。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章