python - 在 Python 中为 OAuth 签名请求

标签 python oauth signature

目前我正在使用 OAuth 协议(protocol)连接 Twitter API 并使用 Python 编写代码。作为大多数用户,我认为规范中最困难的部分是处理签名。

在网上四处寻找解决方案后,我决定使用自定义代码,以便更好地了解正在发生的事情。

为了其他用户,我在这里发布了一个非常简单和简短的 Python 中 SHA1 签名规范的实现:

import hmac

from hashlib import sha1
from urllib import quote, urlencode
from base64 import b64encode
from urlparse import urlparse

def sign_request_sha1(url,method,data,secret=""):
  pu = urlparse(urlparse(url).geturl())

  normUrl = "%s://%s%s%s" % (
      pu.scheme,
      pu.hostname,
      "" if not pu.port or {"http":80,"https":443}[pu.scheme] == pu.port else ":%d" % pu.port,
      pu.path,
                            )

  names = data.keys()
  names.sort()

  sig = "%s&%s&%s" % (
          method.upper(),
          quote(normUrl,''),
          quote("&".join(["%s=%s" % (k,quote(data[k].encode('utf-8'),'')) for k in names]),''),
                     )

  key = "%s&%s" % (quote(CONSUMER_SECRET.encode('utf-8'),''),secret)

  return b64encode(hmac.new(key,sig,sha1).digest())

函数的输入参数是:

  • url:您要为特定 OAuth 请求调用的 url。
  • 方法:这必须是“GET”或“POST”,具体取决于您将如何发出请求。
  • 数据:包含请求的所有参数的字典,包括任何自定义参数但不包括“oauth_signature”参数(原因很明显)。
  • secret:您在协议(protocol)初始阶段收到的 secret token 。

我用 Twitter 对其进行了测试,它似乎可以工作,但我希望收到一些关于错误、改进等方面的评论。

最后,这里有一段代码调用了初始“请求 token ”阶段的代码:

from random import getrandbits
from base64 import b64encode
from time import time

def twitter_request_token(req,callback,errback):
  req_url="http://twitter.com:80/oauth/request_token"

  data = { \
    "oauth_consumer_key" : CONSUMER_KEY,
    "oauth_nonce" : b64encode("%0x" % getrandbits(256))[:32],
    "oauth_timestamp" : str(int(time())),
    "oauth_signature_method" : "HMAC-SHA1",
    "oauth_version" : "1.0",
    "oauth_callback" : "http://localhost:8080/",
         }

  data["oauth_signature"] = sign_request_sha1(req_url,"GET",data)

谢谢。

最佳答案

我对此的下意识 react 是 If You're Typing The Letters A-E-S Into Your Code, You're Doing It Wrong .或者,作为 redditor khafra recently reminded us of the Sicilian's version :

Haha.. you fool! You fell victim to one of the classic blunders. The most famous is: Never get involved in a land war in Asia. But only slightly less famous is this: Never attempt to roll your own crypto when there's a well-tested library that'll do it better!

我是说,我明白了。第一次看,oauth.py也没有打动我。从那以后已经做了很多工作,看起来好多了,但似乎仍然没有测试,所以我不知道。无论如何,无论是否进行测试,它已经被比您的代码更多的人审查和使用。

但这只是我在加密代码重用问题上的紧张,并不能真正帮助您弄清楚协议(protocol)机制。我觉得还不错,但最近我不太关注 OAuth 规范。

只需为该 pu.port 业务使用更多行;将条件 if 表达式、or 表达式和 {}[] 构造全部放在一行中真的 难以阅读。

如果你真的想让熟悉协议(protocol)的人审查代码,你最好问 the mailing list .如果您可以为他们提供替代 API,使他们存储库中的代码对新用户更具吸引力,那将对每个人都有好处。

关于python - 在 Python 中为 OAuth 签名请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2138656/

相关文章:

Python - 直接嵌套函数调用

python - Jinja 安装 python

python - 查找 python 内置函数的调用者

python - 为 matplotlib 文本设置固定位置

encryption - Owin WebApi 2 - 在单独的平台上解密 OAuth key

ios - 获取 SSL 证书详细信息

ruby-on-rails - 谷歌 OAuth 访问 token

android - 将原生 Google 帐户选择器与 B2C 结合使用

typescript - TsLint 说 "expected callSignature to have a typedef."是什么意思

html - 为什么这条白线出现在我的 HTML 电子邮件签名上?