def get_code(self, username, password, allow_redirects=True): data = { "client_id": self.app.key, "redirect_uri": self.app.redirect_uri, "userId": username, "passwd": password, "isLoginSina": "0", "action": "submit", "response_type": "code", } headers = { "User-Agent": "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36", "Host": "api.weibo.com", "Referer": self.authorize() } r = self._session.post(self.AUTH_URL, data=data, headers=headers, allow_redirects=allow_redirects) if allow_redirects: code_url = r.url else: code_url = r.headers['location'] query = parse_querystring(code_url) return query['code']
def login(self, username, password): su = base64.b64encode(urllib.quote(username)) pre_data = self.pre_login(su) sp = self.encrypt_password(password, pre_data) data = { 'client': self.JS_CLIENT, 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '0', 'userticket': '1', 'ssosimplelogin': '******', 'vsnf': '1', 'vsnval': '', 'su': su, 'service': 'miniblog', 'servertime': pre_data.servertime, 'nonce': pre_data.nonce, 'pwencode': 'rsa2', 'prelt': '164', 'sp': sp, 'encoding': 'UTF-8', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META', 'rsakv': pre_data.rsakv, } response = self._session.post(self.LOGIN_URL, data=data) url = self.URL_REGEX.findall(response.text)[0] query = parse_querystring(url) if query['retcode'] != '0': raise ApiResponseError(response, query['retcode'], query['reason']) return self._session.get(url)
def get_code(self, username, password, allow_redirects=True): data = {"client_id": self.app.key, "redirect_uri": self.app.redirect_uri, "userId": username, "passwd": password, "isLoginSina": "0", "action": "submit", "response_type": "code", } headers = { "User-Agent": "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36", "Host": "api.weibo.com", "Referer": self.authorize() } r = self._session.post(self.AUTH_URL, data=data, headers=headers, allow_redirects=allow_redirects) if allow_redirects: code_url = r.url else: code_url = r.headers['location'] query = parse_querystring(code_url) return query['code']
def test_parse_querystring(self): r = parse_querystring("http://test.com?foo=bar&n=1") self.assertEqual(2, len(r)) self.assertEqual(r['foo'], 'bar') self.assertEqual( r['n'], '1')
def test_parse_querystring(self): r = parse_querystring("a=a&b=b") self.assertEqual(2, len(r)) self.assertEqual('a', r['a']) self.assertEqual('b', r['b'])
def _parse_token(self, response): data = parse_querystring(response.text) if 'errorCode' in data: raise ApiResponseError(response, data['errorCode'], data.get('errorMsg', '').strip("'")) return Token(**data)
def decode_parameters(top_parameters): """ 将top_parameters字符串解码并转换为字典,(已测试成功,不要更改) """ parameters = base64.decodestring(unquote(top_parameters)) return parse_querystring(parameters)
def test_parse_querystring(self): r = parse_querystring("http://test.com?foo=bar&n=1") self.assertEqual(2, len(r)) self.assertEqual(r['foo'], 'bar') self.assertEqual(r['n'], '1')