yelp.py 文件源码

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

项目:stewie 作者: nnewman 项目源码 文件源码
def request(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.parse.quote(path.encode('utf8')))

    consumer = oauth2.Consumer(CONSUMER_KEY, 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': TOKEN,
                'oauth_consumer_key': CONSUMER_KEY
            }
    )
    token = oauth2.Token(TOKEN, TOKEN_SECRET)
    oauth_request.sign_request(
            oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
    signed_url = oauth_request.to_url()

    print(('Querying {0} ...'.format(url)))

    conn = urllib.request.urlopen(signed_url, None)
    try:
        response = json.loads(conn.read().decode('utf-8'))
    finally:
        conn.close()

    return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号