最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]微信授权登陆后获取用户信息后用Python解码

Python crifan 3230浏览 0评论

已经解决了:

[已解决]微信授权后获取用户信息失败:40001 invalid credential access_token is invalid or not latest hints req_id

然后获得到了用户信息,但是其中包含不可堵的unicode字符:

type(respUserInfoDict)=<type ‘dict’>, respUserInfoDict={u’province’: u’\xe6\xb1\x9f\xe8\x8b\x8f’, u’openid’: u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’, u’headimgurl’: u’http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0′, u’language’: u’zh_CN’, u’city’: u’\xe8\x8b\x8f\xe5\xb7\x9e’, u’country’: u’\xe4\xb8\xad\xe5\x9b\xbd’, u’sex’: 1, u’privilege’: [], u’nickname’: u’\xe7\xa4\xbc\xe8\xb2\x8c’}

现在希望,用Python,去解码成为可见的中文字符

结果试了半天,还是有点没搞懂:

# type(respUserInfoDict) = < type ‘dict’ >, respUserInfoDict = {u’province’: u’\xe6\xb1\x9f\xe8\x8b\x8f’,
#                               u’openid’: u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’,
#                               u’headimgurl’: u’http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0′,
#                               u’language’: u’zh_CN’, u’city’: u’\xe8\x8b\x8f\xe5\xb7\x9e’,
#                               u’country’: u’\xe4\xb8\xad\xe5\x9b\xbd’, u’sex’: 1, u’privilege’: [],
#                               u’nickname’: u’\xe7\xa4\xbc\xe8\xb2\x8c’}
province = respUserInfoDict[‘province’]
city = respUserInfoDict[‘city’]
country = respUserInfoDict[‘country’]
nickname = respUserInfoDict[‘nickname’]
app.logger.debug(‘province=%s, city=%s, country=%s, nickname=%s’, province, city, country, nickname)
app.logger.debug(‘type(province)=%s’, type(province))
respUserInfoStr = jsonToStr(respUserInfoDict)
app.logger.debug(‘type(respUserInfoStr)=%s, respUserInfoStr=%s’, type(respUserInfoStr), respUserInfoStr)
decodedUserInfo = respUserInfoStr.decode("unicode-escape")
app.logger.debug(‘type(decodedUserInfo)=%s, decodedUserInfo=%s’, type(decodedUserInfo), decodedUserInfo)
#callbackOkStr = u"回调成功: code=%s, state=%s, respAccessTokenStr=%s, respUserInfoStr=%s" % (paraCode, paraState, respAccessTokenStr, decodedUserInfo)
callbackOkStr = u"回调成功: " + "code=%s, state=%s, respAccessTokenStr=%s, respUserInfoStr=%s" % (paraCode, paraState, respAccessTokenStr, decodedUserInfo)
return make_re

输出:

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:89]:
type(respUserInfoDict)=<type ‘dict’>, respUserInfoDict={u’province’: u’\xe6\xb1\x9f\xe8\x8b\x8f’, u’openid’: u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’, u’headimgurl’: u’http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0′, u’language’: u’zh_CN’, u’city’: u’\xe8\x8b\x8f\xe5\xb7\x9e’, u’country’: u’\xe4\xb8\xad\xe5\x9b\xbd’, u’sex’: 1, u’privilege’: [], u’nickname’: u’\xe7\xa4\xbc\xe8\xb2\x8c’}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:100]:
province=æ±è, city=èå·, country=中å½, nickname=礼è²

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:101]:
type(province)=<type ‘unicode’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:104]:
type(respUserInfoStr)=<type ‘str’>, respUserInfoStr={
  "province": "\u00e6\u00b1\u009f\u00e8\u008b\u008f", 
  "openid": "oswxxxxxxxxxxxxxxxxxxxxxxVVY", 
  "headimgurl": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0", 
  "language": "zh_CN", 
  "city": "\u00e8\u008b\u008f\u00e5\u00b7\u009e", 
  "country": "\u00e4\u00b8\u00ad\u00e5\u009b\u00bd", 
  "sex": 1, 
  "privilege": [], 
  "nickname": "\u00e7\u00a4\u00bc\u00e8\u00b2\u008c"
}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:106]:
type(decodedUserInfo)=<type ‘unicode’>, decodedUserInfo={
  "province": "æ±è", 
  "openid": "oswxxxxxxxxxxxxxxxxxxxxxxVVY", 
  "headimgurl": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0", 
  "language": "zh_CN", 
  "city": "èå·", 
  "country": "中å½", 
  "sex": 1, 
  "privilege": [], 
  "nickname": "礼è²"
}

