コード例 #1
0
ファイル: test_base.py プロジェクト: alexfromvl/vk-requests
    def test_stringify_string_values(self):
        # Expect the string will be set as is
        data = {'fields': 'owner'}
        self.assertEqual(utils.stringify_values(data), {'fields': 'owner'})

        data_2 = {'fields': ['owner']}
        self.assertEqual(utils.stringify_values(data_2), {'fields': 'owner'})
コード例 #2
0
    def test_stringify_string_values(self):
        # Expect the string will be set as is
        data = {'fields': 'owner'}
        self.assertEqual(utils.stringify_values(data), {'fields': 'owner'})

        data_2 = {'fields': ['owner']}
        self.assertEqual(utils.stringify_values(data_2), {'fields': 'owner'})
コード例 #3
0
    def do_direct_authorization(self, session):
        """ Direct Authorization, more info: https://vk.com/dev/auth_direct """
        logger.info('Doing direct authorization, app_id=%s', self.app_id)
        auth_data = {
            'client_id': self.app_id,
            'client_secret': self._client_secret,
            'username': self._login,
            'password': self._password,
            'grant_type': 'password',
            '2fa_supported': self._two_fa_supported,
            'scope': self.scope,
            'v': self.api_version
        }
        response = session.post(url=self.DIRECT_AUTHORIZE_URL,
                                data=stringify_values(auth_data))
        try:
            response_json = response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
            logger.error(response.text)
            raise VkAuthError(error_message)
        else:
            if 'access_token' in response_json:
                return response_json

            if response_json['error'] == 'need_validation':
                return self.direct_auth_require_2fa(session, auth_data)
            elif response_json['error'] == 'need_captcha':
                return self.direct_auth_require_captcha(
                    session, response_json, auth_data)
            else:
                error_message = 'VK error: [{}] {}'.format(
                    response_json['error'], response_json['error_description'])
                raise VkAuthError(error_message)
コード例 #4
0
    def send_api_request(self, request_obj, captcha_response=None):
        """Prepare and send HTTP API request

        :param request_obj: vk_requests.api.Request instance 
        :param captcha_response: None or dict 
        :return: HTTP response
        """
        url = self.API_URL + request_obj.method_name

        # Prepare request arguments
        method_kwargs = {'v': self.api_version}

        for values in (request_obj.method_args, ):
            method_kwargs.update(stringify_values(values))

        if self.is_token_required() or self._service_token:
            # Auth api call if access_token hadn't been gotten earlier
            method_kwargs['access_token'] = self.access_token

        if captcha_response:
            method_kwargs['captcha_sid'] = captcha_response['sid']
            method_kwargs['captcha_key'] = captcha_response['key']

        http_params = dict(url=url,
                           data=method_kwargs,
                           **request_obj.http_params)
        response = self.http_session.post(**http_params)
        return response
コード例 #5
0
 def direct_auth_require_2fa(self, session, auth_data):
     if self._two_fa_force_sms:
         auth_data['force_sms'] = self._two_fa_force_sms
         session.post(url=self.DIRECT_AUTHORIZE_URL,
                      data=stringify_values(auth_data))
     logger.info(
         'User enabled 2 factors authentication. Auth check code is needed '
         '(SMS, Google Authenticator or one-time password generated by vk)')
     auth_data['code'] = self.get_2fa_code()
     response = session.post(url=self.DIRECT_AUTHORIZE_URL,
                             data=stringify_values(auth_data))
     try:
         response_json = response.json()
     except ValueError:  # not JSON in response
         error_message = 'OAuth2 grant access error'
         logger.error(response.text)
         raise VkAuthError(error_message)
     return response_json
コード例 #6
0
    def direct_auth_require_captcha(self, session, response, auth_data):
        logger.info('Captcha is needed. Response: %s', response)

        captcha_url = response['captcha_img']
        logger.info('Captcha url %s', captcha_url)

        auth_data['captcha_sid'] = response['captcha_sid']
        auth_data['captcha_key'] = self.get_captcha_key(captcha_url)

        response = session.post(url=self.DIRECT_AUTHORIZE_URL,
                                data=stringify_values(auth_data))
        try:
            response_json = response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
            logger.error(response.text)
            raise VkAuthError(error_message)
        return response_json
