https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=http%3A//www.example.com/wechat8008/index&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirecthtml
下圖爲scope等於snsapi_userinfo時的受權頁面:python
http://www.example.com/wechat8008/index?code=021ci0np18bW0j0IpHnp1eXGmp1ci0ns&state=1nginx
若是用戶贊成受權,頁面將跳轉至 redirect_uri/?code=CODE&state=STATE。若用戶禁止受權,則重定向後不會帶上code參數,僅會帶上state參數redirect_uri?state=STATEjson
以上是贊成受權的示例url,該url會訪問你的視圖函數,只須要留意獲取code參數flask
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_codeapi
該url包含4個參數:服務器
其中appid和secret在填寫你本身公衆號設置裏的的實際參數,微信
code是前面訪問時帶過來的url參數,獲取之code = request.args.get("code")app
grant_type=authorization_code固定寫法函數
url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code" % (WECHAT_APPID, WECHAT_APPSECRET, code)
我這裏用urllib2發送請求 response = urllib2.urlopen(url)
返回值爲一個響應體數據,解析之resp_dict = json.loads(response.read())
正確時返回的JSON數據包以下:
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"snsapi_userinfo" }
https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=open_id&lang=zh_CN
該url包含3個參數:
access_token是上一步請求返回的參數,提取之 access_token = resp_dict.get("access_token")
open_id同上open_id = resp_dict.get("openid")
語言lang=zh_CN
url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN" % (access_token, open_id)
一樣用urllib2發送請求
response = urllib2.urlopen(url)
返回值
正確時返回的JSON數據包以下:
{ "openid":" OPENID", " nickname": NICKNAME, "sex":"1", "province":"PROVINCE" "city":"CITY", "country":"COUNTRY", "headimgurl":"http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ], "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }
這裏就能夠根據本身的需求使用用戶信息了。
from flask import Flask, request, abort, render_template
import hashlib
import xmltodict
import time
import urllib2
import json
WECHAT_TOKEN = "python"
WECHAT_APPID = "wxe123456789"
WECHAT_APPSECRET = "123456789"
app = Flask(__name__)
# www.example.com/wechat8008/index @app.route("/wechat8008/index") def index(): """讓用戶微信訪問的網頁頁面視圖""" # 從微信服務器中拿取用戶 的資料 # 1.拿取code參數 code = request.args.get("code") # print(1, code) if not code: return u"缺失code參數" # 2.向微信服務器發送http請求,獲取access_token url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code" % \ (WECHAT_APPID, WECHAT_APPSECRET, code) # 使用urllib2的urlopen方法發送請求 # 若是隻傳網址url參數,則默認使用http的get請求方式,返回響應對象 response = urllib2.urlopen(url) # 獲取響應體數據,微信返回的json數據 json_str = response.read() resp_dict = json.loads(json_str) #print(2, resp_dict) # 提取access_token if "errcode" in resp_dict: return u"獲取acces_token失敗" access_token = resp_dict.get("access_token") open_id = resp_dict.get("openid") # 用戶的編號 # 3.向微信服務器發送http請求,獲取用戶信息 url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN" % \ (access_token, open_id) response = urllib2.urlopen(url) # 讀取微信傳回的json的響應體數據 user_json_str = response.read() user_dict_data = json.loads(user_json_str) # print(user_dict_data) if "errcode" in user_dict_data: return u"獲取用戶信息失敗" else: # 將用戶的資料數據填充到頁面中 return render_template("index.html",user=user_dict_data) if __name__ == '__main__': app.run(port=8008, debug=True)
須要注意的是,微信只認域名和80端口。若是你80端口已被佔用,可用nginx轉發一下
server { # 監聽80端口 listen 80; # 本機 server_name localhost;
location /wechat8008 { proxy_pass http://127.0.0.1:8008; } }