-
-
Save VerizonMediaOwner/e6be950f74c5a8071329f1d9a50e3158 to your computer and use it in GitHub Desktop.
""" | |
Weather API Python sample code | |
Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms. | |
$ python --version | |
Python 2.7.10 | |
""" | |
import time, uuid, urllib, urllib2 | |
import hmac, hashlib | |
from base64 import b64encode | |
""" | |
Basic info | |
""" | |
url = 'https://weather-ydn-yql.media.yahoo.com/forecastrss' | |
method = 'GET' | |
app_id = 'your-app-id' | |
consumer_key = 'your-consumer-key' | |
consumer_secret = 'your-consumer-secret' | |
concat = '&' | |
query = {'location': 'sunnyvale,ca', 'format': 'json'} | |
oauth = { | |
'oauth_consumer_key': consumer_key, | |
'oauth_nonce': uuid.uuid4().hex, | |
'oauth_signature_method': 'HMAC-SHA1', | |
'oauth_timestamp': str(int(time.time())), | |
'oauth_version': '1.0' | |
} | |
""" | |
Prepare signature string (merge all params and SORT them) | |
""" | |
merged_params = query.copy() | |
merged_params.update(oauth) | |
sorted_params = [k + '=' + urllib.quote(merged_params[k], safe='') for k in sorted(merged_params.keys())] | |
signature_base_str = method + concat + urllib.quote(url, safe='') + concat + urllib.quote(concat.join(sorted_params), safe='') | |
""" | |
Generate signature | |
""" | |
composite_key = urllib.quote(consumer_secret, safe='') + concat | |
oauth_signature = b64encode(hmac.new(composite_key, signature_base_str, hashlib.sha1).digest()) | |
""" | |
Prepare Authorization header | |
""" | |
oauth['oauth_signature'] = oauth_signature | |
auth_header = 'OAuth ' + ', '.join(['{}="{}"'.format(k,v) for k,v in oauth.iteritems()]) | |
""" | |
Send request | |
""" | |
url = url + '?' + urllib.urlencode(query) | |
request = urllib2.Request(url) | |
request.add_header('Authorization', auth_header) | |
request.add_header('X-Yahoo-App-Id', app_id) | |
response = urllib2.urlopen(request).read() | |
print(response) |
@ubdussamad I took another look at my code and it looks like if I just use the yahoo pypi library I can authenticate with the credentials using keyring so that shouldn't be causing any issues. I also just printed the strings to standard output and did not see any whitespace or illegal characters. This is leading me to believe that the user agent is causing issues. I haven't done too many web requests in python. My system is running python 3.7 on windows but the desired goal is to run this script on a linux OS so I'm not 100% sure what user agent to be using but I will do some research. I will comment below if I find a solution to get the script running. Thanks!!
@ubdussamad I had accidentally misclicked a "h" in my keyring import so it wasn't grabbing the secret key. Works flawlessly now. Thanks! 😄
@GrantBirki Well, it's good that it worked out. That's why I don't use key rings for projects since they add extra margin of error. :) But sometimes it's just nessasary. I forgot the snippet can be considered under MIT licence.
Hi @GrantBirki , I tested my code again and it works on my machine. (Python 3.5.2 & Python 3.7 [GCC 5.4.0 20160609] on linux). My best guess analyzing the error would be that your header is somehow messed up, maybe some text with illegal characters or the whole header is a different object or something.
What python version are you using? Plus if you could double check your credentials for any unintended white space or something maybe?
(usage of keyring might be the cause, have your tried pasting the credential values itself?)