微信开发之获取用户基本信息及openid 、access_token、code
凌雪 2018-12-17 来源 :网络 阅读 3712 评论 0

摘要:本文将带你了解微信开发——获取用户基本信息及openid 、access_token、code,希望本文对大家学微信有所帮助。

本文将带你了解微信开发——获取用户基本信息及openid 、access_token、code,希望本文对大家学微信有所帮助。



获取用户信息,需要获取   access_token、openid

然后调用接口https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

access_token:公众号的全局唯一票据,

获取access_token,需要调用https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

参数

是否必须

说明

grant_type    是    获取access_token填写client_credential    

appid    是    第三方用户唯一凭证    

secret    是    第三方用户唯一凭证密钥,即appsecret    

openid:普通用户的标识,对当前公众号唯一

获取openid需要先获取code,

获取code需要调用接口

https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=现在访问的方法的url&response_type=code&scope=snsapi_userinfo&state=STATE

获取code后,

再调用接口https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=code&grant_type=authorization_code以获取openid

 具体代码

def get_user_id(self,code):    """    获取code      openid    :return:    """    #getwx() 方法获取appid    wxinfo = self.getwx()    #获取正在访问的模块的路径(手机上的)      urls="{}://{}{}".format(self.request.protocol,self.request.host,self.request.uri)    #获取code      if code == 'mistake':          codeurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+wxinfo['appid']+'&redirect_uri={}&response_type=code&scope=snsapi_userinfo&state=STATE'.format(urls)        self.redirect(codeurl)    else:        #如果存在code 则调用weixin()函数,获取openid        openid=self.weixin(code)        return openiddef getwx(self):    """    #获取appid和secret    :return:    """    db_wx = self.db.query(Wx)    db_wx = db_wx.first()    return   {'appid':db_wx.appid,'secret':db_wx.secret}def weixin(self,code):    """    获取openid :普通用户的标识,对当前公众号唯一    :return:    """    #getwx() 方法获取appid和secret    wxinfo = self.getwx()      opidurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+wxinfo['appid']+"&secret="+wxinfo['secret']+"&code="+code+"&grant_type=authorization_code"    #先使用opreq =   urllib2.Request(opidurl)实例化一个resquest对象,      opreq = urllib2.Request(opidurl)      #接下来使用urllib2.urlopen(opreq)来打开这个网页      opreq_data = urllib2.urlopen(opreq)      #read()   不带参数的read是将文件所有内容读入到   一个字符串中    opres =   opreq_data.read()      opres=json_decode(opres)      openid= opres['openid']      return openiddef accesstokens(self):      """    获取 token    token:access_token是公众号的全局唯一票据    :return:    """    token = self.redis.get("wx_token")    if not token:        #getwx() 方法获取appid和secret        wxinfo = self.getwx()        url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+wxinfo['appid']+"&secret="+wxinfo['secret']+""        req = urllib2.Request(url)        res_data = urllib2.urlopen(req)        res = res_data.read()        res=json_decode(res)        token=str(res['access_token'])          self.redis.set("wx_token",token,ex=3600)    return token

    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!


本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 1
看完这篇文章有何感觉?已经有1人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved