def get_user_info_by_token(self, openid='', access_token=''): if not (access_token and self.appid and self.secret): logger.warn('args error, access_token:%s, appid:%s, secret:%s' % (access_token, self.appid, self.secret)) return None, None, None args = (Domain, urllib.quote(openid), urllib.quote(access_token), urllib.quote(self.appid), urllib.quote(self.secret)) get_info_url = "http://%s/qq/token/valid?openid=%s&token=%s&qqappid=%s&qqappsecret=%s" % args tmp = open_url(get_info_url) logger.info('get_user_info_by_token info:%s' % tmp) if tmp and isinstance(tmp, dict) and tmp.get('code') == 1: obj = tmp.get("data") openid = tmp.get("openid") token = tmp.get("token") if not (obj and token and openid): logger.warn('get_user_info_by_token fail, access_token:%s' % access_token) return None, None, None session = get_session(openid=openid, token=token) return token, session, obj else: logger.warn('get_user_info_by_token fail, access_token:%s' % access_token) return None, None, None
def get_user_info_by_code(self, code): if not (code and self.appid and self.secret and self.redirect_uri): logger.warn( 'args error, code:%s, appid:%s, secret:%s, redirect_uri:%s' % (code, self.appid, self.secret, self.redirect_uri)) return None, None, None args = (Domain, urllib.quote(code), urllib.quote(self.redirect_uri), urllib.quote(self.appid), urllib.quote(self.secret)) get_info_url = "http://%s/qq/code/get?code=%s&ruri=%s&qqappid=%s&qqappsecret=%s" % args tmp = open_url(get_info_url) logger.info('get_user_info_by_code info:%s' % tmp) if tmp and isinstance(tmp, dict) and tmp.get('code') == 1: obj = tmp.get("data") token = tmp.get("token") openid = tmp.get("openid") expires = tmp.get("expires") if not (obj and token and expires): logger.warn('get user info by code fail, code:%s' % code) return None, None, None return token, obj, expires else: logger.warn('get user info by code fail, code:%s' % code) return None, None, None
def _get_token(self): get_atk_url = "https://api.weixin.qq.com/cgi-bin/token?appid=%s&secret=%s&grant_type=client_credential" % ( self.appid, self.secret) tmp = open_url(get_atk_url) if tmp and isinstance(tmp, dict) and tmp.has_key("access_token"): logger.info('get access_token success,appid:%s,secret:%s' % (self.appid, self.secret)) return tmp["access_token"] else: logger.warn('[WARN]no access_token, appid:%s,secret:%s' % (self.appid, self.secret)) return ''
def _get_sdk_ticket(self): access_token = self._get_token() if access_token: get_ticket_url = "http://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=2" % access_token tmp = open_url(get_ticket_url) if tmp and isinstance(tmp, dict) and tmp.has_key('ticket'): return tmp['ticket'] else: logger.warn('[WARN]no ticket, access_token:%s' % access_token) else: logger.warn('[WARN]get access_token fail') return ''
def get_signature(self): get_signature_url = "http://%s/weixin/signature/get" % Domain tmp = open_url(get_signature_url) logger.info('get_signature info:%s' % tmp) if tmp and isinstance(tmp, dict) and tmp.get('code') == 1: signature = tmp.get("signature") ts = tmp.get("ts") noncestr = tmp.get("noncestr") return (signature, ts, noncestr) else: logger.warn('get signature fail') return None
def _get_weixin_userinfo(self, access_token, openid): get_userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s" % ( access_token, openid) tmp = open_url(get_userinfo_url) if tmp and isinstance(tmp, dict) and tmp.get("nickname") and tmp.get( 'sex') and tmp.get('headimgurl'): obj = {} obj['nickname'] = tmp.get("nickname") obj['sex'] = tmp.get('sex') obj['sex'] = '男' if obj['sex'] == 1 else '女' obj['usericon'] = tmp.get('headimgurl') obj['openid'] = openid logger.info('get userinfo success,info:%s' % obj) return obj else: logger.warn('access_token:%s expired.' % access_token) return None
def _refresh_token(self, refresh_token): get_token_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s" % ( self.appid, refresh_token) tmp = open_url(get_token_url) if tmp and isinstance(tmp, dict) and tmp.has_key( "access_token") and tmp.has_key("openid"): token = tmp.get("access_token") openid = tmp.get("openid") logger.info( 'get token success,token:%s,refresh_token:%s,appid:%s' % (token, refresh_token, self.appid)) return (token, openid) else: logger.warn( '[WARN]refresh token failed,appid:%s,refresh_token:%s' % (self.appid, refresh_token)) logger.warn('tmp:%s' % tmp) return None
def _get_token_by_code(self, code): get_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code" % ( self.appid, self.secret, code) tmp = open_url(get_token_url) if tmp and isinstance( tmp, dict) and tmp.has_key("access_token") and tmp.has_key( "openid") and tmp.has_key("refresh_token"): token = tmp["access_token"] openid = tmp["openid"] refresh_token = tmp["refresh_token"] expire = tmp["expires_in"] logger.info('get token success,appid:%s,secret:%s' % (self.appid, self.secret)) return (token, refresh_token, openid, expire) else: logger.warn('[WARN]get token failed,appid:%s,secret:%s,code:%s' % (self.appid, self.secret, code)) return None
def _get_qq_userinfo(self, access_token, openid): get_userinfo_url = "https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s" % ( access_token, self.appid, openid) logger.info('start get_qq_userinfo, url:%s' % get_userinfo_url) tmp = open_url(get_userinfo_url) logger.info('get_qq_info:%s' % tmp) if tmp and isinstance(tmp, dict) and tmp.get("nickname") and tmp.get( 'gender') and tmp.get('figureurl_qq_1'): obj = {} obj['nickname'] = tmp.get("nickname") obj['sex'] = tmp.get('gender') obj['usericon'] = tmp.get('figureurl_qq_2') or tmp.get( 'figureurl_qq_1') obj['openid'] = openid logger.info( 'get userinfo success,nickname:%s,sex:%s,headimgurl:%s,openid:%s' % (obj['nickname'], obj['sex'], obj['usericon'], openid)) return obj else: logger.warn('access_token:%s expired.' % access_token) return None