0

Was wondering how to url encode my parameters, is this 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 this guide:

To make the Authorization header, you simply append all the values starting with “OAuth”. Each value must be 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"
musss
  • 125
  • 1
  • 4
  • 16

1 Answers1

0

You should use urllib.quote_plus or urllib.parse.quote_plus in python 3, which will handle it nicely for you.

The way you want to construct your authorization header from your params is the following:

(python3 version)

import urllib
authorization = 'OAuth ' + ', '.join([key + '="' + urllib.parse.quote_plus(str(value)) + '"' for key, value in params.items()])
http_headers = {'Authorization': authorization}

(python2 version)

import urllib
authorization = 'OAuth ' + ', '.join([key + '="' + urllib.quote_plus(str(value)) + '"' for key, value in params.items()])
http_headers = {'Authorization': authorization}

However, you may want to have a look at existing implementations, such as requests-oauth1. See here.

Flavian Hautbois
  • 2,940
  • 6
  • 28
  • 45