Created
July 23, 2010 04:02
-
-
Save sinsoku/486991 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import getrandbits | |
from time import time | |
import hmac, hashlib | |
import sys | |
import urllib | |
import urllib2 | |
import urlparse | |
# build parameters to post | |
consumer_key = 'your <consumer_key>' | |
consumer_secret = 'your <consumer_secret' | |
token_key = 'your <token_key>' | |
token_secret = 'your <token_secret>' | |
message = '' | |
post_url = 'https://cacoo.com/api/v1/users/sinsoku.xml' | |
params = { | |
'oauth_consumer_key' : consumer_key, | |
'oauth_signature_method' : 'HMAC-SHA1', | |
'oauth_timestamp' : str(int(time())), | |
'oauth_nonce' : str(getrandbits(64)), | |
'oauth_version' : '1.0', | |
'oauth_token' : token_key, | |
} | |
params['status'] = urllib.quote(message, '') | |
params['oauth_signature'] = hmac.new( | |
'%s&%s' % (consumer_secret, token_secret), | |
'&'.join([ | |
'POST', | |
urllib.quote(post_url, ''), | |
urllib.quote('&'.join(['%s=%s' % (x, params[x]) | |
for x in sorted(params)]), '') | |
]), | |
hashlib.sha1).digest().encode('base64').strip() | |
del params['status'] | |
# post with oauth token | |
req = urllib2.Request(post_url, data = urllib.urlencode(params)) | |
req.add_data(urllib.urlencode({'status' : message})) | |
req.add_header('Authorization', 'OAuth %s' % ', '.join( | |
['%s="%s"' % (x, urllib.quote(params[x], '')) for x in params])) | |
# done! | |
print urllib2.urlopen(req).read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下記のソースを元に作成
Big Sky :: デスクトップアプリケーションでも認証可能なOAuth「xAuth」をpythonから試してみた。
http://mattn.kaoriya.net/software/lang/python/20100217004716.htm