Example #1
0
    def send_request(self, url, data, timeout):

        # dirty realization
        yield from asyncio.sleep(self.pause_between_requests)
        response = yield from asyncio.wait_for(
            self.requests_session.post(url, data=data),
            timeout=timeout,
        )
        return RequestsLikeResponse(response)
Example #2
0
    def oauth2_authorization(self):
        """
        OAuth2
        """
        auth_data = {
            'client_id': self.app_id,
            'display': 'mobile',
            'response_type': 'token',
            'scope': self.scope,
            'v': '5.37',
            # "redirect_uri": "https://oauth.vk.com/blank.html",
        }
        response = yield from self.auth_session.post(self.AUTHORIZE_URL, data=auth_data)
        response = RequestsLikeResponse(response)
        response_url_query = get_url_query(response.url)
        if 'access_token' in response_url_query:
            return response_url_query

        # Permissions is needed
        logger.info('Getting permissions')
        # form_action = re.findall(r'<form method="post" action="(.+?)">', auth_response.text)[0]
        form_action = get_form_action((yield from response.text()))
        logger.info('Response form action: %s', form_action)
        if form_action:
            response = yield from self.auth_session.get(form_action)
            response = RequestsLikeResponse(response)
            response_url_query = get_url_query(response.url)
            return response_url_query

        try:
            response_json = yield from response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
        else:
            error_message = 'VK error: [{}] {}'.format(response_json['error'], response_json['error_description'])
        logger.error('Permissions obtained')
        raise VkAuthError(error_message)
Example #3
0
    def login(self):
        """
        Login
        """

        response = yield from self.auth_session.get(self.LOGIN_URL)
        response = RequestsLikeResponse(response)
        login_form_action = get_form_action((yield from response.text()))
        if not login_form_action:
            raise VkAuthError('VK changed login flow')

        login_form_data = {
            'email': self.user_login,
            'pass': self.user_password,
        }
        response = yield from self.auth_session.post(login_form_action,
                                                     data=login_form_data)
        response = RequestsLikeResponse(response)
        yield from response.text()
        # logger.info('Cookies: %s', self.auth_session.cookies)

        response_url_query = get_url_query(response.url)

        if 'remixsid' in self.auth_session.cookies or 'remixsid6' in self.auth_session.cookies:
            # if 'remixsid' in response_url_query or 'remixsid6' in response_url_query:
            return

        if 'sid' in response_url_query:
            yield from self.auth_captcha_is_needed(response, login_form_data)
        elif response_url_query.get('act') == 'authcheck':
            self.auth_check_is_needed((yield from response.text()))
        elif 'security_check' in response_url_query:
            self.phone_number_is_needed((yield from response.text()))
        else:
            message = 'Authorization error (incorrect password)'
            logger.error(message)
            raise VkAuthError(message)
Example #4
0
    def oauth2_authorization(self):
        """
        OAuth2
        """
        auth_data = {
            'client_id': self.app_id,
            'display': 'mobile',
            'response_type': 'token',
            'scope': self.scope,
            'v': '5.37',
            # "redirect_uri": "https://oauth.vk.com/blank.html",
        }
        response = yield from self.auth_session.post(self.AUTHORIZE_URL,
                                                     data=auth_data)
        response = RequestsLikeResponse(response)
        response_url_query = get_url_query(response.url)
        if 'access_token' in response_url_query:
            return response_url_query

        # Permissions is needed
        logger.info('Getting permissions')
        # form_action = re.findall(r'<form method="post" action="(.+?)">', auth_response.text)[0]
        form_action = get_form_action((yield from response.text()))
        logger.info('Response form action: %s', form_action)
        if form_action:
            response = yield from self.auth_session.get(form_action)
            response = RequestsLikeResponse(response)
            response_url_query = get_url_query(response.url)
            return response_url_query

        try:
            response_json = yield from response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
        else:
            error_message = 'VK error: [{}] {}'.format(
                response_json['error'], response_json['error_description'])
        logger.error('Permissions obtained')
        raise VkAuthError(error_message)
Example #5
0
    def login(self):
        """
        Login
        """

        response = yield from self.auth_session.get(self.LOGIN_URL)
        response = RequestsLikeResponse(response)
        login_form_action = get_form_action((yield from response.text()))
        if not login_form_action:
            raise VkAuthError('VK changed login flow')

        login_form_data = {
            'email': self.user_login,
            'pass': self.user_password,
        }
        response = yield from self.auth_session.post(login_form_action, data=login_form_data)
        response = RequestsLikeResponse(response)
        yield from response.text()
        # logger.info('Cookies: %s', self.auth_session.cookies)

        response_url_query = get_url_query(response.url)

        if 'remixsid' in self.auth_session.cookies or 'remixsid6' in self.auth_session.cookies:
        # if 'remixsid' in response_url_query or 'remixsid6' in response_url_query:
            return

        if 'sid' in response_url_query:
            yield from self.auth_captcha_is_needed(response, login_form_data)
        elif response_url_query.get('act') == 'authcheck':
            self.auth_check_is_needed((yield from response.text()))
        elif 'security_check' in response_url_query:
            self.phone_number_is_needed((yield from response.text()))
        else:
            message = 'Authorization error (incorrect password)'
            logger.error(message)
            raise VkAuthError(message)