def parse_token_response(self, res): if 'callback(' in res: res = res[res.index('(') + 1:res.rindex(')')] res = json.loads(res) raise SocialAPIError( self.site_name, '', u'%s:%s' % (res['error'], res['error_description'])) else: res = res.split('&') res = [_r.split('=') for _r in res] res = dict(res) self.access_token = res['access_token'] self.expires_in = int(res['expires_in']) self.refresh_token = None res = self.http_get(self.OPENID_URL, {'access_token': self.access_token}, parse=False) res = json.loads(QQ_OPENID_PATTERN.search(res).group()) self.uid = res['openid'] _url = 'https://graph.qq.com/user/get_user_info' res = self.api_call_get(_url) if res['ret'] != 0: raise SocialAPIError(self.site_name, _url, res) self.name = res['nickname'] self.avatar = res['figureurl_qq_1'] self.avatar_large = res['figureurl_qq_2'] self.gender = res['gender'] == u"男" and "M" or "F"
def deco(self, *args, **kwargs): try: res = func(self, *args, **kwargs) except HTTPError as e: raise SocialAPIError(self.site_name, e.url, e.read()) except URLError as e: raise SocialAPIError(self.site_name, args[0], e.reason) error_key = getattr(self, 'RESPONSE_ERROR_KEY', None) if error_key is not None and error_key in res: raise SocialAPIError(self.site_name, args[0], res) return res
def get_access_token(self, code, method='POST', parse=True): """parse is True means that the api return a json string. So, the result will be parsed by json library. Most sites will follow this rule, return a json string. But some sites (e.g. Tencent), Will return an non json string, This sites MUST set parse=False when call this method, And handle the result by themselves. This method Maybe raise SocialAPIError. Application MUST try this Exception. """ data = { 'client_id': self.CLIENT_ID, 'client_secret': self.CLIENT_SECRET, 'redirect_uri': self.REDIRECT_URI, 'code': code, 'grant_type': 'authorization_code' } if method == 'POST': res = self.http_post(self.ACCESS_TOKEN_URL, data, parse=parse) else: res = self.http_get(self.ACCESS_TOKEN_URL, data, parse=parse) try: self.parse_token_response(res) except: raise SocialAPIError(self.site_name, self.ACCESS_TOKEN_URL, res)
def parse_token_response(self, res): self.uid = res['userid'] self.access_token = res['access_token'] self.expires_in = 0 self.refresh_token = None _url = 'https://graph.qq.com/user/get_user_info' res = self.api_call_get(_url) if res['ret'] != 0: raise SocialAPIError(self.site_name, _url, res) self.name = res['nickname'] self.avatar = res['figureurl_qq_1'] self.avatar_large = res['figureurl_qq_2'] self.gender = res['gender'] == u"男" and "M" or "F"
def parse_token_response(self, res): res = json.loads(res) self.access_token = res['access_token'] self.expires_in = int(res['expires_in']) self.refresh_token = res['refresh_token'] self.uid = res['openid'] if self.SCOPE == 'snsapi_userinfo': res = self.api_call_get(self.OPENID_URL, lang='zh_CN') if 'errcode' in res: raise SocialAPIError(self.site_name, self.OPENID_URL, res) self.name = res['nickname'] self.avatar = res['headimgurl'] self.avatar_large = res['headimgurl'] else: self.name = '' self.avatar = '' self.avatar_large = ''
def parse_token_response(self, res): res = res.split('&') res = [_r.split('=') for _r in res] res = dict(res) self.access_token = res['access_token'] self.expires_in = int(res['expires_in']) self.refresh_token = None res = self.http_get(self.OPENID_URL, {'access_token': self.access_token}, parse=False) res = json.loads(QQ_OPENID_PATTERN.search(res).group()) self.uid = res['openid'] _url = 'https://graph.qq.com/user/get_user_info' res = self.api_call_get(_url) if res['ret'] != 0: raise SocialAPIError(self.site_name, _url, res) self.name = res['nickname'] self.avatar = res['figureurl_qq_1'] self.avatar_large = res['figureurl_qq_2']