コード例 #7
0
    def do_implicit_flow_authorization(self, session):
        """ Standard OAuth2 authorization method. It's used for getting access token
        More info: https://vk.com/dev/implicit_flow_user
        """
        logger.info('Doing implicit flow authorization, app_id=%s',
                    self.app_id)
        auth_data = {
            'client_id': self.app_id,
            'display': 'mobile',
            'response_type': 'token',
            'scope': self.scope,
            'redirect_uri': 'https://oauth.vk.com/blank.html',
            'v': self.api_version
        }
        response = session.post(url=self.AUTHORIZE_URL,
                                data=stringify_values(auth_data))
        url_query_params = parse_url_query_params(response.url)

        if 'expires_in' in url_query_params:
            logger.info('Token will be expired in %s sec.' %
                        url_query_params['expires_in'])
        if 'access_token' in url_query_params:
            return url_query_params

        # Permissions are needed
        logger.info('Getting permissions')
        action_url = parse_form_action_url(response.text)
        logger.debug('Response form action: %s', action_url)

        if action_url:
            response = session.get(action_url)
            url_query_params = parse_url_query_params(response.url)
            return url_query_params
        try:
            response_json = response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
            logger.error(response.text)
        else:
            error_message = 'VK error: [{}] {}'.format(
                response_json['error'], response_json['error_description'])
        logger.error('Permissions obtained')
        raise VkAuthError(error_message)
コード例 #8
0
ファイル: auth.py プロジェクト: Strain88/vk-requests
    def send_api_request(self, request, captcha_response=None):
        url = self.API_URL + request.get_method_name()
        vk_api = request.get_api()

        # Prepare request arguments
        method_kwargs = {'v': self.auth_api.api_version}
        for values in (vk_api.get_default_kwargs(), request.get_method_args()):
            method_kwargs.update(stringify_values(values))

        if self.auth_api.is_token_required():
            # Auth api call if access_token weren't be got earlier
            method_kwargs['access_token'] = self.access_token
        if captcha_response:
            method_kwargs['captcha_sid'] = captcha_response['sid']
            method_kwargs['captcha_key'] = captcha_response['key']

        response = self.http_session.post(
            url=url, data=method_kwargs, timeout=vk_api.get_timeout())
        return response
コード例 #9
0
ファイル: auth.py プロジェクト: gsdu8g9/vk-requests
    def send_api_request(self, request, captcha_response=None):
        url = self.API_URL + request.get_method_name()
        vk_api = request.get_api()

        # Prepare request arguments
        method_kwargs = {'v': self.auth_api.api_version}
        for values in (vk_api.get_default_kwargs(), request.get_method_args()):
            method_kwargs.update(stringify_values(values))

        if self.auth_api.is_token_required():
            # Auth api call if access_token weren't be got earlier
            method_kwargs['access_token'] = self.access_token
        if captcha_response:
            method_kwargs['captcha_sid'] = captcha_response['sid']
            method_kwargs['captcha_key'] = captcha_response['key']

        response = self.http_session.post(url=url,
                                          data=method_kwargs,
                                          timeout=vk_api.get_timeout())
        return response
コード例 #10
0
ファイル: auth.py プロジェクト: Strain88/vk-requests
    def do_oauth2_authorization(self, session):
        """ OAuth2. More info: https://vk.com/dev/auth_mobile
        """
        logger.info('Doing oauth2')
        auth_data = {
            'client_id': self.app_id,
            'display': 'mobile',
            'response_type': 'token',
            'scope': self.scope,
            'v': self.api_version
        }
        response = session.post(url=self.AUTHORIZE_URL,
                                data=stringify_values(auth_data))
        url_query_params = parse_url_query_params(response.url)
        if 'expires_in' in url_query_params:
            logger.info('Token will be expired in %s sec.' %
                        url_query_params['expires_in'])
        if 'access_token' in url_query_params:
            return url_query_params

        # Permissions are needed
        logger.info('Getting permissions')
        action_url = parse_form_action_url(response.text)
        logger.debug('Response form action: %s', action_url)

        if action_url:
            response = session.get(action_url)
            url_query_params = parse_url_query_params(response.url)
            return url_query_params
        try:
            response_json = response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
            logger.error(response.text)
        else:
            error_message = 'VK error: [{}] {}'.format(
                response_json['error'], response_json['error_description'])
        logger.error('Permissions obtained')
        raise VkAuthError(error_message)
