示例#1
0
    def give_access(self, html, opener):
        '''
        Authorizate application by parsing "Receiving access rights" page with html form and
        filling needed parameters. By success you are redirected to the url contains application
        access token. If redirect url contains query parameter with name "error" an error occur.

        See: https://vk.com/developers.php?id=-1_37230422&s=1

        Keyword arguments:
        html    -- HTML content of the "Receiving access rights" page with application authorization form.
        opener  --

        Return -- Redirect url with access token with /blank.html. (string)
        '''
        #Find and parse application authorization form
        parser = FormParser()
        parser.feed(html)
        parser.close()

        if not parser.form_parsed or parser.url is None:
                raise RuntimeError("Something wrong with a parser. Unable parse VK application authorization form.")

        if parser.method == "post":
            response = opener.open(parser.url, urllib.urlencode(parser.params))
        else:
            raise NotImplementedError("Form method '%s'" % parser.params.method + "for application authorization \
            form submission is currently not supported. Please implement it if there is a need.")

        return response.geturl()
示例#2
0
    def auth_user(self, email, password, client_id, scope, opener):
        '''
        Authorizate user by parsing VK WAP login page with html form and filling needed parameters.
        Returns responce content and redirect url. By success you are redirected to "Receiving access rights" page
        with application authorization form. If redirect url ends with /blank.html an error occured or application
        was already authorized by user. TODO: If error and if success redirect to the same url. Find some allways
        available and small size page to redirect if success.

        See: https://vk.com/developers.php?id=-1_37230422&s=1

        Keyword arguments:
        email    -- Email of VK user gives access to the application (string)
        password -- Password of VK user gives access to the application (string)
        app_id   -- APP_ID of your VK client application (string)
        scope    -- Access rights application needs to obtain. (string/list)
                    See: https://vk.com/developers.php?oid=-17680044&p=Application_Access_Rights

        Returns: -- Content of a page you was redirected after submission of the authorization form
                    and its url. (list [content, url])
        '''
        #Get authorization page content
        response = opener.open(
            "https://oauth.vk.com/oauth/authorize?" + \
            "redirect_uri=https://oauth.vk.com/blank.html&response_type=token&" + \
            "client_id=%s&scope=%s&display=wap" % (client_id, ",".join(scope))
            )
        html = response.read()
        #Find and parse user authorization form
        parser = FormParser()
        parser.feed(html)
        parser.close()

        if not parser.form_parsed or parser.url is None or "pass" not in parser.params or \
        "email" not in parser.params:
                raise RuntimeError("Something wrong with a parser. Unable parse VK authorization form.")
        #Set forms parameters, need to be filled by user
        parser.params["email"] = email
        parser.params["pass"] = password

        if parser.method == "post":
            response = opener.open(parser.url, urllib.urlencode(parser.params))
        else:
            raise NotImplementedError("Method '%s'" % parser.params.method % " for user authorization form \
                submission is currently not supported. Please implement it if there is a need.")
        return response.read(), response.geturl()