def _get_access_token(self, code=None): ''' private function, get access_token :return: access_token, refresh_token ''' if not self.oauth_service: raise NoOAuthServiceErr("no found oauth service") if code: headers = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" } params = { "client_id": self.oauth_service.client_id, "client_secret": self.oauth_service.client_secret, "code": code, "redirect_uri": self.oauth_service.redirect_uri + '?service_id=' + str(self.oauth_service.ID), "grant_type": "authorization_code" } url = self.get_access_token_url(self.oauth_service.home_url) try: rst = self._session.request(method='POST', url=url, headers=headers, params=params) except Exception: raise NoAccessKeyErr("can not get access key") if rst.status_code == 200: data = rst.json() self.access_token = data.get("access_token") self.refresh_token = data.get("refresh_token") if self.access_token is None: return None, None self.set_api("https://oauth.aliyun.com", self.access_token) self.update_access_token(self.access_token, self.refresh_token) return self.access_token, self.refresh_token else: raise NoAccessKeyErr("can not get access key") else: if self.oauth_user: self.set_api(self.oauth_service.home_url, self.oauth_user.access_token) try: user = self.api.get_user() if user["login"]: return self.oauth_user.access_token, self.oauth_user.refresh_token except Exception: if self.oauth_user.refresh_token: try: self.refresh_access_token() return self.access_token, self.refresh_token except Exception: self.oauth_user.delete() raise NoAccessKeyErr("access key is expired, please reauthorize") else: self.oauth_user.delete() raise NoAccessKeyErr("access key is expired, please reauthorize") raise NoAccessKeyErr("can not get access key")
def _get_access_token(self, code=None): ''' private function, get access_token :return: access_token, refresh_token ''' if not self.oauth_service: raise NoOAuthServiceErr("no found oauth service") if code: headers = {"Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded", "Connection": "close"} params = { "client_id": self.oauth_service.client_id, "client_secret": self.oauth_service.client_secret, "code": code, "redirect_uri": self.oauth_service.redirect_uri + '?service_id=' + str(self.oauth_service.ID), "grant_type": "authorization_code" } url = self.get_access_token_url(self.oauth_service.home_url) try: rst = self._session.request(method='POST', url=url, headers=headers, params=params) except Exception: raise NoAccessKeyErr("can not get access key") if rst.status_code == 200: data = rst.json() self.access_token = data.get("access_token") self.refresh_token = data.get("refresh_token") if self.access_token is None: return None, None self.set_api(self.access_token) self.update_access_token(self.access_token, self.refresh_token) return self.access_token, self.refresh_token else: raise NoAccessKeyErr("can not get access key") else: if self.oauth_user: try: self.set_api(self.oauth_user.access_token) user = self.api.get_user() if user.login: return self.oauth_user.access_token, self.oauth_user.refresh_token except Exception as e: logger.debug(e) if self.oauth_user.refresh_token: try: self.refresh_access_token() except Exception: self.oauth_user.delete() raise NoAccessKeyErr("refresh key is expired, please reauthorize") elif isinstance(e, (SSLError, MaxRetryError, ReadTimeoutError)): raise ServiceHandleException(msg=e.message, msg_show="连接github不稳定, 请刷新后重试") else: self.oauth_user.delete() raise NoAccessKeyErr("access key is expired, please reauthorize") raise NoAccessKeyErr("can not get access key")
def _get_user_info(self): ''' private function, get access_token :return: access_token, refresh_token ''' if not self.oauth_service: raise NoOAuthServiceErr("no found oauth service") try: user = self.api._api_get(self.get_user_url("")) except Exception as e: logger.exception(e) raise NoAccessKeyErr("can not get user info") if user: return user else: raise NoAccessKeyErr("can not get user info")
def _get_access_token(self, code=None): ''' private function, get access_token :return: access_token, refresh_token ''' if not self.oauth_service: raise NoOAuthServiceErr("no found oauth service") if code: headers = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded", "Connection": "close" } params = { "client_id": self.oauth_service.client_id, "client_secret": self.oauth_service.client_secret, "code": code, "redirect_uri": self.oauth_service.redirect_uri + '?service_id=' + str(self.oauth_service.ID), "grant_type": "authorization_code" } url = self.get_access_token_url(self.oauth_service.home_url) try: rst = self._session.request(method='POST', url=url, headers=headers, data=params) except Exception as e: logger.exception(e) raise NoAccessKeyErr("can not get access key") if rst.status_code == 200: data = rst.json() self.access_token = data.get("access_token") self.refresh_token = data.get("refresh_token") if self.access_token is None: return None, None self.set_api(self.oauth_service.home_url, self.access_token) self.update_access_token(self.access_token, self.refresh_token) return self.access_token, self.refresh_token else: raise NoAccessKeyErr("can not get access key")
def _get_user_info(self, code=None): ''' private function, get access_token :return: access_token, refresh_token ''' if not self.oauth_service: raise NoOAuthServiceErr("no found oauth service") if code: headers = {"Content-Type": "application/json"} timestamp = str(int(round(time.time()))) + '000' signature = self._compute_signature( self.oauth_service.client_secret, timestamp) query = { "timestamp": timestamp, "accessKey": self.oauth_service.client_id, "signature": signature, } query_str = urllib.urlencode(query) params = { "tmp_auth_code": code, } url = self.get_user_url( self.oauth_service.home_url) + "?" + query_str try: rst = self._session.request(method='POST', url=url, headers=headers, json=params) except Exception: raise NoAccessKeyErr("can not get access key") if rst.status_code == 200: data = rst.json() errcode = data["errcode"] errmsg = data["errmsg"] if errcode == 0: return data["user_info"] else: raise NoAccessKeyErr(errmsg) else: raise NoAccessKeyErr("can not get user info") else: raise NoAccessKeyErr("no code")