Exemple #1
0
    def authorize(self, username, password):
        # TODO: This method is very messy, maybe do some cleanup?

        client = APIClient(app_key=const.APP_KEY,
                           app_secret=const.APP_SECRET,
                           redirect_uri=const.CALLBACK_URL)

        # Step 1: Get the authorize url from Sina
        authorize_url = client.get_authorize_url()

        # Step 2: Send the authorize info to Sina and get the authorize_code
        # TODO: Rewrite them with urllib/urllib2
        oauth2 = const.OAUTH2_PARAMETER
        oauth2['userId'] = username
        oauth2['passwd'] = password
        postdata = urllib.parse.urlencode(oauth2)

        conn = http.client.HTTPSConnection('api.weibo.com')
        sock = socket.create_connection((conn.host, conn.port), conn.timeout,
                                        conn.source_address)
        conn.sock = ssl.wrap_socket(sock,
                                    conn.key_file,
                                    conn.cert_file,
                                    ssl_version=ssl.PROTOCOL_TLSv1)

        try:
            conn.request(
                'POST', '/oauth2/authorize', postdata, {
                    'Referer': authorize_url,
                    'Content-Type': 'application/x-www-form-urlencoded'
                })
        except OSError:
            self.loginReturn.emit(None)
            return

        res = conn.getresponse()

        location = res.getheader('location')

        if not location:
            return self.loginReturn.emit(None)

        authorize_code = location.split('=')[1]
        conn.close()

        # Step 3: Put the authorize information into SDK
        r = client.request_access_token(authorize_code)
        access_token = r.access_token
        expires_in = r.expires_in

        client.set_access_token(access_token, expires_in)
        self.loginReturn.emit(client)
Exemple #2
0
    def authorize(self, username, password):
        # TODO: This method is very messy, maybe do some cleanup?

        client = APIClient(app_key=const.APP_KEY, app_secret=const.APP_SECRET,
                           redirect_uri=const.CALLBACK_URL)

        # Step 1: Get the authorize url from Sina
        authorize_url = client.get_authorize_url()

        # Step 2: Send the authorize info to Sina and get the authorize_code
        # TODO: Rewrite them with urllib/urllib2
        oauth2 = const.OAUTH2_PARAMETER
        oauth2['userId'] = username
        oauth2['passwd'] = password
        postdata = urllib.parse.urlencode(oauth2)

        conn = http.client.HTTPSConnection('api.weibo.com')
        sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address)
        conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)

        try:
            conn.request('POST', '/oauth2/authorize', postdata,
                         {'Referer': authorize_url,
                          'Content-Type': 'application/x-www-form-urlencoded'})
        except OSError:
            self.loginReturn.emit(None)
            return

        res = conn.getresponse()

        location = res.getheader('location')

        if not location:
            return self.loginReturn.emit(None)

        authorize_code = location.split('=')[1]
        conn.close()

        # Step 3: Put the authorize information into SDK
        r = client.request_access_token(authorize_code)
        access_token = r.access_token
        expires_in = r.expires_in

        client.set_access_token(access_token, expires_in)
        self.loginReturn.emit(client)
Exemple #3
0
    def authorize(self, username, password):
        # TODO: This method is very messy, maybe do some cleanup?

        client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)

        # Step 1: Get the authorize url from Sina
        authorize_url = client.get_authorize_url()

        # Step 2: Send the authorize info to Sina and get the authorize_code
        # TODO: Rewrite them with urllib/urllib2
        oauth2 = OAUTH2_PARAMETER
        oauth2["userId"] = username
        oauth2["passwd"] = password
        postdata = urllib.parse.urlencode(oauth2)

        conn = http.client.HTTPSConnection("api.weibo.com")
        conn.request(
            "POST",
            "/oauth2/authorize",
            postdata,
            {"Referer": authorize_url, "Content-Type": "application/x-www-form-urlencoded"},
        )

        res = conn.getresponse()

        location = res.getheader("location")

        if not location:
            return self.loginReturn.emit(None)

        authorize_code = location.split("=")[1]
        conn.close()

        # Step 3: Put the authorize information into SDK
        r = client.request_access_token(authorize_code)
        access_token = r.access_token
        expires_in = r.expires_in

        client.set_access_token(access_token, expires_in)
        self.loginReturn.emit(client)