好像是:

u’\xe7\xa4\xbc\xe8\xb2\x8c’

竟然是unicode

但是此处去解码这个unicode,却又出错了:

[已解决]Python给unicode去编码出错:UnicodeEncodeError gbk codec can’t encode character u’\xe6′ in position 0 illegal multibyte sequence

以及:

[已解决]Python中如何给http的get请求中所使用的request库中设置字符编码为UTF-8

然后:

[已解决]Python中把dict的json对象格式化为带缩进的字符串

最后用代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from flask import Flask, g, request, make_response
from flask import abort, redirect, url_for
from flask import jsonify
from wechat_sdk import WechatConf
from wechat_sdk import WechatBasic
# from wechat_sdk import WechatBaseCrypto
# from wechat_sdk.lib.crypto.crypto import WechatBaseCrypto
# from wechat_sdk.lib.crypto import WechatBaseCrypto
from wechat_sdk.exceptions import ParseError
from wechat_sdk.messages import (
    TextMessage,
    ImageMessage,
    VoiceMessage,
    VideoMessage,
    ShortVideoMessage,
    LinkMessage,
    LocationMessage,
    EventMessage,
    UnknownMessage
)
app = Flask(__name__)
app.debug=True
wechatConf = WechatConf(
    token=’sipevents’,
    appid=’wx9xxxxxxxxxxxxxxd’,
    appsecret=’8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd’,
    # 可选项:normal/compatible/safe,分别对应于 明文/兼容/安全 模式
    #encrypt_mode=’safe’,
    encrypt_mode=’normal’,
    # 如果传入此值则必须保证同时传入 token, appid
    encoding_aes_key=’bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt’
)
wechat = WechatBasic(conf=wechatConf)
# wechat = WechatBaseCrypto(conf=wechatConf)
def jsonToStr(jsonDict, indent=2):
    return json.dumps(jsonDict, indent=2, ensure_ascii=False)
