def __step1_check_app_valid_and_get_oauthid(self): c, r = http._get( self.auth_package.OAUTH + "auth/", { "client_id": self.auth_package.client_id, "redirect_uri": self.auth_package.redirect_uri, "scope": "credentials.r,account.r", "response_type": "code", "state": "", }, ) if r.status != 200: raise Exception("Incorrect/unauthorized " "HubiC client_id (%s)" % str(http._parse_error(r))) rdata = r.read() c.close() try: from lxml import html as lxml_html except ImportError: lxml_html = None if lxml_html: oauth = lxml_html.document_fromstring(rdata).xpath('//input[@name="oauth"]') oauth = oauth[0].value if oauth else None else: oauth = re.search(r'<input\s+[^>]*name=[\'"]?oauth[\'"]?\s+[^>]*value=[\'"]?(\d+)[\'"]?>', rdata) oauth = oauth.group(1) if oauth else None if not oauth: raise Exception("Unable to get oauth_id from authorization page") return oauth
def _requestSwiftToken(self): """ Request a swift token by creating an accesstoken """ oauth_access_token = self.accessTokenManager.token c, r = http._get( self.auth_package.HUBIC_API + "account/credentials/", headers={"Authorization": "Bearer " + oauth_access_token}, ) result = json.loads(r.read()) c.close() if r.status != 200: try: err = result err["code"] = r.status except Exception as e: err = {} raise Exception("Unable to get swift token, " "(%s)" % str(err)) self._endpoint = result["endpoint"] self._token = result["token"] self._expire = datetime.strptime(result["expires"][:-6], "%Y-%m-%dT%H:%M:%S") - timedelta(seconds=10)