-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxauth.py
48 lines (39 loc) · 1.7 KB
/
xauth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
#-*- coding=utf-8 -*-
import sys, urllib, re
import oauth.oauth as oauth
from urllib2 import Request, urlopen
consumer_key = '9eab891a46e90644738442f4c03d461b' # api key
consumer_secret = 'acce8a65db9d239e8ba5d9865ac6a1d6' # api secret
access_token_url = 'http://fanfou.com/oauth/access_token'
url_start = 'http://api.fanfou.com/'
url_end = '.xml'
def request_to_header(request, realm=''):
"""Serialize as a header for an HTTPAuth request."""
auth_header = 'OAuth realm="%s"' % realm
# Add the oauth parameters.
if request.parameters:
for k, v in request.parameters.iteritems():
if k.startswith('oauth_') or k.startswith('x_auth_'):
auth_header += ', %s="%s"' % (k, oauth.escape(str(v)))
return {'Authorization': auth_header}
consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
params = {}
params["x_auth_username"] = 'kuailkaiba@gmail.com'
params["x_auth_password"] = 'sfjnz6Wayol'
params["x_auth_mode"] = 'client_auth'
request = oauth.OAuthRequest.from_consumer_and_token(consumer,
http_url=access_token_url,
parameters=params)
signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
request.sign_request(signature_method, consumer, None)
headers=request_to_header(request)
resp = urlopen(Request(access_token_url, headers=headers))
token = resp.read()
m = re.match(r'oauth_token=(?P<key>[^&]+)&oauth_token_secret=(?P<secret>[^&]+)', token)
def get_oauth_token():
if m:
oauth_token = oauth.OAuthToken(m.group('key'), m.group('secret'))
print oauth_token
if __name__ == '__main__':
get_oauth_token()