@app.route("/")
def index():
    requestArgs = request.args
    app.logger.debug(‘requestArgs=%s’, requestArgs)
    needRedirect = False
    paraCode = request.args.get(‘code’, ”)
    app.logger.debug(‘paraCode=%s’, paraCode)
    if not paraCode :
        needRedirect = True
    paraState = request.args.get(‘state’, ”)
    app.logger.debug(‘paraState=%s’, paraState)
    # if not paraState :
    #     needRedirect = True
    if needRedirect:
        # 1. redirect url
        redirect_uri = "http://hd.webonn.com"
        authorize_url = wechat.generate_oauth2_authorize_url(redirect_uri)
        app.logger.debug(‘redirect_uri=%s, authorize_url=%s’, redirect_uri, authorize_url)
        return redirect(authorize_url)
    else:
        # 2. get access token
        respAccessToken = wechat.get_oauth2_access_token(paraCode)
        app.logger.debug(‘respAccessToken=%s’, respAccessToken)
        # respAccessToken={u’access_token’: u’5Lnjm56ox1djVEFEIAdvp4HdcHVO1WsEWCDXVIS0zLZc3veMB4KXLMu843h_MpLJ6xLjlHGeclhhhSsErowWCeFMBMC3CbWX4zDezvu_D7M’, u’openid’: u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’, u’expires_in’: 7200, u’refresh_token’: u’QE-prEWTMHrDBqZGcJLYOg5fNqvpVL6qrp2YekXlyWq2agtwsGhO5IIxujpIBwXzUMy80n7d9xiWFZEwrNt10ilFpRkTbwP4cBzQ2df7eUw’, u’scope’: u’snsapi_userinfo’}
        respAccessTokenStr = jsonToStr(respAccessToken)
        app.logger.debug(‘respAccessTokenStr=%s’, respAccessTokenStr)
        # respAccessTokenStr = {
        #     "access_token": "K8EMRg7DTYnElLOgLix0yVIKeSg85KWmChnGK4PZ8T5N2EiHevQZEm6hyMBPfkOh5Hl3r0H_koLFmHuMFuGOXoyLMn-A7IT0ztqPKvi6TIY",
        #     "openid": "oswxxxxxxxxxxxxxxxxxxxxxxVVY",
        #     "expires_in": 7200,
        #     "refresh_token": "wWfqaQyxbAK4WUKwmPeTuSQ7_Nlnxz35xAZ5fu6ZlQseR526zKyemwA7YozbUmbGYSETIpGCi0T-HXMhgKXKVvi5Kon2_4_uWAwhNcxCRBI",
        #     "scope": "snsapi_userinfo"
        # }
        oauth2Access_token = respAccessToken[‘access_token’]
        oauth2Openid = respAccessToken[‘openid’]
        app.logger.debug(‘oauth2Access_token=%s, oauth2Openid=%s’, oauth2Access_token, oauth2Openid)
        respUserInfoDict = wechat.get_oauth2_userinfo(oauth2Access_token, oauth2Openid)
        app.logger.debug(‘type(respUserInfoDict)=%s, respUserInfoDict=%s’, type(respUserInfoDict), respUserInfoDict)
        # type(respUserInfoDict) = < type ‘dict’ >, respUserInfoDict = {u’province’: u’\xe6\xb1\x9f\xe8\x8b\x8f’,
        #                               u’openid’: u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’,
        #                               u’headimgurl’: u’http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0′,
        #                               u’language’: u’zh_CN’, u’city’: u’\xe8\x8b\x8f\xe5\xb7\x9e’,
        #                               u’country’: u’\xe4\xb8\xad\xe5\x9b\xbd’, u’sex’: 1, u’privilege’: [],
        #                               u’nickname’: u’\xe7\xa4\xbc\xe8\xb2\x8c’}
        province = respUserInfoDict[‘province’]
        city = respUserInfoDict[‘city’]
        country = respUserInfoDict[‘country’]
        nickname = respUserInfoDict[‘nickname’]
        app.logger.debug(‘province=%s, city=%s, country=%s, nickname=%s’, province, city, country, nickname)
        app.logger.debug(‘type(province)=%s’, type(province))
        # provinceEncodedUtf8 = province.encode(‘utf-8’)
        # app.logger.debug(‘provinceEncodedUtf8=%s’, provinceEncodedUtf8)
        # provinceEncodedGbk = province.encode(‘gbk’)
        # app.logger.debug(‘provinceEncodedGbk=%s’, provinceEncodedGbk)
        # encodedProvinceIso8859_1 = province.encode(‘ISO-8859-1’)
        # app.logger.debug(‘encodedProvinceIso8859_1=%s’, encodedProvinceIso8859_1)
        # decodedProvinceUnicode = encodedProvinceIso8859_1.decode(‘utf-8’)
        # app.logger.debug(‘decodedProvinceUnicode=%s’, decodedProvinceUnicode)
        respUserInfoStr = jsonToStr(respUserInfoDict)
        app.logger.debug(‘type(respUserInfoStr)=%s, respUserInfoStr=%s’, type(respUserInfoStr), respUserInfoStr)
        # decodedUserInfo = respUserInfoStr.decode("unicode-escape")
        # app.logger.debug(‘type(decodedUserInfo)=%s, decodedUserInfo=%s’, type(decodedUserInfo), decodedUserInfo)
        # jsonifiedUserInfo =  jsonify(respUserInfoDict)
        # app.logger.debug(‘type(jsonifiedUserInfo)=%s, jsonifiedUserInfo=%s’, type(jsonifiedUserInfo), jsonifiedUserInfo)
        #callbackOkStr = u"回调成功: code=%s, state=%s, respAccessTokenStr=%s, respUserInfoStr=%s" % (paraCode, paraState, respAccessTokenStr, decodedUserInfo)
        callbackOkStr = u"回调成功: " + u"code=%s, state=%s, respAccessTokenStr=%s, respUserInfoStr=%s" % (paraCode, paraState, respAccessTokenStr, respUserInfoStr)
        return make_response(callbackOkStr)
