Skip to content

Instantly share code, notes, and snippets.

@michaelhelmick
Created January 7, 2012 17:03
Show Gist options
  • Save michaelhelmick/1575323 to your computer and use it in GitHub Desktop.
Save michaelhelmick/1575323 to your computer and use it in GitHub Desktop.
Python OAuth2 - Return to_header() in alpha order without "oauth_body_hash"
def to_header(self, realm=''):
"""Serialize as a header for an HTTPAuth request."""
oauth_params = ((k, v) for k, v in self.items()
if k.startswith('oauth_'))
stringy_params = ((k, escape(str(v))) for k, v in oauth_params)
abc_params = {}
for k, v in stringy_params:
if k == 'oauth_body_hash':
continue
abc_params[k] = v
abc_keys = sorted(abc_params)
header_params = ['%s="%s"' % (k, abc_params[k]) for k in abc_keys]
#print header_params
#print ', '.join(header_params)
#header_params = ('%s="%s"' % (k, v) for k, v in stringy_params)
params_header = ', '.join(header_params)
auth_header = 'OAuth '
if params_header:
auth_header = "%s, %s" % (auth_header, params_header)
auth_header = auth_header.replace('OAuth , ', 'OAuth ')
print 'AH: ', auth_header
return {'Authorization': auth_header}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment