def _save_user_and_token(token_dict, user_info, openid_type): ua = UserAlias.get(openid_type, user_info.get_user_id()) if not ua: if not g.user: ua = UserAlias.create_new_user(openid_type, user_info.get_user_id(), user_info.get_nickname()) else: ua = UserAlias.bind_to_exists_user(g.user, openid_type, user_info.get_user_id()) if not ua: return None ##设置个人资料(头像等等) u = User.get(ua.user_id) u.set_avatar_url(user_info.get_avatar()) u.set_icon_url(user_info.get_icon()) ##保存access token if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("access_token_secret", "")) else: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("refresh_token", "")) ##set cookie,保持登录状态 if not g.user: g.user = User.get(ua.user_id) set_user_cookie(g.user, session) return g.user
def check_result(self, uri, resp, content): user_id = self.user_alias and self.user_alias.user_id or None excp = OAuthTokenExpiredError( user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN], content) jdata = json_decode(content) if content else None if str(resp.status) == "200": excp.clear_the_profile() return jdata log.warning("get %s fail, status code=%s, msg=%s. go to refresh token" \ % (uri, resp.status, content)) if jdata and isinstance(jdata, dict): error_code = jdata.get("code") if str(error_code) == "103" or str(error_code) == "123": excp.set_the_profile() raise excp elif str(error_code) == "106" and self.user_alias: try: new_tokens = super(Douban, self).refresh_tokens() if new_tokens and isinstance(new_tokens, dict): OAuth2Token.add(self.user_alias.id, new_tokens.get("access_token"), new_tokens.get("refresh_token")) excp.clear_the_profile() except OAuthError, e: log.warn("refresh token fail: %s" % e) excp.set_the_profile() raise e
def check_result(self, uri, resp, content): user_id = self.user_alias and self.user_alias.user_id or None excp = OAuthTokenExpiredError(user_id, config.OPENID_TYPE_DICT[config.OPENID_DOUBAN], content) jdata = json_decode(content) if content else None if str(resp.status) == "200": excp.clear_the_profile() return jdata log.warning("get %s fail, status code=%s, msg=%s. go to refresh token" \ % (uri, resp.status, content)) if jdata and isinstance(jdata, dict): error_code = jdata.get("code") if str(error_code) == "103" or str(error_code) == "123": excp.set_the_profile() raise excp elif str(error_code) == "106" and self.user_alias: try: new_tokens = super(Douban, self).refresh_tokens() if new_tokens and isinstance(new_tokens, dict): OAuth2Token.add(self.user_alias.id, new_tokens.get("access_token"), new_tokens.get("refresh_token")) excp.clear_the_profile() except OAuthError, e: log.warn("refresh token fail: %s" % e) excp.set_the_profile() raise e
def _save_user_and_token(token_dict, thirdparty_user, openid_type): first_connect = False ua = UserAlias.get(openid_type, thirdparty_user.get_user_id()) if not ua: if not g.user: ua = UserAlias.create_new_user(openid_type, thirdparty_user.get_user_id(), thirdparty_user.get_nickname()) else: ua = UserAlias.bind_to_exists_user(g.user, openid_type, thirdparty_user.get_user_id()) first_connect = True if not ua: return None ##设置个人资料(头像等等) u = User.get(ua.user_id) u.set_avatar_url(thirdparty_user.get_avatar()) u.set_icon_url(thirdparty_user.get_icon()) ##把各个第三方的uid保存到profile里面 k = openid_type v = { "uid": thirdparty_user.get_uid(), "name": thirdparty_user.get_nickname(), "intro": thirdparty_user.get_intro(), "signature": thirdparty_user.get_signature(), "avatar": thirdparty_user.get_avatar(), "icon": thirdparty_user.get_icon(), "email": thirdparty_user.get_email(), "first_connect": "Y" if first_connect else "N", } u.set_profile_item(k, json_encode(v)) ##保存access token if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("access_token_secret", "")) else: OAuth2Token.add(ua.id, token_dict.get("access_token"), token_dict.get("refresh_token", "")) ##set cookie,保持登录状态 if not g.user: g.user = User.get(ua.user_id) set_user_cookie(g.user, session) return g.user
def _request(self, api, method="POST", extra_dict=None): if extra_dict is None: extra_dict = {} params = { "method": api, "v": "1.0", "access_token": self.access_token, "format": "json", } params.update(extra_dict) _, qs = Renren.sign(self.apikey_secret, **params) uri = "%s?%s" % (self.api_host, qs) log.info('getting %s...' % uri) resp, content = httplib2_request(uri, method) if resp.status == 200: user_id = self.user_alias and self.user_alias.user_id or None excp = OAuthTokenExpiredError( user_id=None, openid_type=config.OPENID_TYPE_DICT[config.OPENID_RENREN], msg=content) jdata = json_decode(content) if content else None if jdata and isinstance(jdata, dict): error_code = jdata.get("error_code") error_msg = jdata.get("error_msg") if error_code: if str(error_code) == "105": ## 无效的token excp.set_the_profile() raise excp elif str(error_code) == "106" and self.user_alias: ## FIXME: 过期的token, 是不是106? try: new_tokens = super(Renren, self).refresh_tokens() if new_tokens and isinstance(new_tokens, dict): OAuth2Token.add( self.user_alias.id, new_tokens.get("access_token"), new_tokens.get("refresh_token")) excp.clear_the_profile() except OAuthError, e: log.warn("refresh token fail: %s" % e) excp.set_the_profile() raise e return jdata
def _request(self, api, method="POST", extra_dict=None): if extra_dict is None: extra_dict = {} params = { "method": api, "v": "1.0", "access_token": self.access_token, "format": "json", } params.update(extra_dict) _, qs = Renren.sign(self.apikey_secret, **params) uri = "%s?%s" % (self.api_host, qs) log.info('getting %s...' % uri) resp, content = httplib2_request(uri, method) if resp.status == 200: user_id = self.user_alias and self.user_alias.user_id or None excp = OAuthTokenExpiredError(user_id=None, openid_type=config.OPENID_TYPE_DICT[config.OPENID_RENREN], msg=content) jdata = json_decode(content) if content else None if jdata and isinstance(jdata, dict): error_code = jdata.get("error_code") error_msg = jdata.get("error_msg") if error_code: if str(error_code) == "105": ## 无效的token excp.set_the_profile() raise excp elif str(error_code) == "106" and self.user_alias: ## FIXME: 过期的token, 是不是106? try: new_tokens = super(Renren, self).refresh_tokens() if new_tokens and isinstance(new_tokens, dict): OAuth2Token.add(self.user_alias.id, new_tokens.get("access_token"), new_tokens.get("refresh_token")) excp.clear_the_profile() except OAuthError, e: log.warn("refresh token fail: %s" % e) excp.set_the_profile() raise e return jdata
def update_tokens(self, refresh_token): qs = {} qs["client_id"] = self.apikey qs["client_secret"] = self.apikey_secret qs["redirect_uri"] = self.redirect_uri qs["grant_type"] = "refresh_token" qs["refresh_token"] = refresh_token resp, content = httplib2_request(self.access_token_uri, "POST", body=urllib.urlencode(qs)) if resp.status != 200: raise OAuthLoginError('refres_tokens fail, status=%s:reason=%s:content=%s' \ %(resp.status, resp.reason, content)) r = json_decode(content) return OAuth2Token.add(r.get("douban_user_id"), r.get("access_token"), r.get("refresh_token"))