oautheventsapi.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:api_stuff 作者: tgebhart 项目源码 文件源码
def superSecureRequest(self, host, path, url_params=None):
        """Prepares OAuth authentication and sends the request to the API.
        Args:
            host (str): The domain host of the API.
            path (str): The path of the API after the domain.
            url_params (dict): An optional set of query parameters in the request.
        Returns:
            dict: The JSON response from the request.
        Raises:
            urllib2.HTTPError: An error occurs from the HTTP request.
        """
        url_params = url_params or {}
        url = 'https://{0}{1}?'.format(host, urllib.quote(path.encode('utf8')))

        consumer = oauth2.Consumer(self.oath_consumer_key, self.oath_consumer_secret)
        oauth_request = oauth2.Request(
            method="GET", url=url, parameters=url_params)

        oauth_request.update(
            {
                'oauth_nonce': oauth2.generate_nonce(),
                'oauth_timestamp': oauth2.generate_timestamp(),
                'oauth_token': self.oath_token,
                'oauth_consumer_key': self.oath_consumer_key
            }
        )
        token = oauth2.Token(self.oath_token, self.oath_token_secret)
        oauth_request.sign_request(
            oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
        signed_url = oauth_request.to_url()

        conn = urllib2.urlopen(signed_url, None)
        try:
            response = json.loads(conn.read().decode())
        finally:
            conn.close()

        return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号