def sign_request(self, url, url_params={}):
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.token.key,
'oauth_consumer_key': self.consumer.key
}
)
oauth_request.sign_request(
oauth2.SignatureMethod_HMAC_SHA1(),
self.consumer,
self.token
)
return oauth_request.to_url()
python类generate_nonce()的实例源码
def get_base_params():
"""Set-up the basic parameters needed for a request"""
params = {}
params['oauth_version'] = "1.0"
params['oauth_nonce'] = oauth.generate_nonce()
params['oauth_timestamp'] = int(time.time())
return params
def get_base_params():
"""Set-up the basic parameters needed for a request"""
params = {}
params['oauth_version'] = "1.0"
params['oauth_nonce'] = oauth.generate_nonce()
params['oauth_timestamp'] = int(time.time())
return params
def test_gen_nonce(self):
nonce = oauth.generate_nonce()
self.assertEqual(len(nonce), 8)
nonce = oauth.generate_nonce(20)
self.assertEqual(len(nonce), 20)
def test_gen_nonce(self):
nonce = oauth.generate_nonce()
self.assertEqual(len(nonce), 8)
nonce = oauth.generate_nonce(20)
self.assertEqual(len(nonce), 20)
def generate_oauth_params(self):
"""
Generates the oauth parameters needed for multipart/form requests
:returns: a dictionary of the proper headers that can be used
in the request
"""
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': int(time.time()),
'oauth_token': self.token.key,
'oauth_consumer_key': self.consumer.key
}
return params
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
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
def request_restaurants(user_latitude, user_longitude, radius=40000, limit=20):
"""Prepares OAuth authentication and sends the request to the API."""
user_lat_lng = user_latitude + ',' + user_longitude
url_params = {
'll': user_lat_lng,
'limit': limit,
# Sort mode: 0=Best matched (default), 1=Distance, 2=Highest Rated.
'sort': 1,
'category_filter': 'bubbletea',
# Search radius in meters. Max value is 40000 meters (25 miles).
'radius_filter': radius
}
url = 'https://{0}{1}?'.format(API_HOST, urllib.quote(SEARCH_PATH.encode('utf8')))
# creates a consumer object
consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
# creates a token object
token = oauth2.Token(TOKEN, TOKEN_SECRET)
oauth_request = oauth2.Request(
method="GET", url=url, parameters=url_params)
# adds more entries to the request dictionary (of parameters for the query, looks like)
# adds oauth bits to the oauth
oauth_request.update(
{
'oauth_nonce': oauth2.generate_nonce(),
'oauth_timestamp': oauth2.generate_timestamp(),
'oauth_token': TOKEN,
'oauth_consumer_key': CONSUMER_KEY
}
)
# hashes sensitive parts of the request
oauth_request.sign_request(
oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
# generates the final url
signed_url = oauth_request.to_url()
# open up a connection to the signed_url
conn = urllib2.urlopen(signed_url, None)
try:
response = json.loads(conn.read())
# response = conn.read()
finally:
conn.close()
yelp_location_ids = save_restaurants(response)
return yelp_location_ids