Пример #1
0
    def __step2_get_oauth_request_token(self, oauth_id):
        """
            Use the user login & password to get an oauth request token (required only once)
        """

        c, r = http._post(
            self.auth_package.OAUTH + "auth/",
            data={
                "action": "accepted",
                "oauth": oauth_id,
                "login": self.auth_package.login,
                "user_pwd": self.auth_package.password,
                "account": "r",
                "credentials": "r",
            },
        )
        data = r.read()
        c.close()

        if r.status == 302:
            location = r.getheader("location", "")
            if not location.startswith(self.auth_package.redirect_uri):
                raise Exception("Got an unexpected redirection to %s" % location)
            query = urlparse.urlsplit(location).query
            query_dict = dict(urlparse.parse_qsl(query))
            if "code" in query_dict:
                self._token = query_dict["code"]  # Oauth Request Token
        else:
            raise Exception("Got unexpected http code %s (%s)" % (r.status, r.reason))
Пример #2
0
    def _requestAccessToken(self):
        oauth_request_token = self.requestTokenManager.token

        c, r = http._post(
            self.auth_package.OAUTH + "token/",
            {
                "code": oauth_request_token,
                "redirect_uri": self.auth_package.redirect_uri,
                "grant_type": "authorization_code",
            },
            {
                "Authorization": "Basic "
                + (
                    "{0}:{1}".format(self.auth_package.client_id, self.auth_package.client_secret)
                    .encode("base64")
                    .replace("\n", "")
                )
            },
        )

        rdata = r.read()
        c.close()

        if r.status != 200:
            try:
                err = json.loads(rdata)
                err["code"] = r.status
            except Exception as e:
                logging.error(rdata)
                err = {}

            raise Exception(
                "Unable to get oauth access token, "
                "wrong client_id or client_secret ? (%s) http code %s" % (str(err), r.status)
            )

        oauth_token = json.loads(rdata)
        if oauth_token["token_type"].lower() != "bearer":
            raise Exception("Unsupported access token type")
        self._token = oauth_token["access_token"]
        self._refresh_token = oauth_token["refresh_token"]
        self._expire = datetime.now() + timedelta(seconds=oauth_token["expires_in"] - 10)