Exemple #1
0
    def fetch_access_token(self, code, redirect_uri, error=None,
                           method="POST", payload_params=None,
                           async_callback=None):
        """
        Fetches an access token and a refresh token.

        :param code:
            The code returned by the OAuth 2.0 server to your callback URI.
            Set to ``None`` if an error occurred.
        :param redirect_uri:
            The URL to which the OAuth 2.0 server should redirect.
        :param error:
            Set this to the error query parameter from your callback handler
            if available. (Default ``None``.)
        :param method:
            (Default POST). The HTTP method to use.
        :param payload_params:
            Additional params to he URL-encoded into the body. (Existing
            parameters may be overridden).
        :param async_callback:
            (Optional) Asynchronous callback handler that will be called with the
            received token. If none is specified, this function returns
            the token instead.

        :returns:
            Access token and refresh token (if ``async_callback`` is
            unspecified). If ``async_callback`` is specified, ``None``
            will be returned.
        """
        # TODO: Add async_callback signature and example in documentation.
        if error:
            raise OAuthError(error)

        if not code:
            raise ValueError("argument ``code`` not specified")

        params = {
            "code": code,
            "client_id": self._client_credentials.identifier,
            "client_secret": self._client_credentials.shared_secret,
            "redirect_uri": redirect_uri,
            "grant_type": "authorization_code",
        }
        if payload_params:
            params.update(payload_params)
        body = urlencode_s(params)
        headers = {
            HEADER_CONTENT_TYPE: CONTENT_TYPE_FORM_URLENCODED,
        }
        response = self._http_client.fetch(
            RequestAdapter(method=method, url=self._token_uri,
                           body=body, headers=headers))
        if response.error:
            raise HttpError(
                "[fetch access token] OAuth 2.0 server response " \
                "error: %d - %s" % (response.status, response.reason))
        logging.info(response.content_type)
        token = json_decode(response.content)
        logging.info(token)
        return token
Exemple #2
0
    def fetch_refreshed_access_token(self,
                                     refresh_token,
                                     method="POST",
                                     payload_params=None):
        """
        Fetches a refreshed access token from the OAuth 2.0 server.

        :param refresh_token:
            The previously-obtained refresh token.
        :param method:
            (Default POST) The HTTP method to use for the request.
        :param payload_params:
            (Default ``None``) Additional payload parameters to be URL-encoded
            and added into the request body.
        :returns:
            Refreshed access token.
        """
        # TODO: Add async_callback.
        params = {
            "client_id": self._client_credentials.identifier,
            "client_secret": self._client_credentials.shared_secret,
            "refresh_token": refresh_token,
            "grant_type": "refresh_token",
        }
        if payload_params:
            params.update(payload_params)
        body = urlencode_s(params)
        headers = {
            HEADER_CONTENT_TYPE: CONTENT_TYPE_FORM_URLENCODED,
        }
        response = self._http_client.fetch(RequestAdapter(method=method,
                                                          url=self._token_uri,
                                                          body=body,
                                                          headers=headers))
        if response.error:
            raise HttpError(
                "[refresh access token] OAuth 2.0 server response " \
                "error: %d - %s" % (response.status, response.reason))
        logging.info(response.content_type)
        token = json_decode(response.content)
        logging.info(token)
        return token
Exemple #3
0
 def test_encode(self):
   # json deals with strings, not bytes.
   self.assertEqual(json_decode(json_encode(unicode_value)), unicode_value)
Exemple #4
0
 def test_decode(self):
   # decode accepts unicode only.
   self.assertEqual(json_decode(json_ufoo), ufoo)
Exemple #5
0
 def test_encode(self):
   # json deals with strings, not bytes.
   self.assertEqual(json.json_decode(json.json_encode(constants.UNICODE_VALUE)), constants.UNICODE_VALUE)
Exemple #6
0
 def test_decode(self):
   # decode accepts unicode only.
   self.assertEqual(json.json_decode(constants.JSON_UFOO), constants.UFOO)