@app.route(‘/wechat_auth’, methods=[‘GET’, ‘POST’])
def wechat_auth():
    signature = request.args.get(‘signature’, ”)
    timestamp = request.args.get(‘timestamp’, ”)
    nonce = request.args.get(‘nonce’, ”)
    echostr = request.args.get(‘echostr’, ”)
    app.logger.debug(‘signature=%s, timestamp=%s, nonce=%s, echostr=%s’, signature, timestamp, nonce, echostr)
    if wechat.check_signature(signature, timestamp, nonce):
    # if wechat.check_signature(signature, timestamp, nonce, echostr):
        app.logger.debug("wechat check_signature OK")
        if request.method == ‘GET’ :
            respStr = echostr
            app.logger.debug(‘respStr=%s’, respStr)
            return make_response(respStr)
        else :
            # for POST
            requestMethod = request.method
            app.logger.debug(‘requestMethod=%s’, requestMethod)
            # requestMethod=POST
            requestData = request.data
            app.logger.debug(‘requestData=%s’, requestData)
            # <xml>
            #     <ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName>
            #     <Encrypt><![CDATA[6PBVvYed+IuFgEa4BuDeOaNwM3KLqv6uoWqQcSCY+SidbN2VBjWL+N88kp0GU2BvLXyzlvbGGOxvJnFIDRQCDAgBUBhk3YQMWJmR+bB3D5phuVEIFo1MoknxB4xsIi3wVQunlyljyFl43TCwCxIvBVxXeHsKSzYOxhFGL+2ZmelLp46WJRaNrrh8yLl2eK1pRCR09QAoNZISLUszfzlr4N9t4nnl8ZuQpYqEdWt4bH8xsqTRPd91TG6hSio/o46G9Ow2QA16j7l4q9HXV2fZEIdlSJma4Jepl7N9jBDwrjd4zi4MXwsJpQ0KBI3Q1LBojlipt8wk1xzuVzmyidkHzvDtfIa22ZTvEu4MDYdYa8BXRYk1EtGpW0eDCiF/NLOyQzJsFM1T7jGsxjrKUIcYPmLFbNW8qZEIEDkAhahbVCI=]]></Encrypt>
            # </xml>
            requestArgs = request.args
            app.logger.debug(‘requestArgs=%s’, requestArgs)
            # requestArgs=ImmutableMultiDict([(‘nonce’, u’1438950245′), (‘openid’, u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’), (‘signature’, u’cb962992182efd9f94f09560b893b20106408e49′), (‘timestamp’, u’1471573757′), (‘encrypt_type’, u’aes’), (‘msg_signature’, u’dc3336e6f85053f3563802f4e7b5398e3930d017′)])
            try:
                # decryptedReqData = wechat.conf.crypto.decrypt_message(requestData, signature, timestamp, nonce)
                # app.logger.debug(‘decryptedReqData=%s’, decryptedReqData)
                # app.logger.debug(‘decrypt post body data OK’)
                # wechat.parse_data(decryptedReqData)
                #wechat.parse_data(decryptedReqData, signature, timestamp, nonce)
                #wechat.parse_data(requestData, signature, timestamp, nonce)
                wechat.parse_data(requestData)
                app.logger.debug(‘parse post body data OK’)
            except ParseError:
                respStr = "parse post body failed"
                app.logger.debug(respStr)
                return wechat.response_text(respStr)
            messageId = wechat.message.id          # 对应于 XML 中的 MsgId
            messageTarget = wechat.message.target  # 对应于 XML 中的 ToUserName
            messageSource = wechat.message.source  # 对应于 XML 中的 FromUserName
            messageTime = wechat.message.time      # 对应于 XML 中的 CreateTime
            messageType = wechat.message.type      # 对应于 XML 中的 MsgType
            messageRaw = wechat.message.raw        # 原始 XML 文本,方便进行其他分析
            app.logger.debug(‘messageId=%s, messageTarget=%s, messageSource=%s, messageTime=%s, messageType=%s, messageRaw=%s’, messageId, messageTarget, messageSource, messageTime, messageType, messageRaw)
            if isinstance(wechat.message, TextMessage) :
                msgContent = wechat.message.content
                app.logger.debug(‘type(msgContent)=%s, msgContent=%s’, type(msgContent), msgContent)
                resonseTextContent = wechat.response_text(msgContent)
                app.logger.debug(‘resonseTextContent=%s’, resonseTextContent)
                return app.make_response(resonseTextContent)
            else :
                respStr = "Not Support type of POST"
                app.logger.debug(‘respStr=%s’, respStr)
                return wechat.response_text(respStr)
    else :
        app.logger.debug("wechat check_signature wrong")
        return wechat.response_text("wechat auth failed")
@app.route(‘/get_menu’, methods=[‘GET’])
def get_menu():
    respDict = wechat.get_menu()
    app.logger.debug(‘respDict=%s’, respDict)
    menuJson = respDict[‘menu’]
    app.logger.debug(‘menuJson=%s’, menuJson)
    menuJsonStr = jsonToStr(menuJson)
    app.logger.debug(‘menuJsonStr=%s’, menuJsonStr)
    return app.make_response(menuJsonStr)
