python - URL encode parameters -
was wondering how url encode parameters, right?
params = {'oauth_version': "1.0", 'oauth_nonce': oauth.generate_nonce(), 'oauth_timestamp': int(time.time()), 'oauth_consumer_key': consumer_key, 'oauth_token': access_key, 'oauth_nonce' : nonce, 'oauth_consumer_key': consumer.key, 'oauth_signature': 'el078a5axgi43fbdyfg5ywy', }
following guide:
to make authorization header, append values starting “oauth”. each value must url encoded.
> 1. authorization: oauth oauth_callback="http%3a%2f%2fwww.website-tm-access.co.nz%2ftrademe-callback", > oauth_consumer_key="c74cd73fdbe37d29bdd21bab54bc70e422", > oauth_version="1.0", oauth_timestamp="1285532322", > oauth_nonce="7o3kee", oauth_signature_method="hmac-sha1", > oauth_signature="5s3%2bel078a5axgi43fbdyfg5ywy%3d"
you should use urllib.quote_plus or urllib.parse.quote_plus in python 3, handle nicely you.
the way want construct authorization header params following:
(python3 version)
import urllib authorization = 'oauth ' + ', '.join([key + '="' + urllib.parse.quote_plus(str(value)) + '"' key, value in params.items()]) http_headers = {'authorization': authorization}
(python2 version)
import urllib authorization = 'oauth ' + ', '.join([key + '="' + urllib.quote_plus(str(value)) + '"' key, value in params.items()]) http_headers = {'authorization': authorization}
however, may want have @ existing implementations, such requests-oauth1. see here.
Comments
Post a Comment