zoukankan      html  css  js  c++  java
  • 微信获取用户的openid和详细信息

    获取用户的信息的原理,首先用户会点击一个url,这个url会包含一个参数redirect_uri,这个url是指向微信那边的服务器的,然后微信会把这个http请求重定向到redirect_uri,即我们的后端,而且会附带一个code参数,如果我们需要获取用户的基础信息(也就是openid)就需要用这个code去访问微信的指定url来请求用户的openid,如果我们需要获取用户的详细信息(微信名称,头像),我们就需要先用code获取一个access_token,再用这个access_token来获取用户的信息

    获取用户点击的那个url的方法:

    #encoding=utf-8
    __author__ = 'kevinlu1010@qq.com'
    
    
    def get_redirect_url(uri, is_info=0):
        '''
        获取url,改url可以访问微信的网址,然后会重定向回来我们的网址,而且附带访问的微信用户的信息
        uri 需要跳转到的uri  如/cherrs
        is_info 是否获取详细信息,如果为1,就获取用户的详细信息,包括名字,图片,否则就获取基本信息,只有open_id
        返回 url
        '''
        pre_url = 'http://xxxx.xxxxx.com'
        appid = 'wxxxxxxxxxxxxxxxxxeb'
        scope = 'snsapi_userinfo' if is_info else 'snsapi_base'
        data = {'redirect_uri': pre_url + uri,
                'appid': appid,
                'response_type': 'code',
                'scope': scope,
                'state': '123'}
    
        urlencode = urllib.urlencode(data)
        wei_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' + urlencode + '#wechat_redirect'
    
        return wei_url

    通过code获取用户open_id

    def get_base_info(code):
        '''
        通过获取的code,访问微信的api,获取用户基本信息(只有openid)
        return 用户的open id
        '''
        if not code: return ''
        url = 'https://api.weixin.qq.com/sns/oauth2/access_token'
        try:
            data = {'appid': const.appid, 'secret': const.secret, 'code': code, 'grant_type': 'authorization_code'}
            post_body = urllib.urlencode(data)
            f = urllib2.urlopen(url, post_body).read()
            r = ast.literal_eval(f) #字符串转换成字典
            return r['openid']
        except:
            return ''

    通过code获取用户详细信息:

    def get_detail_info(code):
        '''
        获取用户的详细信息
        return dict {'userID':'','nickname':''m',headimgurl':''}
        '''
        ret = {}
        url = 'https://api.weixin.qq.com/sns/oauth2/access_token'
        try:
            data = {'appid': const.appid, 'secret': const.secret, 'code': code, 'grant_type': 'authorization_code'}
            post_body = urllib.urlencode(data)
            f = urllib2.urlopen(url, post_body).read()
            r = ast.literal_eval(f) #字符串转换成字典
    
            #获取用户详细信息
            access_token = r['access_token']
            ret['userID'] = r['openid']
            url = 'https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN' % (
                access_token, ret['userID'])
            f = urllib2.urlopen(url).read()
            r = ast.literal_eval(f) #字符串转换成字典
            ret['nickname'] = urllib.unquote(r['nickname'])
            ret['headimgurl'] = urllib.unquote(r['headimgurl'])
        except urllib2.URLError, e:
            return ret
        return ret
  • 相关阅读:
    ORA12560: TNS: 协议适配器错误的问题
    ibatis代码生成工具abator使用全过程
    DbHelper数据操作类
    眼睛有干涩、血丝、怕光,流泪,甚至红肿的现象吗
    Dot.Net代码生成器
    两分钟让你明白什么是ERP
    spring的b/s项目中配置log4j
    十面埋妇
    程序员发展的目标
    标准体重计算查询
  • 原文地址:https://www.cnblogs.com/Xjng/p/3910511.html
Copyright © 2011-2022 走看看