@app.route(‘/create_menu’, methods=[‘GET’, ‘POST’])
def create_menu():
    menu_info = {
        ‘button’: [
            {
                ‘type’: ‘click’,
                ‘name’: ‘今日活动’,
                ‘key’: ‘GET_TODAY_EVENTS’
            },
            {
                ‘type’: ‘view’,
                ‘name’: ‘我的活动’,
                ‘url’: ‘http://hd.webonn.com/’
            }
        ]
    }
    app.logger.debug(‘menu_info=%s’, menu_info)
    respDict = wechat.create_menu(menu_info)
    app.logger.debug(‘respDict=%s’, respDict)
    menuJsonStr = jsonToStr(menu_info)
    app.logger.debug(‘menuJsonStr=%s’, menuJsonStr)
    if respDict[‘errcode’] == 0 :
        createdMenuOkStr = "create ok for menu: %s" % (menuJsonStr)
        app.logger.debug(‘createdMenuOkStr=%s’, createdMenuOkStr)
        return app.make_response(createdMenuOkStr)
    else:
        failCreateMenuStr = "Fail to create menu: %s" % (menuJsonStr)
        app.logger.debug(‘failCreateMenuStr=%s’, failCreateMenuStr)
        failCreateMenuStr += ", reason: %s" % (respDict[‘errmsg’])
        app.logger.debug(‘failCreateMenuStr=%s’, failCreateMenuStr)
        return app.make_response(failCreateMenuStr)
if __name__ == ‘__main__’:
    app.run(debug=True)

可以打印出log:

<div–<——————————————————————————
DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:51]:
requestArgs=ImmutableMultiDict([])

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:56]:
paraCode=

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:61]:
paraState=

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:69]:
redirect_uri=http://hd.webonn.com, authorize_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9xxxxxxxxxxxxxxd&redirect_uri=http%3A//hd.webonn.com&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:51]:
requestArgs=ImmutableMultiDict([(‘state’, u”), (‘code’, u’021m0qrQ1qRBpW057XrQ1gaqrQ1m0qrr’)])

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:56]:
paraCode=021m0qrQ1qRBpW057XrQ1gaqrQ1m0qrr

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:61]:
paraState=

<div–<——————————————————————————

r.encoding None
r.apparent_encoding ascii
r.headers {‘date’: ‘Sun, 21 Aug 2016 09:59:54 GMT’, ‘connection’: ‘keep-alive’, ‘content-type’: ‘application/json; encoding=utf-8’, ‘content-length’: ‘175’}
r.url https://api.weixin.qq.com/cgi-bin/token?secret=8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd&grant_type=client_credential&appid=wx9xxxxxxxxxxxxxxd
r.text {"access_token":"wWmsVoT2ql2PQbR0ATnr_tC9mWVPdzKWxraSTucohvqkIbHpj1p2PnMonxKtOv3WBD4K5tQmLC8vsM6dyyua80D9CkPwKRcLsll9K-aKv9MBb3lQIQUcLpdNYRTPioXzLGKhAJASNR","expires_in":7200}
type(r)= <class ‘requests.models.Response’>
r= <Response [200]>
r.encoding ISO-8859-1
r.apparent_encoding ascii
r.headers {‘date’: ‘Sun, 21 Aug 2016 09:59:54 GMT’, ‘connection’: ‘keep-alive’, ‘content-type’: ‘text/plain’, ‘content-length’: ‘336’}
r.url https://api.weixin.qq.com/sns/oauth2/access_token?access_token=wWmsVoT2ql2PQbR0ATnr_tC9mWVPdzKWxraSTucohvqkIbHpj1p2PnMonxKtOv3WBD4K5tQmLC8vsM6dyyua80D9CkPwKRcLsll9K-aKv9MBb3lQIQUcLpdNYRTPioXzLGKhAJASNR&appid=wx9xxxxxxxxxxxxxxd&secret=8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd&grant_type=authorization_code&code=021m0qrQ1qRBpW057XrQ1gaqrQ1m0qrr
r.text {"access_token":"UHdVobKyoSME4ANVjvNkr_JQwLX3hiTFSde-DvWKIWm4dhXvHfcYFrLz8bkfVmd2DwZxIbNp4FiQLP3d9MbpU3ka_6wrGy9cCD3ziBcBrjc","expires_in":7200,"refresh_token":"tS039LO8pMcodPBD9DDyeGcgSrcfFOksRLB5m0aUTUyjG0K6d9Z-XxA_xP1FhDRlbDPkafcOf6vICGrLpNPJhg9Yda7i-e5iIIs0tLKN1UU","openid":"oswxxxxxxxxxxxxxxxxxxxxxxVVY","scope":"snsapi_userinfo"}
type(r)= <class ‘requests.models.Response’>
r= <Response [200]>

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:74]:
respAccessToken={u’access_token’: u’UHdVobKyoSME4ANVjvNkr_JQwLX3hiTFSde-DvWKIWm4dhXvHfcYFrLz8bkfVmd2DwZxIbNp4FiQLP3d9MbpU3ka_6wrGy9cCD3ziBcBrjc’, u’openid’: u’oswxxxxxxxxxxxxxxxxxxxxxxVVY’, u’expires_in’: 7200, u’refresh_token’: u’tS039LO8pMcodPBD9DDyeGcgSrcfFOksRLB5m0aUTUyjG0K6d9Z-XxA_xP1FhDRlbDPkafcOf6vICGrLpNPJhg9Yda7i-e5iIIs0tLKN1UU’, u’scope’: u’snsapi_userinfo’}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:77]:
respAccessTokenStr={
  "access_token": "UHdVobKyoSME4ANVjvNkr_JQwLX3hiTFSde-DvWKIWm4dhXvHfcYFrLz8bkfVmd2DwZxIbNp4FiQLP3d9MbpU3ka_6wrGy9cCD3ziBcBrjc", 
  "openid": "oswxxxxxxxxxxxxxxxxxxxxxxVVY", 
  "expires_in": 7200, 
  "refresh_token": "tS039LO8pMcodPBD9DDyeGcgSrcfFOksRLB5m0aUTUyjG0K6d9Z-XxA_xP1FhDRlbDPkafcOf6vICGrLpNPJhg9Yda7i-e5iIIs0tLKN1UU", 
  "scope": "snsapi_userinfo"
}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:88]:
oauth2Access_token=UHdVobKyoSME4ANVjvNkr_JQwLX3hiTFSde-DvWKIWm4dhXvHfcYFrLz8bkfVmd2DwZxIbNp4FiQLP3d9MbpU3ka_6wrGy9cCD3ziBcBrjc, oauth2Openid=oswxxxxxxxxxxxxxxxxxxxxxxVVY

