python-oauth2是一个python oauth库,与2.6、2.7、3.3和3.4等python版本完全兼容。许多其他下游软件包(例如Flask-Oauth)都依赖此库。
注意:此库实现OAuth 1.0而不是OAuth 2.0
该代码最初是由Leah Culver和Andy Smith的oauth.py代码派生的。一些测试来自Vic Fryzel的分支,而经过改进的Request类和更多测试从Mark Paschal的分支中合并。此代码与其前辈之间存在许多显着差异:
100%单元测试覆盖率。
该DataStore对象已被完全撕掉。在为库创建单元测试时,我发现该实现存在一些重大错误,并与Andy Smith确认它从未完全成熟。
类不再以开头OAuth。
在Request类现在从扩展dict。
该库可能不再与Python 2.3兼容。
在Client类的工作,并从延伸httplib2。这是一个薄包装器,可自动处理您可能希望发出的任何普通HTTP请求的签名。
签署一个请求
import oauth2 as oauth
import time
# Set the API endpoint
url = "http://example.com/photos"
# Set the base oauth_* parameters along with any other parameters required
# for the API call.
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': str(int(time.time())),
'user': 'joestump',
'photoid': 555555555555
}
# Set up instances of our Token and Consumer. The Consumer.key and
# Consumer.secret are given to you by the API provider. The Token.key and
# Token.secret is given to you after a three-legged authentication.
token = oauth.Token(key="tok-test-key", secret="tok-test-secret")
consumer = oauth.Consumer(key="con-test-key", secret="con-test-secret")
# Set our token/key parameters
params['oauth_token'] = token.key
params['oauth_consumer_key'] = consumer.key
# Create our request. Change method, etc. accordingly.
req = oauth.Request(method="GET", url=url, parameters=params)
# Sign the request.
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)
使用客户端
import oauth2 as oauth
# Create your consumer with the proper key/secret.
consumer = oauth.Consumer(key="your-twitter-consumer-key",
secret="your-twitter-consumer-secret")
# Request token URL for Twitter.
request_token_url = "https://api.twitter.com/oauth/request_token"
# Create our client.
client = oauth.Client(consumer)
# The OAuth Client request works just like httplib2 for the most part.
resp, content = client.request(request_token_url, "GET")
print resp
print content
IMAP
import oauth2 as oauth
import oauth2.clients.imap as imaplib
# Set up your Consumer and Token as per usual. Just like any other
# three-legged OAuth request.
consumer = oauth.Consumer('your_consumer_key', 'your_consumer_secret')
token = oauth.Token('your_users_3_legged_token',
'your_users_3_legged_token_secret')
# Setup the URL according to Google's XOAUTH implementation. Be sure
# to replace the email here with the appropriate email address that
# you wish to access.
url = "https://mail.google.com/mail/b/your_users_email@gmail.com/imap/"
conn = imaplib.IMAP4_SSL('imap.googlemail.com')
conn.debug = 4
# This is the only thing in the API for impaplib.IMAP4_SSL that has
# changed. You now authenticate with the URL, consumer, and token.
conn.authenticate(url, consumer, token)
# Once authenticated everything from the impalib.IMAP4_SSL class will
# work as per usual without any modification to your code.
conn.select('INBOX')
print conn.list()
SMTP
import oauth2 as oauth
import oauth2.clients.smtp as smtplib
# Set up your Consumer and Token as per usual. Just like any other
# three-legged OAuth request.
consumer = oauth.Consumer('your_consumer_key', 'your_consumer_secret')
token = oauth.Token('your_users_3_legged_token',
'your_users_3_legged_token_secret')
# Setup the URL according to Google's XOAUTH implementation. Be sure
# to replace the email here with the appropriate email address that
# you wish to access.
url = "https://mail.google.com/mail/b/your_users_email@gmail.com/smtp/"
conn = smtplib.SMTP('smtp.googlemail.com', 587)
conn.set_debuglevel(True)
conn.ehlo('test')
conn.starttls()
# Again the only thing modified from smtplib.SMTP is the authenticate
# method, which works identically to the imaplib.IMAP4_SSL method.
conn.authenticate(url, consumer, token)
您可以通过pip软件包进行安装或者下载python oauth2的安装包手动安装,我们建议使用virtualenv。
您可以在命令行中使用以下命令运行测试:
$ pip install -r requirements.txt
$ python setup.py test