1.網頁受權 php/wx-sample.phpphp
2.設置分享內容php/wx-sample.phpjson
3.處理用戶消息wx.phpapi
微信受權登陸數組
用戶贊成受權,獲取code微信
在確保微信公衆帳號擁有受權做用域(scope參數)的權限的前提下(服務號得到高級接口後,默認擁有scope參數中的snsapi_base和snsapi_userinfo),引導關注者打開以下頁面:app
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
參數說明ui
參數是否必須說明url
appid是公衆號的惟一標識spa
redirect_uri是受權後重定向的回調連接地址,請使用urlencode對連接進行處理code
response_type是返回類型,請填寫code
scope是應用受權做用域,snsapi_base (不彈出受權頁面,直接跳轉,只能獲取用戶openid),snsapi_userinfo (彈出受權頁面,可經過openid拿到暱稱、性別、所在地。而且,即便在未關注的狀況下,只要用戶受權,也能獲取其信息)
state否重定向後會帶上state參數,開發者能夠填寫a-zA-Z0-9的參數值,最多128字節
#wechat_redirect是不管直接打開仍是作頁面302重定向時候,必須帶此參數
[demo]:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx35d17e1582ed4ab0&redirect_uri=http%3A%2F%2Fchina.bringmehope.org%2Fwp-admin%2Fthemes%2Fbmh%2Fwx.php&response_type=code&scope=snsapi_userinfo&state=sigstatus#wechat_redirect
受權成功後微信重定向到以下地址
http://china.bringmehope.org/wp-admin/themes/bmh/wx.php?code=031Zb2w12kUEI21ueyt12bf6w12Zb2w9&state=sigstatus
根據code參數獲取access_token
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx35d17e1582ed4ab0&secret=1cbe9bb6b6591097dd7b43481c5f4d43&code=041r9d0S04Lgnc2kjP3S0rBj0S0r9d0Q&grant_type=authorization_code
[response]:
{"access_token":"t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc","expires_in":7200,"refresh_token":"XBHRNtBFFFtcWawO--RcDf77x5wVFOo8VYAqUeYI48_KpeJ14_wIUamdT-d_Av9ht8olIwateABuwEmDzmxEdMvLYVu5E4GYZ4DwRWMpuOg","openid":"o6HFmuGtadMNbuicrjpByl4gBeTA","scope":"snsapi_userinfo"}
刷新access_token(若是須要)
因爲access_token擁有較短的有效期,當access_token超時後,可使用refresh_token進行刷新,refresh_token擁有較長的有效期(7天、30天、60天、90天),當refresh_token失效的後,須要用戶從新受權。
獲取第二步的refresh_token後,請求如下連接獲取access_token:
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx35d17e1582ed4ab0&grant_type=refresh_token&refresh_token=XBHRNtBFFFtcWawO--RcDf77x5wVFOo8VYAqUeYI48_KpeJ14_wIUamdT-d_Av9ht8olIwateABuwEmDzmxEdMvLYVu5E4GYZ4DwRWMpuOg
[response]:
{"openid":"o6HFmuGtadMNbuicrjpByl4gBeTA","access_token":"t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc","expires_in":7200,"refresh_token":"XBHRNtBFFFtcWawO--RcDf77x5wVFOo8VYAqUeYI48_KpeJ14_wIUamdT-d_Av9ht8olIwateABuwEmDzmxEdMvLYVu5E4GYZ4DwRWMpuOg","scope":"snsapi_base,snsapi_userinfo,"}
拉取用戶信息(需scope爲 snsapi_userinfo)
https://api.weixin.qq.com/sns/userinfo?access_token=t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc&openid=o6HFmuGtadMNbuicrjpByl4gBeTA&lang=zh_CN
[response]:
{"openid":"o6HFmuGtadMNbuicrjpByl4gBeTA","nickname":"漂浮的雲","sex":1,"language":"zh_CN","city":"深圳","province":"廣東","country":"中國","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/f8OjTfQlh73iaK0lct9FHKx8icqY6leclpsy485XpezqDibibcBfIXlwmKL3WIEVUJyVM1rvn9iaDjr7YoOLKLwj77E64f9ZV22z1\/0","privilege":[]}
參數描述
openid用戶的惟一標識
nickname用戶暱稱
sex用戶的性別,值爲1時是男性,值爲2時是女性,值爲0時是未知
province用戶我的資料填寫的省份
city普通用戶我的資料填寫的城市
country國家,如中國爲CN
headimgurl用戶頭像,最後一個數值表明正方形頭像大小(有0、4六、6四、9六、132數值可選,0表明640*640正方形頭像),用戶沒有頭像時該項爲空。若用戶更換頭像,原有頭像URL將失效。
privilege用戶特權信息,json 數組,如微信沃卡用戶爲(chinaunicom)
unionid只有在用戶將公衆號綁定到微信開放平臺賬號後,纔會出現該字段。詳見:獲取用戶我的信息(UnionID機制)
檢驗受權憑證(access_token)是否有效
https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
[demo]:
https://api.weixin.qq.com/sns/auth?access_token=t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc&openid=o6HFmuGtadMNbuicrjpByl4gBeTA
[response]:
(正常)
{"errcode":0,"errmsg":"ok"}
(失敗)
{"errcode":40003,"errmsg":"invalid openid, hints: [ req_id: R2xLVa0634ns80 ]"}