コード例 #11
0
ファイル: auth.py プロジェクト: anatolykazantsev/vk-requests
    def do_oauth2_authorization(self, session):
        """ OAuth2. More info: https://vk.com/dev/auth_mobile
        """
        logger.info('Doing oauth2')
        auth_data = {
            'client_id': self.app_id,
            'display': 'mobile',
            'response_type': 'token',
            'scope': self.scope,
            'v': self.api_version
        }
        response = session.post(url=self.AUTHORIZE_URL,
                                data=stringify_values(auth_data))
        url_query_params = parse_url_query_params(response.url)
        if 'expires_in' in url_query_params:
            logger.info('Token will be expired in %s sec.' %
                        url_query_params['expires_in'])
        if 'access_token' in url_query_params:
            return url_query_params

        # Permissions is needed
        logger.info('Getting permissions')
        form_action = parse_form_action_url(response.text)
        logger.debug('Response form action: %s', form_action)

        if form_action:
            response = session.get(form_action)
            url_query_params = parse_url_query_params(response.url)
            return url_query_params
        try:
            response_json = response.json()
        except ValueError:  # not JSON in response
            error_message = 'OAuth2 grant access error'
            logger.error(response.text)
        else:
            error_message = 'VK error: [{}] {}'.format(
                response_json['error'], response_json['error_description'])
        logger.error('Permissions obtained')
        raise VkAuthError(error_message)
コード例 #12
0
 def test_stringify_int_values(self):
     values = {'user_ids': [1, 2, 3]}
     self.assertEqual(utils.stringify_values(values),
                      {'user_ids': u'1,2,3'})
コード例 #13
0
 def test_stringify_3(self):
     self.assertEqual({1: u'стр,стр2'},
                      utils.stringify_values({1: [u'стр', u'стр2']}))
コード例 #14
0
 def test_stringify_2(self):
     self.assertEqual({1: u'str,стр2'},
                      utils.stringify_values({1: ['str', u'стр2']}))
コード例 #15
0
 def test_stringify(self):
     self.assertEqual({1: 'str,str2'},
                      utils.stringify_values({1: ['str', 'str2']}))
コード例 #16
0
ファイル: test_base.py プロジェクト: alexfromvl/vk-requests
 def test_stringify_3(self):
     self.assertEqual({1: u'стр,стр2'},
                      utils.stringify_values({1: [u'стр', u'стр2']}))
コード例 #17
0
ファイル: test_base.py プロジェクト: alexfromvl/vk-requests
 def test_stringify_2(self):
     self.assertEqual({1: u'str,стр2'},
                      utils.stringify_values({1: ['str', u'стр2']}))
コード例 #18
0
ファイル: test_base.py プロジェクト: alexfromvl/vk-requests
 def test_stringify_int_values(self):
     values = {'user_ids': [1, 2, 3]}
     self.assertEqual(utils.stringify_values(values),
                      {'user_ids': u'1,2,3'})
コード例 #19
0
ファイル: test_base.py プロジェクト: alexfromvl/vk-requests
 def test_stringify_wrong_input(self):
     with self.assertRaises(ValueError) as err:
         values = utils.stringify_values('wrong input')
         self.assertIsNone(values)
         self.assertIn('Data must be dict', str(err))
コード例 #20
0
 def test_stringify_wrong_input(self):
     with self.assertRaises(ValueError) as err:
         values = utils.stringify_values('wrong input')
         self.assertIsNone(values)
         self.assertIn('Data must be dict', str(err))
コード例 #21
0
ファイル: test_base.py プロジェクト: alexfromvl/vk-requests
 def test_stringify(self):
     self.assertEqual(
         {1: 'str,str2'}, utils.stringify_values({1: ['str', 'str2']}))