<div–<——————————————————————————

r.encoding ISO-8859-1
r.apparent_encoding utf-8
r.headers {‘date’: ‘Sun, 21 Aug 2016 09:59:55 GMT’, ‘connection’: ‘keep-alive’, ‘content-type’: ‘text/plain’, ‘content-length’: ‘298’}
r.url https://api.weixin.qq.com/sns/userinfo?access_token=UHdVobKyoSME4ANVjvNkr_JQwLX3hiTFSde-DvWKIWm4dhXvHfcYFrLz8bkfVmd2DwZxIbNp4FiQLP3d9MbpU3ka_6wrGy9cCD3ziBcBrjc&openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY&lang=zh_CN
r.text {"openid":"oswjmv4X0cCXcfkIwjoDfCkeTVVY","nickname":"礼è²","sex":1,"language":"zh_CN","city":"èå·","province":"æ±è","country":"中å½","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ\/0","privilege":[]}
type(r)= <class ‘requests.models.Response’>
r= <Response [200]>

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:90]:
type(respUserInfoDict)=<type ‘dict’>, respUserInfoDict={u’province’: u’\u6c5f\u82cf’, u’openid’: u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’, u’headimgurl’: u’http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0′, u’language’: u’zh_CN’, u’city’: u’\u82cf\u5dde’, u’country’: u’\u4e2d\u56fd’, u’sex’: 1, u’privilege’: [], u’nickname’: u’\u793c\u8c8c’}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:101]:
province=江苏, city=苏州, country=中国, nickname=礼貌

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:102]:
type(province)=<type ‘unicode’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in sipevents [/usr/share/nginx/html/SIPEvents/sipevents.py:114]:
type(respUserInfoStr)=<type ‘unicode’>, respUserInfoStr={
  "province": "江苏", 
  "openid": "oswjmv4X0cCXcfkIwjoDfCkeTVVY", 
  "headimgurl": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0", 
  "language": "zh_CN", 
  "city": "苏州", 
  "country": "中国", 
  "sex": 1, 
  "privilege": [], 
  "nickname": "礼貌"
}

<div–<——————————————————————————

微信显示如图:

转载请注明:在路上 » [已解决]微信授权登陆后获取用户信息后用Python解码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.329 seconds, using 22.14MB memory